首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试向背景图像添加透明度不透明度

尝试向背景图像添加透明度不透明度
EN

Stack Overflow用户
提问于 2017-09-27 08:34:21
回答 2查看 73关注 0票数 0

我正在尝试使用透明度(opacity)来使图像变暗,以便前景文本可以更好地阅读。

这是我的标题HTML:

代码语言:javascript
复制
.header-image {
  display: block;
  width: 100%;
  text-align: center;
  /* image must be 1900 x 500 */
  background: url('back.1.jpg') no-repeat center center scroll;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  background-size: cover;
  -o-background-size: cover;
  opacity: 1.0;
}

.headline {
  padding: 120px 0;
}

.headline h1 {
  font-size: 50px;
  font-weight: 500;
  background: #24292E;
  background: rgba(36, 41, 46, 0.7);
  color: #FCFCFC;
}
代码语言:javascript
复制
<header class="header-image" style="background: url(' URL TO IMAGE') center no-repeat; background-size: cover;">
  <div class="headline">
    <div class="container">
      <h1>Headline</h1>
    </div>
  </div>
</header>

您将看到我在“header-image”的最后一行添加了“opacity:1.0;”,但它不起作用。

你知道我在哪里做错了吗?

谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-09-27 08:55:56

你不会想要改变整个div的过渡,我想只是一个图像。您应该使用::将它放在伪元素之前。现在,所有css属性将仅应用于::before:

HTML

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<header class="header-image">
    <div class="headline">
     <div class="container">
      <h1>Headlines</h1>
     </div>
    </div>
</header>
</body>
</html>

CSS:

代码语言:javascript
复制
.header-image {
    position: relative;
    overflow: hidden;
    height: 180px;
}

.header-image:before {
    content: ' ';
    display: block;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    opacity: 0.5;
    background-image: url('http://placekitten.com/1500/1000');
    background-repeat: no-repeat;
    background-position: 50% 0;
    -ms-background-size: cover;
    -o-background-size: cover;
    -moz-background-size: cover;
    -webkit-background-size: cover;
    background-size: cover;
}

.headline {
}

.headline h1 {
    position: relative;
    z-index: 1;
    font-size: 50px;
    font-weight: 500;
    background: #24292E;
    background: rgba(36, 41, 46, 0.3);
    color: #FCFCFC;
    margin: 0;
}

https://jsbin.com/lisakez/edit?html,css,output

票数 0
EN

Stack Overflow用户

发布于 2017-09-27 09:20:39

不能将不透明度应用于背景图像。

解决这个问题的一种方法是将你想要作为背景的图像直接放在容器的顶部,这会给人一种它被设置为背景的印象。然后,通过对文本应用更高的z索引,将任何文本直接放在图像的顶部。

代码语言:javascript
复制
.header-image {
    position: relative;
    overflow: hidden;
    text-align: center;
}

.header-image img {
    position: absolute; 
    left: 0; 
    top: 0; 
    width: 100%;
    height: auto; 
    opacity: 0.6;
}

.headline h1 { 
    position: relative;
    z-index: 2;
    font-size: 50px;
    font-weight: 500;
    background: #24292E;
    background: rgba(36, 41, 46, 0.7);
    color: #FCFCFC; 
}

<header class="header-image"> 
    <div class="headline">
        <div class="container">
            <h1>Headline</h1>
            <img src="your-image.jpg">
        </div>
    </div>
</header>

See fiddle

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46437713

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档