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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:01:26  来源:igfitidea点击:

Absolute Positioning & Text Alignment

htmlcss

提问by Misha Moroshko

I would like some text to be centered in the bottom of the screen.

我希望一些文本在屏幕底部居中。

I tried this, but it doesn't work. It looks like absolute positioning conflicts with the alignment.

我试过这个,但它不起作用。看起来绝对定位与对齐冲突。

How could I achieve this simple task ?

我怎么能完成这个简单的任务?

回答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:

尝试这个:

http://jsfiddle.net/HRz6X/2/

http://jsfiddle.net/HRz6X/2/

You need to add left: 0and right: 0(not supported by IE6). Or specify a width

您需要添加left: 0right: 0(IE6 不支持)。或者指定一个宽度

回答by Happy

This should work:

这应该有效:

#my-div { 
  left: 0; 
  width: 100%; 
}

回答by tau

Maybe specifying a width would work. When you position:absolutean element, it's width will shrink to the contents I believe.

也许指定宽度会起作用。当你position:absolute是一个元素时,它的宽度会缩小到我相信的内容。