试图设置一个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>
最终结果如下:
我已经尝试在text-align div
上将高度设置为100%,但没有运气。
一种可能的解决方案是从my-auto
元素中删除col-4
并用以下代码替换目标div(具有text-align
的那个):
<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>
您应该使用d-flex
进行显示:在列内容上使用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>