背景不透明影响子文本

时间:2018-11-08 14:40:57

标签: html css

这可能是重复的帖子,但我似乎无法提前找到道歉的html / css布局解决方案:

HTML代码:

<section class="palm-section text-center" id="palm-section">
    <div class="palm-img">
        <h1>Palm Hotel</h1>
        <div class="break-line"></div>
    </div>
</section>

CSS代码:

 .palm-img h1{ /* Child text to background */
  position: absolute;
  top: 0;
  padding-top: 10px;
  font-size: 62px;
  color: white;
  font-weight: 400;
  z-index: 1;
}


.palm-img{ /* Background */
  position: relative;
  width: 100%;
  padding-top: 150px;
  padding-bottom: 100px;
  height: 100%;
  background: url(/images/the-palm-962785_1280.png) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  color: white;
  background-size: cover;
  opacity: 0.5;
}

输出:

enter image description here

文本受父级背景的不透明度影响

更新:

在CSS之前和之后都不起作用,并且将背景埋在另一个div中似乎不起作用

2 个答案:

答案 0 :(得分:1)

.palm-img h1{ /* Child text to background */
  position: absolute;
  top: 0;
  padding-top: 10px;
  font-size: 62px;
  color: white;
  font-weight: 400;
  z-index: 1;
}


.palm-img{ /* Background */
  position: relative;
  width: 100%;
  padding-top: 150px;
  padding-bottom: 100px;
  height: 100%;
  color: white;
}

.palm-img::after {
  content: "";
  background: url(https://www.fillmurray.com/500/500) no-repeat center center fixed; 
  opacity: 0.5;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}
<section class="palm-section text-center" id="palm-section">
    <div class="palm-img">
        <h1>Non transparent text</h1>
        <div class="break-line"></div>
    </div>
</section>

您可以使用分层伪元素作为背景

答案 1 :(得分:1)

您可以像这样向thepolygon添加透明颜色

Fiddle demo

background

background: linear-gradient( rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.5) ), url(https://images.pexels.com/photos/531880/pexels-photo-531880.jpeg?cs=srgb&dl=cold- vehicles-water-531880.jpg&fm=jpg) no-repeat center center fixed; 接受更多参数,因此首先像这样Background通过rgba添加透明的白色。然后添加URL。

linear-gradient( rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.5) )
 .palm-img h1{ /* Child text to background */
  position: absolute;
  top: 0;
  padding-top: 10px;
  font-size: 62px;
  color: white;
  font-weight: 400;
}


.palm-img{ /* Background */
  position: relative;
  width: 100%;
  padding-top: 150px;
  padding-bottom: 100px;
  height: 100%;
  background: linear-gradient( rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.5) ), url(https://images.pexels.com/photos/531880/pexels-photo-531880.jpeg?cs=srgb&dl=cold-vehicles-water-531880.jpg&fm=jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  color: white;
  background-size: cover;
}