html - 居中显示内联 block 的 2 个元素

标签 html css

我有这个简单的 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 autotext-align center 都不起作用。

最佳答案

text-align将对齐元素的内联内容。因此,您需要将此属性应用于 <div> 的父级

text-align - MDN:

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;
}

JSFiddle

关于html - 居中显示内联 block 的 2 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25834144/

相关文章:

html - 我的图像不会显示,但会显示背景颜色

html - 最小高度和滚动 Div

html - 当 child 移出父边界时,如何为子 div 使用父 div 之类的框架?

javascript - 将 HTML 放入 Javascript 源中还是使用 AJAX 检索 HTML?

html - 如何在 HTML 中将 header 与 UL 进行语义分组?

jquery - 如何将大写的文本大写?

jquery - 如何使动态创建的内容在 jQuery 中保持其功能?

twitter-bootstrap - Bootstrap CSS 行填充父列

html - Bootstrap 轮播指标自定义类添加

javascript - 在 jQuery 中实现 CSS