【问题标题】:CSS adjust brightness of image background and not contentCSS调整图像背景而不是内容的亮度
【发布时间】:2016-07-04 03:04:01
【问题描述】:

该部分有一个背景图片,顶部有内容。 我只想降低部分中背景图像的亮度,而不是内容。

我尝试了下面的方法,但是亮度仍然适用于所有人,而不仅仅是图像。

<!-- 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);
}

【问题讨论】:

    标签: html css filter


    【解决方案1】:

    您可以添加一个新的透明黑色元素,该元素仅覆盖背景,您的 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%;
    }
    

    【讨论】:

    • 感谢建议,但亮度似乎不适用于背景
    • 对不起,我第一次写的时候写错了,我已经编辑并添加到position: absolute
    • 一个快速的建议。我如何才能使内容居中。文本对齐:中心或边距:0 自动;似乎不起作用
    • 您可能需要确保您的内容 div 具有与父级相同的宽度,因为它现在绝对定位。 width: 100% 应该可以工作
    猜你喜欢
    • 2013-10-25
    • 2013-12-23
    • 2015-03-06
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    相关资源
    最近更新 更多