2017-09-27 233 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