0

I have this CSS

.jumbotron {
  background: url('/background.svg');
  opacity: 0.5;
}

but I want the opacity to target the background image only. At the moment it targets the whole jumbotron so the fonts etc. are also faded.

I've tried

.jumbotron {
  background: url('/background.svg');
  background: opacity: 0.5;
}

and

.jumbotron {
  background: url('/background.svg');
}
.jumbotron:img {
  opacity: 0.5;
}

but neither work.

Jack
  • 167
  • 3
  • 13
  • 4
    See this http://stackoverflow.com/questions/4183948/css-set-background-image-with-opacity and http://stackoverflow.com/questions/4997493/set-opacity-of-background-image-without-affecting-child-elements – Sandeep Nayak Oct 01 '15 at 12:03
  • You need to set the background as pseudo element ( `:before` / `:after`) and use opacity on this pseudo element only - Note, you can set a `z-index:-1;` to this pseudo element – Luís P. A. Oct 01 '15 at 12:04
  • Sorry I don't understand. What gets :before or :after? ... My current code is the 1st example, what do I need to do? – Jack Oct 01 '15 at 12:06

2 Answers2

0

From CSS-tricks.com:

There is no CSS property background-opacity, but you can fake it by inserting a pseudo element with regular opacity the exact size of the element behind it.

.jumbotron::after {
  content: "";
  background: url(/background.svg);
  opacity: 0.5;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: -1;   
}

As isherwood mentioned in the comments, maybe you have to set your

.jumbotron {
  position: relative;
}
Wavemaster
  • 1,794
  • 16
  • 27
-1

Set opacity:1 for anoter element in jumbotron like font etc.