我用几个 div 中的 CSS 剪辑制作了一个 BG(嵌套在 .background
中)我如何配置整个事物以响应?当我将宽度或高度设置为 100% 时,背景根本不会呈现。
body,
html {
margin: 0px;
}
.bg0 {
position: relative;
width: 1366px;
height: 675px;
background-color: #3B3B3B;
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
.bg1 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #333333;
background-repeat: no-repeat;
background-size: cover;
-webkit-clip-path: polygon(0 0, 80% 0, 35% 50%, 0 12%);
clip-path: polygon(0 0, 80% 0, 35% 50%, 0 12%);
}
.bg2 {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
background-color: #2D2D2D;
background-repeat: no-repeat;
background-size: cover;
-webkit-clip-path: polygon(0 12%, 0 86%, 35% 50%);
clip-path: polygon(0 12%, 0 86%, 35% 50%);
}
#wrapper {
position: relative;
width: 200px;
height: 200px;
}
.content {
position: relative;
}
.background {
position: absolute;
}
<div id="wrapper">
<div class="background">
<div class="bg0"></div>
<div class="bg1"></div>
<div class="bg2"></div>
</div>
<div class="content">
<h1>Content!!</h1>
</div>
</div>
最佳答案
首先....为什么你对 bg0
类使用固定宽度,而且你给 bg0
类的宽度比它的父元素要大 #包装器
...
我认为不需要...只需设置 .background
类的 width
和 height
堆栈片段
body,
html {
margin: 0px;
}
.bg0 {
position: relative;
width: 100%;
height: 100%;
background-color: #3B3B3B;
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
.bg1 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #333333;
background-repeat: no-repeat;
background-size: cover;
-webkit-clip-path: polygon(0 0, 80% 0, 35% 50%, 0 12%);
clip-path: polygon(0 0, 80% 0, 35% 50%, 0 12%);
}
.bg2 {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
background-color: #2D2D2D;
background-repeat: no-repeat;
background-size: cover;
-webkit-clip-path: polygon(0 12%, 0 86%, 35% 50%);
clip-path: polygon(0 12%, 0 86%, 35% 50%);
}
#wrapper {
position: relative;
width: 200px;
height: 200px;
}
.content {
position: relative;
}
.background {
position: absolute;
top:0;
left:0;
width:100%;
height:100%;
}
<div id="wrapper">
<div class="background">
<div class="bg0"></div>
<div class="bg1"></div>
<div class="bg2"></div>
</div>
<div class="content">
<h1>Content!!</h1>
</div>
</div>
关于css - 嵌套div的整页响应式背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48717432/