垂直对齐头痛

发布于 2025-01-02 11:09:03 字数 549 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剩余の解释 2025-01-09 11:09:03

您可以给父 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.

隔岸观火 2025-01-09 11:09:03

尝试:

.item {
    position: relative;
    top: 10%;
}

可能需要调整top:10%;

Try:

.item {
    position: relative;
    top: 10%;
}

You may need to adjust top: 10%;

绅士风度i 2025-01-09 11:09:03

只要父/祖父母 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.

野侃 2025-01-09 11:09:03

vertical-align 适用于表格元素,而不是常规 div 等。为了使 vertical-align 中间工作,该元素需要设置为 display:table-cell 并且它的父级需要设置为 display:table-row

不过要小心,因为它确实改变了元素与其同级元素交互的方式,它肯定会改变页面的布局方式。

最好的用法是这样的:

<div class="table-row">
    <div class="td">lorem ipsum</div>
    <div class="td">dolor sit amat</div>
</div>

Css:

.table-row {display: table-row}
.td {display: table-cell; vertical-align: middle;}

NOTE

这不适用于左/右浮动的元素,并且它将改变边框宽度影响元素整体宽度的方式。

我只会将其与表格数据一起使用,就像我建议仅使用表格一样。

vertical-align is meant for table elements, not regular divs, etc. In order to get vertical-align middle to work, the element needs to be set to display:table-cell and it's parent needs to be set to display: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:

<div class="table-row">
    <div class="td">lorem ipsum</div>
    <div class="td">dolor sit amat</div>
</div>

Css:

.table-row {display: table-row}
.td {display: table-cell; vertical-align: middle;}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文