【问题标题】:Overlying Divs with position absolute具有绝对位置的覆盖 div
【发布时间】:2018-06-09 23:23:39
【问题描述】:

我已将一个<div> 放置在另一个具有绝对位置且左边距为 10 像素(这是因为我想显示阴影)的位置上。 问题是基础<div>的内容显示在标题<div>的左侧,如何避免这种情况?如果我使用border-left而不是padding-left,则不会显示阴影。

#container {
    background-color: green;
    display: flex;
}
.item {
    background-color: white;
    border: 1px solid black;
    flex-grow: 0;
}

.bigContent{
  height: 1000px;
  width: 1000px;
}

.scroll{
  overflow: auto;
  height: 300px;
  width: 500px;
}

.header{
  height: 280px;
  width: 200px;
  position: absolute;
  z-index: 1;
  padding-left:10px;
  overflow:hidden;
  display: inline;
  border: solid 1px;
}

.headerContent{
  background: lightgrey;
  height: 280px;
  width: 200px;
  box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
}
<div id="container">
  <div id="header" class="header">
    <div class="headerContent">
      <div class="shadow">
        <table width="500px">
          <tr><td>Header</td></tr>
        </table>
      </div>  
    </div>
  </div>
    <div class="item scroll">
      <div class="bigContent">
         <table width="500px">
            <tr><td>Some content</td><td>Some content</td><td>Some content</td><td>Some content</td><td>Some content</td><td>Some content</td></tr>
         </table>
      </div>      
    </div>
</div>

【问题讨论】:

  • 添加 .header {background-color: white);

标签: css css-position padding absolute


【解决方案1】:

问题是基础&lt;div&gt; 的内容显示在 标题左侧&lt;div&gt;,如何避免?

您已在标题 div 上正确添加 padding-left 以允许阴影,并且标题 div 确实位于内容之上。但是,默认情况下,标题 div 具有 透明 背景。

所以为了隐藏下面的内容 - 你可以简单地在标题 div 中添加一个白色背景。

.header {
  ...
  background-color: white;
}

#container {
    background-color: green;
    display: flex;
}
.item {
    background-color: white;
    border: 1px solid black;
    flex-grow: 0;
}

.bigContent{
  height: 1000px;
  width: 1000px;
}

.scroll{
  overflow: auto;
  height: 300px;
  width: 500px;
}

.header{
  height: 280px;
  width: 200px;
  position: absolute;
  z-index: 1;
  padding-left:10px;
  overflow:hidden;
  display: inline;
  border: solid 1px;
  background-color: white;
}

.headerContent{
  background: lightgrey;
  height: 280px;
  width: 200px;
  box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
}
<div id="container">
  <div id="header" class="header">
    <div class="headerContent">
      <div class="shadow">
        <table width="500px">
          <tr><td>Header</td></tr>
        </table>
      </div>  
    </div>
  </div>
    <div class="item scroll">
      <div class="bigContent">
         <table width="500px">
            <tr><td>Some content</td><td>Some content</td><td>Some content</td><td>Some content</td><td>Some content</td><td>Some content</td></tr>
         </table>
      </div>      
    </div>
</div>

【讨论】:

  • 虽然这很快解决了问题,但 imo 最好去掉覆盖的 div 元素并改用伪元素。
  • 这取决于 OP 需要覆盖什么。如果他里面有链接怎么办?我假设 OP 发布了他需要的简化版本
猜你喜欢
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多