使子 div 大于固定位置父 div



请看下面的小提琴: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。您无法看到全部的原因是因为您的#contentdiv 显示在固定#sidebardiv 的前面。

尝试将 z 索引添加到#sidebar并像这样#contentdiv:

#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>

这是你需要的吗?您需要在内容div 和侧边栏上设置适当的 z 索引。

#navbar{
  position: fixed;
  top: 0;
  left: 0;
  height:40px;
  background-color: grey;
  width: 100%;
}
#sidebar{
  position: fixed;
  top: 40px;
  left: 0;
  z-index: 10;
  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;
  z-index: 0;
  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>

你需要设置两件事一个是你的"z指数",在 #sidebar。另一个是 #content 的"最小高度"。

喜欢

 #sidebar{
  position: fixed;
  top: 40px;
  left: 0;
  z-index: 10;
  height:100%;
  width: 200px;
  background-color: black;
}
#content{
  position: absolute;
  top: 40px;
  left: 200px;
  right: 0px;
  min-height: 400px;
  background-color: green;
}  

如果你想修复它,那么还要添加 z-index:-1; 在 #content

最新更新