简体   繁体   English

一个div在另一个中的垂直对齐不起作用

[英]Vertical alignment of one div within another not working

This is a cut down version of my code: http://jsfiddle.net/f6GCz/ 这是我的代码的简化版本: http : //jsfiddle.net/f6GCz/

I'm trying to vertically center the "learn more" box using this code: 我正在尝试使用以下代码将“了解更多”框垂直居中:

   position: absolute;
   margin-left: auto;
   margin-right: auto;

What would cause this box not to be centered vertically? 是什么导致此框未垂直居中? Even when I apply a width to it it doesnt work. 即使当我对其应用宽度时,它也不起作用。 Cross browser solutions (IE8+) preferred! 首选跨浏览器解决方案(IE8 +)!

If you have to use absolute positioning, add this to your "learn more" box css: 如果必须使用绝对定位,请将其添加到“了解更多”框css中:

left:50%;
width:90px;
margin-left:-45px;

Where the margin-left is always 1/2 of the container width. 左边距始终是容器宽度的1/2。

Update 更新

If you don't have to use absolute, then give the div a width and set the margin to: 如果不必使用绝对值,则为div设置宽度并将边距设置为:

margin:0 auto;

If desired, the width can be a percentage instead of a px value. 如果需要,宽度可以是百分比而不是px值。

Updated fiddle with both results. 更新了两个结果的小提琴

Update Two 更新二

If you want to align your learn more box without specifying it as absolute, put it inside a footer container that is positioned at the bottom, then use margin:0 auto on it. 如果要在不将其指定为绝对值的情况下对齐了解更多信息的框,请将其放在位于底部的页脚容器中,然后在其上使用margin:0 auto Something like this: Fiddle 2 像这样: 小提琴2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM