如果我提前不知道宽度是多少,我如何可以中心的东西? [英] How can I center something if I don't know ahead of time what the width is?
问题描述
我试图使一个段落标签与其中的一些文本在一个div中心,但我似乎无法使用margin:0自动中心,而无需为段落指定一个固定的宽度。我不想指定一个固定的宽度,因为我会有动态文本进入段落标签,它将总是一个不同的宽度,基于它是多少文本。
I am trying to center a paragraph tag with some text in it within a div, but I can't seem to center it using margin: 0 auto without having to specify a fixed width for the paragraph. I don't want to specify a fixed width, because I will have dynamic text coming into the paragraph tag and it will always be a different width based on how much text it is.
有没有人知道我如何能够将div标签放在div中,而不必为段落指定固定宽度或不使用表格?
Does anyone know how I can center the paragraph tag within the div without having to specify a fixed width for the paragraph or without using tables?
推荐答案
以下是使用样式表执行此操作的方法。
Here's how to do it using a style sheet.
样式表:
div.center-content
{
text-align: center;
}
div.center-content p
{
text-align: left;
margin-left: auto;
margin-right: auto;
}
HTML:
<div class="center-content">
<p>Lorem ipsum dolor sit amet</p>
</div>
这篇关于如果我提前不知道宽度是多少,我如何可以中心的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!