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

时间:2017-09-27 00:34:21

标签: html css

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

这是我的标题HTML:



<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>
&#13;
protocol UIButtonSelectorDelegate{
    func handleTap(button:DelegatingButton) //will select different functions based on DelegatingButton.actionType
}
class DelegatingButton:UIButton{
    var selectorDelegate:UIButtonSelectorDelegate?
    var actionType:String = "default"
}
&#13;
&#13;
&#13;

你会看到我添加了不透明度:1.0;&#39;在#header; image-image&#39;的最后一行但它没有用。

知道我在哪里错了吗?

由于

2 个答案:

答案 0 :(得分:0)

嗯,你不想改变整个div的转换,只是想象一个图像。你应该使用:: before伪元素放置它。现在所有的css属性都只适用于:: before:

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

答案 1 :(得分: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