0

I'm trying to give my full page background-image an opacity but when I add the opacity code, my whole html get's an opacity of 0.4 exept the image.

html { 
  background: url(../img/bg2.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

Source

I tried to add opacity like this:

html { 
  background: url(../img/bg2.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  opacity: 0.4;
}

But as I said, my whole html turns 0.4

And I tried to add 0.4 after background: url(../img/bg2.jpg) no-repeat center center fixed 0.4; but my image just goes blank then.

How can I add 0.4 only to my background-image?

yAnTar
  • 4,269
  • 9
  • 47
  • 73
Blank
  • 540
  • 2
  • 21

3 Answers3

1

Try using ::after pseudoclass:

html::after {
  content: "";
  background: url(../img/bg2.jpg) no-repeat center center fixed; 
 -webkit-background-size: cover;
 -moz-background-size: cover;
 -o-background-size: cover;
  background-size: cover;
  opacity: 0.4;  
  position: absolute; 
  top: 0; 
  left: 0; 
  right: 0; 
  bottom: 0;
  z-index: -1;
}
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
0

Set opacity for body also and try again :

body {
   opacity : 1;
}
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
Harsh Sanghani
  • 1,666
  • 1
  • 14
  • 32
0

I use the :after psuedo element on the body tag in these cases, together with some absolute positioning and the opacity property: https://jsfiddle.net/kc0sf39r/

HTML:

<body>
  content
</body>

CSS:

body {
    background: url("http://digital-photography-school.com/wp-content/uploads/flickr/205125227_3f160763a0_o.jpg") no-repeat;
    width: 100%;
    height: 100vh;
    position: relative;
}
body:before {
    position: absolute;
    top: -99px;
    bottom: -99px;
    left: -99px;
    right: -99px;
    background-color: red;
    content: " ";
    opacity: 0.5;

}
David Wilkinson
  • 5,060
  • 1
  • 18
  • 32