CSS调整图像背景的亮度而不是内容

时间:2016-03-17 16:14:49

标签: html css filter

该部分的背景图片的内容位于顶部。 我想降低部分中背景图像的亮度而不是内容。

我已尝试过以下功能,但亮度仍然适用于所有人,而不仅仅是图像。

<!-- Section -->
<div id="section1">
    <div id="content">



    <h1 class="heading">headline text</h1>
    <h4 class="subHeading"> Sub-headline text</h4>

    <!-- Call to action button -->
    <br><br>
    <button> Join our wait list </button>

</div>

#section1 {
background: url('../images/headerimage1.jpg') center center no-repeat;
background-size: cover;
-webkit-filter: brightness(0.5);
filter: brightness(0.5);
    padding-top: 100px;
    padding-bottom: 100px;
    padding-left: 10%;
    padding-right: 10%;
    color: white;
    text-align: center;
}

#content {
  -webkit-filter: brightness(1);
filter: brightness(1);
}

1 个答案:

答案 0 :(得分:4)

你可以添加一个透明黑色的新元素,它只覆盖背景,div的内容就位于它前面。

<div id="section1">
    <div id="content">
    </div>
</div>

CSS:

#section1 {
    background: url('../images/headerimage1.jpg') center center no-repeat;
    background-size: cover;
    position: relative;
}

#content {
    position: absolute;
}

#section1::before {
    content: '';
    display: block;
    position: absolute;
    background-color: #000;
    opacity: 0.5;
    width: 100%;
    height: 100%;
}
相关问题