是否有任何方法在主父元素中垂直或水平地居中html元素?
当前回答
对于水平居中,你可以这样做:
.centering{
float:none;
margin:0 auto
}
<div class="span8 centering"></div>
其他回答
我喜欢类似问题的答案:
在实际的表data <td>和表头<th>元素上使用bootstrap的文本中心类。所以 <td class="text-center">单元格数据</td> and <th class="text-center">头单元格数据</th>
在Bootstrap 4中,定心方法已经改变。
水平中心在引导4
文本中心仍然用于显示:内联元素 Mx-auto将中心块替换为中心显示:flex子 Offset -*或mx-auto可用于网格列居中
Mx-auto (auto x轴边距)将中心显示:块或显示:具有定义宽度的flex元素(%,vw, px等)。默认情况下,在网格列上使用Flexbox,因此也有各种Flexbox居中方法。
示范引导4水平定心
有关BS4的垂直定心,请参阅https://stackoverflow.com/a/41464397
我用这个
<style>
html, body {
height: 100%;
margin: 0;
padding: 0 0;
}
.container-fluid {
height: 100%;
display: table;
width: 100%;
padding-right: 0;
padding-left: 0;
}
.row-fluid {
height: 100%;
display: table-cell;
vertical-align: middle;
width: 100%;
}
.centering {
float: none;
margin: 0 auto;
}
</style>
<body>
<div class="container-fluid">
<div class="row-fluid">
<div class="offset3 span6 centering">
content here
</div>
</div>
</div>
</body>
Bootstrap文档:
设置要显示的元素:块和居中通过边距。可作为mixin和类使用。
<div class="center-block">...</div>
为bootstrap4垂直中心的少数项目
D-flex用于伸缩规则
用于项目垂直方向的弯曲列
Align-items-center用于水平定心
垂直居中的justification -content-center
Style ='height: 300px;'必须为设定点,其中中心为calc或使用h-100类
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/> < span class=" font - family -宋体-宋体-宋体-宋体-宋体-宋体-宋体-宋体-宋体-宋体-宋体-宋体-宋体" style=" 身高:300 px; " > <div class="p-2 bg-primary">Flex item</div> <div class="p-2 bg-primary">Flex item</div> <div class="p-2 bg-primary">Flex item</div> < / div >