Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Way to make background images have opacity

What i mean by this is my code,

<body style="
background-image: url('https://i1.sndcdn.com/artworks-000030364386-yrh8p5-t500x500.jpg'), 
                              url('https://media.tenor.com/x8v1oNUOmg4AAAAM/rickroll-roll.gif');
background-blend-mode:multiply;
background-repeat: no-repeat;
background-size: contain;
background-position:center;
"></body>

this is not what im currently working on, but is similar to whats in my code, but i want to give a single background image an opacity, any one of them, i want to limit me using multiple background-image’s but i will if its the only way.

i have tried <body style="url('https://i1.sndcdn.com/artworks-000030364386-yrh8p5-t500x500.jpg',0.5) but to no avail, it didn’t work, i have tried searching for it, and could not find an answer. im expecting it for one background image to be partially invisible.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

also, the code is just a meme version of a serious project im working on.

>Solution :

You can achieve the effect of having a background image with opacity using a pseudo-element. This method allows you to apply an opacity to one background image while keeping the others unaffected. Here’s how you can do it:

<body>
  <div class="background"></div>
</body>
body {
  position: relative;
  overflow: hidden; /* Prevents overflow from the pseudo-element */
}

.background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url('https://i1.sndcdn.com/artworks-000030364386-yrh8p5-t500x500.jpg');
  opacity: 0.5; /* Adjust opacity here */
  z-index: 1; /* Ensures it's behind other content */
}

body {
  background-image: url('https://media.tenor.com/x8v1oNUOmg4AAAAM/rickroll-roll.gif');
  background-blend-mode: multiply;
  background-repeat: no-repeat;
  background-size: contain;
  background-position: center;
  z-index: 0; /* Ensures this background is on top */
}

Explanation:

  • Pseudo-element: By creating a <div class="background">, you can set it to cover the entire body and apply an opacity to it.
  • Z-Index: Adjusting the z-index ensures the correct stacking order of the background images.
  • Flexibility: You can easily adjust the opacity of the specific background image without affecting the other layers.

This approach keeps your HTML clean and allows for more control over your backgrounds.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading