【发布时间】:2019-08-09 23:29:07
【问题描述】:
我有一个必须在 div 中居中的 inline-block 元素。
<div class="parent">
<div class="other">
some stuff that should not be centered
</div>
<div class="other2">
some stuff that should not be centered
</div>
<div class="element">
some element that needs to be centered
</div>
</div>
我只能访问 CSS 而不能访问 HTML。我不能在父 div 上使用“text-align:center”,因为父 div 中有很多容器。该元素必须保持为 inline-block 元素。
我只想将“元素”居中。我该怎么做?
【问题讨论】:
-
你不能在
.element类上只使用text-align: center吗? -
对不起,应该指定它不是需要居中的文本。我试图将整个“元素” div 居中,而不是其中的文本。
-
由于您不/不能使用 Flexbox,我更改了副本。看看这个答案:stackoverflow.com/a/46590898/2827823 ... 另请注意,您可以将
position: relative与这个答案一起使用:stackoverflow.com/a/34071945/2827823