css - 嵌套div的整页响应式背景

标签 css html

我用几个 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 类的 widthheight

堆栈片段

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/

相关文章:

jquery - IE7 jquery z 索引

javascript - CSS 幻灯片在单击之前不显示图像

jquery - 如何使用 div 标签和 jquery 从侧面菜单加载 html 页面

jquery - 如何获取 jqm rangeslider 和 sliderthumb 之间的标签?

javascript - Ajax 表单未在 Chrome 上提交

php - 如何摆脱页脚下方的这个额外部分 - Bootstrap

javascript - 能够通过内容的 url 控制 iframe 大小的内容 - javascript 可以利用这一点

javascript - JavaScript/HTML5 的机器可读 (WebIDL) 引用?

html - Bootstrap 4 - 如何使文本彼此处于相对位置?

javascript - 将 PHP 变量值发送到同一页面中的 JavaScript 弹出窗口