使绝对定位div展开父div高度

编程入门 行业动态 更新时间:2024-10-27 08:31:11
本文介绍了使绝对定位div展开父div高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我面对一个挑战,我绝望解决asap。正如你可以看到在下面的CSS,我想让child2在child1之前自己定位。这是因为我目前开发的网站也应该在移动设备上,child2应该在底部,因为它包含我想在移动设备上的内容下面的导航。 - 为什么不是2个母版?这是唯一在整个HTML中重新定位的2个div,因此这个小改动的2个母版页是过度的。

HTML:

< div id =parent> < div class =child1>< / div> < div class =child2>< / div> < / div>

CSS:

code> parent {position:relative;宽度:100%; } child1 {width:auto; margin-left:160px; } child2 {width:145px; position:absolute; top:0px; bottom:0px; }

child2具有动态高度,因为不同的子网站可能具有更多或更少的导航项。

我知道绝对定位的元素从流中删除,因此被其他元素忽略。 我尝试设置 overflow:hidden; / code>在父div,但没有帮助,也没有clearfix。

我最后的手段将是javascript重新定位两个div

解决方案

你回答了问题:我知道绝对定位的元素从流中移除,因此被其他元素忽略。所以你不能根据绝对定位的元素来设置父级的高度。

你可以使用固定的高度,或者你需要涉及JS。

I'm facing a challenge which I'm desperate to solve asap. As you can see in the CSS below, I want child2 to position itself before child1. This is because the site I'm currently developing should also work on mobile devices, on which the child2 should be at the bottom, as it contains the navigation which I want below the content on the mobile devices. - Why not 2 masterpages? This is the only 2 divs which are repositioned in the entire HTML, so 2 masterpages for this minor change is an overkill.

HTML:

<div id="parent"> <div class="child1"></div> <div class="child2"></div> </div>

CSS:

parent { position: relative; width: 100%; } child1 { width: auto; margin-left: 160px; } child2 { width: 145px; position: absolute; top: 0px; bottom: 0px; }

child2 has dynamic height, as different subsites could have more or less navigation items.

I know that absolute positioned elements are removed from the flow, thus ignored by other elements. I tried setting overflow:hidden; on the parent div, but that didn't help, neither does the clearfix.

My last resort will be javascript to reposition the two divs accordingly, but for now I'll try and see if there exist a non-javascript way of doing this.

解决方案

You answered the question by yourself: "I know that absolute positioned elements are removed from the flow, thus ignored by other elements." So you can't set the parents height according to an absolutely positioned element.

You either use fixed heights or you need to involve JS.

更多推荐

使绝对定位div展开父div高度

本文发布于:2023-10-08 04:34:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1471523.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高度   div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!