body {
background: url(bg.jpg);
margin: 0;
}
#bg {
background-color: black;
margin: 0;
padding: 0;
height: 100vh;
opacity: 0.7;
}
#topbar {
background-color: gray;
height: 80px;
width: 100%;
}
<div id="bg">
<div id="topbar"></div>
</div>
I’ve tried adding opacity: 1
to the topbar div and rgba with alpha: 1.
The result: there`s an image, black background over it with opacity 0.7 and topbar which also has opacity 0.7.
My goal is to achieve darker image as a background and solid color topbar div.
2
Answers
#topbar
is inside of#bg
, so adjusting the opacity of#bg
also affects#topbar
. What I would do instead is omit opacity and change the background color to a transparent black.That would look like this:
This way only the background color is transparent instead of the entire element.
The answer by @CoffeedUpHacker is good, but here is an alternative which has some extra benefits.
Here I do not nest
#topbar
inside#bg
; they are siblings. I then style#bg
with thebackground-image
rather thanbody
. This has the following benefits:filter
to reduce the brightness instead of a partially-transparent black overlay (one element instead of two).background-attachment: fixed
on the body element is not currently supported on mobile devices).