CSS Bootstrap 4 垂直对齐文本不会在卡片上居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46857136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap 4 vertical align text won't center on card
提问by tealowpillow
Trying to vertically align the text center in the following card:
尝试垂直对齐以下卡片中的文本中心:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<div class="row text-center h-100">
<div class="col-md-3 text-center my-auto">
<div class="card card-block justify-content-center" style="height: 120px">
<div class="card-body">
This is some text within a card body.
</div>
</div>
</div>
</div>
I am fully aware of the many similar questions here, however I tried various solutions and none of them seems to work for my cards.
我完全知道这里有许多类似的问题,但是我尝试了各种解决方案,但似乎没有一个对我的卡有效。
I tried "my-auto" on parent, card, and card-body. I tried "d-flex align-items-center h-100" and "justify-content-center". Also tried to play around with the display property. What am I missing?
我在 parent、card 和 card-body 上尝试了“my-auto”。我试过“d-flex align-items-center h-100”和“justify-content-center”。还尝试使用 display 属性。我错过了什么?
回答by Naren Murali
Here is a solution using bootstrap 4 documentation for flexbox
. Read more here
这是一个使用 bootstrap 4 文档的解决方案flexbox
。在这里阅读更多
What you need to know,
你需要知道的,
d-flex
-> makes the element a flexbox.
d-flex
-> 使元素成为弹性盒。
align-items-center
- vertically aligns the content.
align-items-center
- 垂直对齐内容。
justify-content-center
- horizontally aligns the content.
justify-content-center
- 水平对齐内容。
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<div class="row text-center h-100">
<div class="col-md-3 text-center my-auto">
<div class="card card-block d-flex" style="height: 120px">
<div class="card-body align-items-center d-flex justify-content-center">
This is some text within a card body.
</div>
</div>
</div>
</div>
回答by c-smile
What am I missing?
我错过了什么?
Line-height :
行高:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<div class="row text-center h-100">
<div class="col-md-3 text-center my-auto">
<div class="card card-block justify-content-center" style="height: 120px; line-height:120px;">
<div class="card-body">
This is some text within a card body.
</div>
</div>
</div>
</div>