html - 应用于 :before element of css did not get applied to the entire div 的背景

标签 html css

<分区>

使用 :before 伪元素应用于 div 的背景并未应用于整个 div。这是 html 和 css 代码。

HTML

<section id="pages">
 <div class="container">
  ...
  ...
  ....
 </div
</section>

CSS

#pages {
  display: flex;
  align-items: center;
  justify-content: center;
  background: url(../img/background.png);
  background-size: 100% 100%;
  padding: 80px 0 0 0;
  min-height: 100vh;
}
#pages:after{
  content: "";
  position: absolute;
  left: 0; right: 0;
  top: 0; bottom: 0;
  background: rgba(0,0,0,.5);
}

表单位于容器 div 内,页面部分延伸到页面末尾。我已经上传了网页的图像。 Click here to see the image

最佳答案

您需要将position: relative 添加到#pages div。所以 :after 伪元素相对于它是绝对定位的。

#pages {
  display: flex;
  align-items: center;
  justify-content: center;
  background: url(../img/background.png);
  background-size: 100% 100%;
  padding: 80px 0 0 0;
  min-height: 100vh;
  position: relative;
}

#pages:after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, .5);
  height: 100%;
}
<section id="pages">
 <div class="container">
  ...
  ...
  ....
 </div>
</section>

关于html - 应用于 :before element of css did not get applied to the entire div 的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58081185/

相关文章:

html - 属性不工作

javascript - 将超大屏幕背景图像溢出到导航栏

javascript - 纯 JS 的上下文菜单

html - CSS3 动画 - 停止@每个循环

html - 如何在 react 中向按钮添加禁用属性?

html - 如何在自动高度 div 的子 div 上设置 50% 高度?

html - 无法应用线性渐变

javascript - 使用 Google Earth API 和 KML 来获取 kml 并嵌入为在线 3D map

javascript - 逐字淡入文本,在当前完全可见之前开始淡出下一个单词

javascript - 是否可以在没有 CSS3 的情况下旋转 HTML 对象?