覆盖绝对位置的 Div
Overlying Divs with position absolute
我将一个 <div>
放置在另一个具有绝对位置且左边距为 10px 的填充上(这是因为我想显示阴影)。
问题是 base <div>
的内容显示在 header <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>
The problem is that the content of the base <div>
is showed on the
left of the header <div>
, how to avoid this?
您已在 header div 上正确添加 padding-left
以允许阴影,并且 header div 确实位于内容。但是,header div 默认情况下具有 透明 背景。
因此,为了隐藏下面的内容 - 您可以简单地向 header 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>
我将一个 <div>
放置在另一个具有绝对位置且左边距为 10px 的填充上(这是因为我想显示阴影)。
问题是 base <div>
的内容显示在 header <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>
The problem is that the content of the base
<div>
is showed on the left of the header<div>
, how to avoid this?
您已在 header div 上正确添加 padding-left
以允许阴影,并且 header div 确实位于内容。但是,header div 默认情况下具有 透明 背景。
因此,为了隐藏下面的内容 - 您可以简单地向 header 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>