Html 绝对定位和文本对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2808592/
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
Absolute Positioning & Text Alignment
提问by Misha Moroshko
回答by P?r Wieslander
The div doesn't take up all the available horizontal space when absolutely positioned. Explicitly setting the width to 100% will solve the problem:
绝对定位时,div 不会占用所有可用的水平空间。将宽度显式设置为 100% 将解决问题:
HTML
HTML
<div id="my-div">I want to be centered</div>?
CSS
CSS
#my-div {
position: absolute;
bottom: 15px;
text-align: center;
width: 100%;
}
?
?
回答by Eric
Try this:
尝试这个:
You need to add left: 0
and right: 0
(not supported by IE6). Or specify a width
您需要添加left: 0
和right: 0
(IE6 不支持)。或者指定一个宽度
回答by Happy
This should work:
这应该有效:
#my-div {
left: 0;
width: 100%;
}
回答by tau
Maybe specifying a width would work. When you position:absolute
an element, it's width will shrink to the contents I believe.
也许指定宽度会起作用。当你position:absolute
是一个元素时,它的宽度会缩小到我相信的内容。