2017-09-27 13 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の推移、私は推測するだけで、画像を変更する必要はありません。 :: before pseudo-elementを使用して配置する必要があります。

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

今、すべてのCSS属性にのみに::前に適用されます。

これを回避する方法の1つは、背景として設定したい画像をコンテナの上に直接置くことです。これは、背景として設定されている印象を与えます。次に、より高いz-インデックスをテキストに適用して、画像の上に直接テキストを配置します。

.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

関連する問題