gpt4 book ai didi

html - 使子 div 大于固定位置的父 div

转载 作者:行者123 更新时间:2023-11-27 23:27:51 24 4
gpt4 key购买 nike

请看下面的 fiddle : https://jsfiddle.net/a9ravkf5/3/

#navbar{
position: fixed;
top: 0;
left: 0;
height:40px;
background-color: grey;
width: 100%;
}
#sidebar{
position: fixed;
top: 40px;
left: 0;
z-index: 0;
height:100%;
width: 200px;
background-color: black;
}

#dropdown{
position: absolute;
top: 0px;
left 0px;
width: 500px;
color: #fff;
z-index: 10;
padding-left: 10px;
padding-top: 10px;
padding-bottom: 10px;
background-color: blue;

}

#content{
position: absolute;
top: 40px;
left: 200px;
right: 0px;
min-height: 300px;
background-color: green;
}
<div id="navbar">
</div>

<div id="sidebar">
<div id="dropdown">
This is a very long sentance that should be visible in its entirety.
</div>
</div>

<div id="content">
</div>

我想让蓝色元素比固定位置的父元素更大(更宽)。这将是一个用于在边栏内选择选项的下拉菜单,我希望它能扩展里面的内容而不是换行到多行(更大的高度)。

执行此操作的最佳解决方案是什么?

最佳答案

您的子 div 大于包含的固定 div。您看不到所有内容的原因是因为您的 #content div 显示在固定的 #sidebar div 的前面。

尝试添加 z-index#sidebar#content div,如下所示:

#sidebar {
position: fixed;
top: 40px;
left: 0;
z-index: 0;
height: 100%;
width: 200px;
background-color: black;
z-index: 2; // Here we give the sidebar a larger z-index resulting in it being showed on top of the content.
}

#content {
position: absolute;
top: 40px;
left: 200px;
right: 0px;
min-height: 300px;
background-color: green;
z-index: 1; // Here we give the content a lower z-index resulting in it being showed beneath the sidebar.
}

#navbar {
position: fixed;
top: 0;
left: 0;
height: 40px;
background-color: grey;
width: 100%;
}

#dropdown {
position: absolute;
top: 0px;
left 0px;
width: 500px;
color: #fff;
z-index: 10;
padding-left: 10px;
padding-top: 10px;
padding-bottom: 10px;
background-color: blue;
}
<div id="navbar"></div>
<div id="sidebar">
<div id="dropdown">
This is a very long sentance that should be visible in its entirety.
</div>
</div>
<div id="content"></div>

关于html - 使子 div 大于固定位置的父 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com