CSS: Changing the opacity of the background-image only

rosiequ

Ruxin Qu

Posted on July 10, 2022

CSS: Changing the opacity of the background-image only
  • Add a opaque background
body::before{
  content: " ";
  background-image: url();
  background-size: cover;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  opacity: 0.5;}
Enter fullscreen mode Exit fullscreen mode
  • Adding text on top of the opaque image HTML:
<body>
    <div class="overlay">
        <div class="header">
            <h1>Hello World</h1>
        </div>
    </div>
</body>

Enter fullscreen mode Exit fullscreen mode

CSS:

body {
    margin: 0;
}

.header {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    background-image: url('https://lumiere-a.akamaihd.net/v1/images/sa_pixar_virtualbg_coco_16x9_9ccd7110.jpeg');
    background-position: center;
    height: 450px;
    width: 90%;
    margin: 0 auto;
}

.overlay::before {
    content: '';
    display: block;
    position: absolute;
    z-index: 1;
    height: 450px;
    width: 90%;
    left: 5%;
    background: rgb(0, 0, 0, 0.6);
}

.overlay * {
    position: relative;
}

h1 {
    color: #fff;
    z-index: 2;
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
rosiequ
Ruxin Qu

Posted on July 10, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

10 Tricks for HTML Semantic Tags
html 10 Tricks for HTML Semantic Tags

November 29, 2024

PAIN:The other story of css
webdev PAIN:The other story of css

November 21, 2024

Add Flip Card in your Project
webdev Add Flip Card in your Project

November 20, 2024

Add Flip Card in your Project
webdev Add Flip Card in your Project

November 20, 2024

10 essential CSS resources
css 10 essential CSS resources

November 13, 2024