2017-09-27 219 views
0

我想使用透明度(不透明度)使图像变暗,以便可以更好地阅读前景文本。试图添加背景图像透明度不透明

这里是我的头HTML:

.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; 
 
}
<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>

你会看到,我说 '不透明度:1.0;'在'header-image'的最后一行,但它不起作用。

任何想法,我哪里错了?

感谢

回答

0

那么你不想改变整个DIV的过渡,只是一个形象,我猜。你应该使用::前伪元素。现在,所有的CSS属性将只适用于::前:

HTML

<!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:

.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

不能应用不透明的背景图像。

解决此问题的一种方法是将想要的图像作为背景直接放置在容器的顶部,从而将其设置为背景。然后通过对文本应用更高的z-index,将任何文本直接放在图像的顶部。

.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