如何水平和垂直居中 在中?
我不想将 div 居中,我希望 div 的内容居中。我想要水平和垂直对齐。
抱歉,如果这太简单了或者其他什么,但这有点麻烦。
格雷
我正在使用 IE7
I don't want to center a div, I want the contents of a div to be centered. I would like horizontal and vertical alignment.
Sorry, if this is too easy or whatever, but this is kind of a hassle to get it right.
Grae
I am using IE7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您知道图像的高度和宽度,请将其绝对定位,将顶部/左侧设置为 50%,并将顶部/左侧边距设置为图像高度/宽度的负一半。
实例:http://jsfiddle.net/Zd2pz/
If you know the height and width of your image, position it absolutely, set top/left to 50% and margin-top/left to negative half the height/width of your image.
Live example: http://jsfiddle.net/Zd2pz/
我知道您说过不想将 div 居中,但使用 jquery 插件和包含要居中的元素的假 div 以跨浏览器方式实现您的要求会更容易。
我已经使用这个可以居中任何块元素的非常小的插件成功地将几乎所有内容居中。
我知道的唯一其他方法是您已经从 @simshaun & 收到的答案。 @Prhogz
编辑:根据评论请求
将脚本包含在您的头标记中
现在,如果您有一个想要在标记内居中的 DIV,只需将其用作
I know you've said that dont want to center a div but to achieve your requirement in a cross browser way would be easier using a jquery plugin and a fake div that contains your element to be centered.
I have successfully centered almost anything using this very small plugin that can center any block element.
The only other way I know are the answer that you already received from @simshaun & @Prhogz
EDIT: As per comment request
Include the script in your head tag
Now if you have a DIV that you want to center inside your markup simply use it as
尽管以下内容已过时,但它仍然适用于几乎所有浏览器
,但请访问此链接
http://www.110mb.com/forum/vertical-horizontal-alignment-of-image-within-div-t31709.0.html< /a>
although the following is obsolete, it still works for almost all browsers
however, visit this link
http://www.110mb.com/forum/vertical-horizontal-alignment-of-image-within-div-t31709.0.html
对于水平对齐,请使用
text-align:center;
对于垂直对齐,请参阅 W3 风格指南
For horizontal alignment, use
text-align:center;
For vertical alignment, see for example the W3 style guide
如果您事先知道内部元素的高度,
CSS:
HTML:
示例
If you know the inner element's height beforehand,
CSS:
HTML:
Example