html - CSS:更改具有相同背景的子div的背景不透明度

标签 html css

我正在尝试实现如图所示的框架效果。我想要的只是改变 div 内的背景不透明度。这是我尝试过的,

.parent {
   background-color:rgba(0,0,0,0.6);
 }
.child {
   background-color:transparent;
 }

但是它不起作用

html

   <html>
<head>
    <title>Xpress Music</title>
    <script src="jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="Express.css">

</head>
<body>
<div class="wrapper main">
<div class="main_box">
    <div class="logo">
        <img src="logo.png" alt="logo.png" width="8%">
    </div>
    <div class="wrapper wrapper1">
      <div class="frame" style="font-size:2vw;border-width:0px;margin-top:-10%;">COMING SOON...</div>
      <div class="frame frame1"></div>
      <div class="frame" style="border-width:0px;">
          <div class="field">
            <form action="/subscribe" method="get">
              <input type="submit" name="search" value="Go" style="float: right" />
              <div style="overflow: hidden; padding-right: .5em;">
                <input type="text" name="term" style="width: 100%;" />
               </div>
            </form>
          </div>
      </div>
    </div>
</div>
</div>
</body>
</html>

CSS

body{
    background-image: url('big-image.jpg')  ; 
    background-size: 100%;
    background-repeat: no-repeat;
    margin: 0px;
    font-family: gunplay;
}
@font-face {
    font-family: gunplay;
    src: url(gunplay.ttf);
}
.logo{
    padding-left: 2%;
    padding-top: 2%;
}
.frame {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  color: white;
  border:9px solid #f4f4f4;
}
.frame1, .frame4 {
    border-width: 12px;
}
.frame2, .frame3 {
    opacity: 0.5;
}
.frame4 {
    opacity: 0.7;
}
.frame1 {
    opacity: 0.8;
}
.main {
    width: 100%;
}
.main:after {
    padding-top: 55.62%;
}
.main_box {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
    color: white;
}
.wrapper {
  display: inline-block;
  position: relative;
}
.wrapper:after {
  display: block;
  content: '';
}

enter image description here

这是我的代码的全部部分。

最佳答案

您必须使用 :before:after 伪选择器。

这是一个尴尬的事情,但你可以这样做:http://jsfiddle.net/uLaa9fnu/1/

html {
    height:100%;
    width:100%;
}
body {
    background-image: url('http://media1.santabanta.com/full1/Outdoors/Landscapes/landscapes-267a.jpg');
    background-size: 100%;
    background-repeat: no-repeat;
    margin: 0px;
    font-family: gunplay;
    height:100%;
    width:100%;
    overflow:hidden;
}
.container {
    position:relative;
    height:100%;
    width:100%;
}
.frame {
    background-image: url('http://i.imgur.com/4AcIXsD.png');
    background-repeat: no-repeat;
    background-size: 100%;
    height:340px;
    width:300px;
    position:absolute;
    left:calc(50% - 150px);
    top:calc(50% - 170px);
}
.frame:before {
    content:'';
    background:rgba(0, 0, 0, 0.5);
    display:block;
    position:absolute;
    left:-1000%;
    top:-1000%;
    bottom:-1000%;
    right:100%;
}
.frame:after {
    content:'';
    background:rgba(0, 0, 0, 0.5);
    display:block;
    position:absolute;
    right:-1000%;
    top:-1000%;
    bottom:-1000%;
    left:100%;
}
.frame2 {
    height:100%;
    width:100%;
}
.frame2:before {
    content:'';
    background:rgba(0, 0, 0, 0.5);
    display:block;
    position:absolute;
    left:0;
    top:-1000%;
    bottom:100%;
    right:0;
}
.frame2:after {
    content:'';
    background:rgba(0, 0, 0, 0.5);
    display:block;
    position:absolute;
    right:0;
    top:100%;
    bottom:-1000%;
    left:0;
}
<div class="container">
    <div class="frame">
        <div class="frame2"></div>
    </div>
</div>

基本上,您可以在主 .frame 元素上使用 beforeafter 将覆盖左侧和右侧的不透明度,然后您在该集合中放置一个元素,其大小与使用 beforeafter 在顶部和底部放置覆盖不透明度的大小相同。

此外,正文(或任何父级)上的 overflow:hidden; 也很重要,否则您将拥有很长的滚动条。

关于html - CSS:更改具有相同背景的子div的背景不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31286894/

相关文章:

javascript - 我如何从另一个网站获得 "getImageData"?安全错误 : DOM Exception 18

javascript - gulp-useref 与几个 html 文件以及如何获取包含在 Assets 中的文件列表

html - 在 flex 方向列中右对齐

css - 带有 float LI 的 UL 底部边框未设置高度

html - 无法使用 css first-child/nth-child 定位元素?

css - 具有负 z-index 的绝对定位 HTML5 视频元素会破坏 webkit 浏览器中的背景附件

html - 带有可选边箱的流体布局

html - 无法将我的 css 文件链接到 html

php - OnClick 事件并检查 MySQL 结果

html - 不可能的? HTML <TD> 鼠标悬停边框颜色随边框折叠而变化?