0

I am trying to have a large header where there is a background photo and then a solid color overtop it with a opacity of like 90%. (so you can barely see the photo).

This is basically what I have:

.bgoverlay {
  background-image: url("https://i.pinimg.com/originals/06/51/e8/0651e8870431f9db3b26b1fd7615cec1.jpg");
}

.bgimg-1 {
  background-position: center top;
  background-repeat: no-repeat;
  top: 0px;
  background-size: 100%;
  background-color: #053426;
  min-height: 60%;
  opacity: 0.9;
}
<header class="bgimg-1 bgoverlay"></header>

edit

Thank you everyone - adding the :before is so far working out nicely. Although, when it comes to responsive, is there a way to change the background size? I tried background-size but it isn't changing.

For example, if I have the min-height at 70% so the whole header takes up 3/4th of the page but then when I shrink it to mobile size the solid background color is revealed below and the photo is small and not large enough to cover the 71% min height.

Thanks

edit 2

nvm I ended up using an @media screen to just shrink the overall header on mobile and now it looks great. Thank you!

Jennifer T
  • 17
  • 4

3 Answers3

1

You can use a pseudo-element like :before.
First, add position: relative to the header element. Then, add the pseudo-element absolutely positioned with the color and opacity, occupying the whole width and height of its parent (header).

header {
  width: 100%;
  height: 300px;
}
.bgoverlay {
  position: relative;
  background-image: url("https://i.pinimg.com/originals/06/51/e8/0651e8870431f9db3b26b1fd7615cec1.jpg");
}

.bgimg-1 {
  background-position: center top;
  background-repeat: no-repeat;
  top: 0px;
  background-size: 100%;
  min-height: 60%;
}
.bgoverlay:before {
  content: '';
  position: absolute;
  height: 100%;
  width: 100%;
  left: 0;
  top: 0;
  opacity: 0.9;
  background-color: #053426;
 }
<header class="bgimg-1 bgoverlay"></header>
Azametzin
  • 5,223
  • 12
  • 28
  • 46
1

hope this help you.

.bgoverlay {
  background-image: url("https://i.pinimg.com/originals/06/51/e8/0651e8870431f9db3b26b1fd7615cec1.jpg");
}

.bgimg-1 {
  background-position: center top;
  background-repeat: no-repeat;
  top: 0px;
  background-size: 100%;
  min-height: 300px;
  position: relative;
}

header.bgimg-1.bgoverlay:after {
  content: '';
  height: 100%;
  width: 100%;
  background: #000;
  opacity: 0.9;
  position: absolute;
}
<header class="bgimg-1 bgoverlay">

</header>
vadivel a
  • 1,754
  • 1
  • 8
  • 18
0

Use a really large inner box-shadow if the solution by Azametzin doesn't pan out for you (relative positioning might get tricky with your content). But please use their solution as a real one, and maybe mine as a fallback. It is a bit hacky after all.

header {
  width: 100%;
  height: 300px;
}
.bgoverlay {
  position: relative;
  background-image: url("https://i.pinimg.com/originals/06/51/e8/0651e8870431f9db3b26b1fd7615cec1.jpg");
  box-shadow: inset 0 0 0 100vmax rgba(2, 20, 15, 0.9)
}

.bgimg-1 {
  background-position: center top;
  background-repeat: no-repeat;
  top: 0px;
  background-size: 100%;
  min-height: 60%;
}
<header class="bgimg-1 bgoverlay"></header>
chriskirknielsen
  • 2,839
  • 2
  • 14
  • 24