HTML将段落居中,但不';t中心词

HTML将段落居中,但不';t中心词,html,css,Html,Css,好吧,标题有点奇怪。我的意思是,我不希望我的文本像这样居中于页面中心: hi! how are you i am doing good 我希望我的文本是这样的: hi how are you i am doing good 在网页的中心。我试过将段落居中,但似乎不起作用。我错过了什么明显的东西吗?对不起,这个可能很愚蠢的问题 使用边距:0自动。确保它有一个宽度段落是块级的,因此以边距:auto为中心,假设它们有一个定义的宽度(或100%,这是默认值) 所以基本上默认段落是居

好吧,标题有点奇怪。我的意思是,我不希望我的文本像这样居中于页面中心:

     hi!
  how are you 
i am doing good
我希望我的文本是这样的:

hi
how are you 
i am doing good

在网页的中心。我试过将段落居中,但似乎不起作用。我错过了什么明显的东西吗?对不起,这个可能很愚蠢的问题

使用
边距:0自动。确保它有一个
宽度

段落是块级的,因此以
边距:auto
为中心,假设它们有一个定义的宽度(或100%,这是默认值)

所以基本上默认段落是居中的,但是因为它是100%宽的,所以你看不到居中

如果添加宽度,则会得到以下结果:

p{
宽度:90%;
保证金:自动;
边框:1px纯灰;
}
Lorem ipsum door sit amet,Concertetur adipising elit。我的别名是“代表权利的最高标准、最高水平、最高尊严”,因为它是非责任性的,不代表我们的劳动成果,也不代表我们的权利!暂时性责任

在veritatis quod iste ab sequi PERPICIATIS,在假设的多洛勒姆(dolorem)多洛勒姆(dolorum)位置的合理数量。文本的左对齐是自然的,你不必做任何事情。你需要的是把整段文字放在中间。为此,请将文本包装在容器(例如div)中,并将其置于中间

一个简单的方法是为容器指定一个固定的with,并将边距设置为auto。请看一个例子。带宽度和
边距:auro

HTML:


html元素的中心标记已被弃用。您要做的是在container元素上放置边距:0 auto和宽度。我在它上面放了一个边框元素,这样你就可以看到它在做什么

JS小提琴-

HTML:


这是一个两步过程

步骤1)将段落置于页面中央

步骤2)确保文本在段落内左对齐

以下是html:

<p class="centered-paragraph">
  This is some text</br>
  Notice the paragraph is centered in the page,<br/>
  But the text is left-aligned within the paragraph
</p>

您当前使用的代码是什么?将文本放在一个div中,并将其居中对齐。它完全有效!!非常感谢你,兄弟!这太酷了:我真的很喜欢直接、简短但有用的链接。再次感谢@bardofshipping很高兴代码有帮助:)@Paulie_D纠正了拼写错误。我同意你的观点,链接将来可能会过时。在这里,快速链接帮助了op:)但是,很明显,一个带有代码示例和解释的答案才是如此有用的原因。这看起来非常整洁,非常有用。谢谢你的回答!超级容易理解。
#wrapper {
    width: 150px;
    margin: auto;
    background-color: yellow;
}
<p class="p1">
    hi<br />
    how are you <br />
    i am doing good
</p>
.p1 {
    width:300px;
    border:1px solid red;
    margin: 0 auto;
}
<p class="centered-paragraph">
  This is some text</br>
  Notice the paragraph is centered in the page,<br/>
  But the text is left-aligned within the paragraph
</p>
.centered-paragraph{

  margin : 0 auto; /* step 1: center the paragraph on the page */
  width: 400px; /* you have to give it a width, or else the browser thinks it's as wide as the whole page */
  border : 1px solid black; /* i gave it a border just so you can see what's happening more clearly */

  text-align : left; /* step 2, make sure the text inside the paragraph is left-aligned. this is the default, but it doesn't hurt to declare it exlpicitly */
}