[英]How to vertically align a div with absolute position on top of each other?
I have two divs, that I need to be places right on top of each other, inside another div. 我有两个div,我需要在另一个div中位于彼此之上。 Simple, I use z-index and give them position:absolute.
很简单,我使用z-index并给它们位置:绝对。 But the contents of each of these divs needs to be vertically aligned.
但是每个div的内容都需要垂直对齐。 When I position them on top, they do not center vertically.
当我将它们放在顶部时,它们不会垂直居中。
Is there a way to mix both of these styles to do this? 有没有办法混合这两种风格来做到这一点?
Or is this possible with jQuery somehow? 或者这可能与jQuery以某种方式?
This is basically what I have. 这基本上就是我所拥有的。
<div style="width:100%">
<div style="height:400; width:400; display:table-cell; vertical-align:middle; z-index:0; position:absolute;"><img></div>
<div style="height:400; width:400; display:table-cell; vertical-align:middle; z-index:1; position:absolute;"><img></div>
</div>
I have height and width, table-cell and vertical-align as middle. 我有高度和宽度,表格单元格和垂直对齐作为中间。 This centers the content until I add the absolute position which I need for the z-index to work.
这使内容居中,直到我添加z-index工作所需的绝对位置。
Any ideas? 有任何想法吗? Thanks,
谢谢,
give your inner divs line-height:400px;
给你的内部divs
line-height:400px;
You can add an extra-div inside the second one: http://jsfiddle.net/MDJDx/ 你可以在第二个内添加一个额外的div: http : //jsfiddle.net/MDJDx/
<div style="width:100%">
<div style="height:400px; width:400px; display:table-cell; vertical-align:middle; z-index:0; position:relative;top:0;">img</div>
<div style="height:400px; width:400px; z-index:1; position:absolute; top:0">
<div style="height:400px; width:400px; display:table-cell; vertical-align:middle; position:relative;">img</div>
</div>
</div>
Style the two inner divs with 设置两个内部div的样式
position: relative; top: 0px;
in addition to what you already have 除了你已经拥有的
Add 加
margin: auto;
to the div with the content you would like to center. 以您想要居中的内容为div。 For this to work you MUST set a width on this div too.
为此,你必须在这个div上设置一个宽度。
Maybe 也许
width: 100%;
should do the trick. 应该做的伎俩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.