垂直对齐头痛
我在 100% 高度 div 内垂直对齐 2 个 div 时遇到问题。我用谷歌搜索但未能解决。
伪代码:
<div container, fixed height>
<img, dynamic height/>
<div inner, 100% height>
<div><img/></div>
<div><img/></div>
</div>
</div>
内部div中的两个div,我希望它们位于内部div的垂直中心,但我找不到方法。它不可能知道内部 div 的高度,它只是设置为 100%,因为它上面的图像的高度是随机的。内部 div 内的 div 也将具有动态高度。
摆弄了2个小时没有结果,所以我来到这里。
您可以在其中看到它的运行页面:http://pyntmeg.no/?iframe
Im having trouble vertical aligning 2 divs inside a 100% height div. I googled but failed solving.
pseudocode:
<div container, fixed height>
<img, dynamic height/>
<div inner, 100% height>
<div><img/></div>
<div><img/></div>
</div>
</div>
The two divs in the inner div, i want them to be in the vertical center of the inner div, but i cant find a way. its not possible to know the height of the inner div, its just set to 100% because of the random height of the image above it. The divs inside the inner div will also have dynamic heights.
2 hours of fiddling around gave no results, so im coming here.
The page where you can see it in action: http://pyntmeg.no/?iframe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以给父 DIV.container 一个position :relative 属性,因为它具有固定的高度。
然后,内部 div 可以有一个位置:绝对,并将其高度设置为 100% 或可能稍微低一些。您可以使用顶部属性来移动它。
You can give the parent DIV.container a position :relative property since it has a fixed height.
The inner div can then have a position:absolute and you set its height to 100% or maybe a little lower. you can use the top property to move it around.
尝试:
可能需要调整top:10%;
Try:
You may need to adjust top: 10%;
只要父/祖父母 div 具有与其配合的宽度,您就可以将“float: left”应用于孙子 div 样式。
As long as the parent/grandparent divs have the width to work with it you can apply 'float: left' to the grandchild divs style.
vertical-align
适用于表格元素,而不是常规 div 等。为了使vertical-align
中间工作,该元素需要设置为display:table-cell
并且它的父级需要设置为display:table-row
不过要小心,因为它确实改变了元素与其同级元素交互的方式,它肯定会改变页面的布局方式。
最好的用法是这样的:
Css:
NOTE
这不适用于左/右浮动的元素,并且它将改变边框宽度影响元素整体宽度的方式。
我只会将其与表格数据一起使用,就像我建议仅使用表格一样。
vertical-align
is meant for table elements, not regular divs, etc. In order to getvertical-align
middle to work, the element needs to be set todisplay:table-cell
and it's parent needs to be set todisplay:table-row
Be careful with that, though, because it really does change the way the element interacts with it's sibling elements, and it could definitely change how your page is laid out.
The best use of this would be something like this:
Css:
NOTE
This will not work with elements that are floated left/right, and it will change how the border width effects the overall width of the element.
I would only use this with tabular data, much like I would suggest only using a table.