我有这个简单的 CSS 代码:
div {
width:100px;
height:100px;
border:1px solid #000000;
display:inline-block;
margin:0 auto;
text-align:center;
}
还有这段 HTML 代码:
<div>
</div>
<div>
</div>
我很难尝试使用 inline-block
将这 2 个 DIV 在屏幕上水平居中。 margin auto
和 text-align center
都不起作用。
最佳答案
text-align
将对齐元素的内联内容。因此,您需要将此属性应用于 <div>
的父级
The text-align CSS property describes how inline content like text is aligned in its parent block element. text-align does not control the alignment of block elements itself, only their inline content.
在上面的例子中,我猜应该是 <body>
:
body{
text-align:center;
}
关于html - 居中显示内联 block 的 2 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25834144/