尝试设置div
来填充整列,但没有运气。这是我当前的代码,有什么想法吗?
<div class="card">
<div class="row">
<div class="col-md-4 my-auto">
<div style="text-align: center;background-color:rgba(0,0,0,.03);" class="h-100">Some date</div>
</div>
<div class="col-md-8 px-3">
<div class="card-body">
<h4 class="card-title">Some event</h4>
<p class="card-text">Some description</p>
<a href="#" class="btn btn-primary">More info</a>
</div>
</div>
</div>
</div>
它最终像这样:
我尝试在文本对齐 div
上将高度设置为 100%,但是没有运气。 / p>
答案 0 :(得分:0)
一种可能的解决方案是从my-auto
元素中删除col-4
并用以下代码替换目标div(具有text-align
的目标div):
<div style="background-color:rgba(0,0,0,.03);" class="d-flex h-100 align-items-center justify-content-center">
Some date
</div>
示例:
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<div class="card">
<div class="row">
<div class="col-md-4">
<div style="background-color:rgba(0,0,0,.03);" class="d-flex h-100 align-items-center justify-content-center">
Some date
</div>
</div>
<div class="col-md-8 px-3">
<div class="card-body">
<h4 class="card-title">Some event</h4>
<p class="card-text">Some description</p>
<a href="#" class="btn btn-primary">More info</a>
</div>
</div>
</div>
</div>
但是,我将仅将自定义background-color
添加到col-4
元素,并将其内容居中放置,就像在下一个示例中一样(删除内部div
)。我相信这种方式更干净:
.custom-bg {
background-color: rgba(0,0,0,.03);
}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<div class="card">
<div class="row">
<div class="col-md-4 custom-bg d-flex justify-content-center align-items-center">
Some date
</div>
<div class="col-md-8 px-3">
<div class="card-body">
<h4 class="card-title">Some event</h4>
<p class="card-text">Some description</p>
<a href="#" class="btn btn-primary">More info</a>
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
您应该在列内容上使用d-flex
进行display:flex,然后使用flexbox utils将其居中...
<div class="card">
<div class="row">
<div class="col-md-4">
<div style="text-align: center;background-color:rgba(0,0,0,.03);" class="h-100 d-flex flex-column justify-content-center">
Some date
</div>
</div>
<div class="col-md-8 px-3">
<div class="card-body">
<h4 class="card-title">Some event</h4>
<p class="card-text">Some description</p>
<a href="#" class="btn btn-primary">More info</a>
</div>
</div>
</div>
</div>