html - 在背景图像上应用不透明度滤镜?

标签 html css

我想在我的网页上的背景图像上应用不透明度。

我如何实现这一点?我只希望它只出现在图像区域。

这是我的 JSFiddle .

HTML:

<header class="main-header " style="background-image:url('http://cdn.c.photoshelter.com/img-get/I00004urs0axX2VI/s/860/860/0801A130.jpg');">

        <div class="sticky-header">
            <div class="row">
                <nav class="medium-10 columns primary menu">
                    <ul class="inline naked">
                        <li><a href="">test</a></li>
                    </ul>
                </nav>
            </div>
        </div>

CSS:

.sticky-header {
padding-top: 30px;
width: 100%;
margin: 0 auto;
position: fixed;
z-index: 1000;
background-color: #ffffff;
background-size: auto 100%;
border-bottom: 5px solid #f2f2f2;
top: 0;
left: 0;
}

.main-header {
width: 100%;
max-width: 1480px;
position: fixed;
background-image: url("http://cdn.c.photoshelter.com/img-get/I00004urs0axX2VI/s/860/860/0801A130.jpg");
background-position-y: -30px;
background-repeat: no-repeat;
background-size: auto 100%;
background-position: center top;
position: relative;
transition: background-position ease-in 0.3s;
height: 720px;
margin: 0 auto;
}

感谢您的帮助

最佳答案

你可以使用伪元素,因为你不能在 css 中对背景应用不透明度

.main-header {
height: 720px;
}

.sticky-header {
padding-top: 30px;
width: 100%;
margin: 0 auto;
position: fixed;
z-index: 1000;
background-color: #ffffff;
background-size: auto 100%;
border-bottom: 5px solid #f2f2f2;
top: 0;
left: 0;
}

.main-header {
width: 100%;
max-width: 1480px;
position: fixed;
background-position-y: -30px;
background-repeat: no-repeat;
background-size: auto 100%;
background-position: center top;
position: relative;
transition: background-position ease-in 0.3s;
height: 720px;
margin: 0 auto;
z-index:1
}

.main-header:before{
    position: absolute;
    content: '';
    width: 100%;
    height: 100%;
    background-image: url("http://cdn.c.photoshelter.com/img-get/I00004urs0axX2VI/s/860/860/0801A130.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    left:0;
    top: 0;
    z-index: 0;
    opacity: .4
}
<header class="main-header">
     
    <div class="sticky-header">
        <div class="row">
            <nav class="medium-10 columns primary menu">
                <ul class="inline naked">
                    <li><a href="">test</a></li>
                </ul>
            </nav>
        </div>
    </div>
</header>

关于html - 在背景图像上应用不透明度滤镜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28187189/

相关文章:

javascript - 在 404 代码上隐藏图像,仍然显示损坏的图像几毫秒

javascript - 创建同一 html 页面的弹出版本,但带有附加样式表

javascript - 尝试创建响应式布局,但右侧 div 不断推过左侧 div 的顶部

html - 最新浏览器的基准和测试站点

html - 在源 SCSS 文件中找到类名,该文件在浏览器中被编译成单个 CSS 文件

javascript - 使用 angularjs 在新页面中查看所选行的详细信息

html - 如何在父 [Angular] 中两次调用的子组件上动态应用 CSS

asp.net - W3C 验证包含 CSS 的 UserControl

html - css:将颜色混合到现有背景颜色中的类?

javascript - 在 HTML 页面中显示 html 空白代码