Html 文字对齐:居中;不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10408072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
text-align:center; doesn't work
提问by Xitcod13
I have been messing around with the layout of my website and my text-align : center;
stopped working
that happened after i set all my divs position to relative to make the parent div change size when I add components to it with javascript.
我一直在搞乱我的网站布局,并且text-align : center;
在我将所有 div 位置设置为相对以使父 div 更改大小后,我 停止了工作,当我使用 javascript 向其添加组件时。
Here are both of my CSS files:
这是我的两个 CSS 文件:
You can see the new one in the JSfiddle link below.
您可以在下面的 JSfiddle 链接中看到新的。
jsFiddle: http://jsfiddle.net/2WvrV/
jsFiddle:http: //jsfiddle.net/2WvrV/
I also provide the code for the old website (which aligns the text properly) :
我还提供了旧网站的代码(正确对齐文本):
http://jsfiddle.net/fiddlerOnDaRoof/fQpjX/
http://jsfiddle.net/fiddlerOnDaRoof/fQpjX/
the old HTML is very similar to the new version i just added the style="float:left;" in one of the divs
旧 HTML 与新版本非常相似,我刚刚添加了 style="float:left;" 在其中一个 div 中
采纳答案by Xitcod13
Finally figured it out. All i needed to do is to add
clear:left;
to my loginBttn
div
after that everything worked fine
终于想通了。我需要做的就是在一切正常之后添加
clear:left;
到我的loginBttn
div
thanks for trying to help everybody
感谢您尝试帮助每个人
回答by Graham Conzett
Short answer: your text isn't centered because the elements are floated, and floated elements "shrink" to the content, even if it's a block level element.
简短回答:您的文本未居中,因为元素是浮动的,并且浮动元素会“缩小”到内容,即使它是块级元素。
Can you explain more what this means?
你能解释一下这意味着什么吗?
I had to set all my position to relative to make the parent div change size when I add components to it with javascript
当我使用 javascript 向父 div 添加组件时,我必须将所有位置设置为相对位置
回答by izolate
Anywhere you have float:left;
in your CSS, add width: 100%;
after it. Floating will kill your desired center alignment.
float:left;
在 CSS 中的任何地方,width: 100%;
在它之后添加。浮动将杀死您想要的中心对齐。
Also, add text-align: center;
to #login
另外,添加text-align: center;
到#login
回答by Biel Gonzalez Pibernat
You have forgot to add width: 100%;
in your CSS class.
您忘记添加width: 100%;
CSS 类了。
回答by NarayaN
try this. i guess it might help you
尝试这个。我想它可能会帮助你
text-align:justify;