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

single background for child elements

How do I set the one background for all inner-blocks

<div class="outer-block">
   <div class="inner-block"></div>
   <div class="inner-block"></div>
   <div class="inner-block"></div>
</div>

To make it look like in the photo

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

In the example with the photo, I used the property:
background-attachment: fixed;

.outer-block {
  position: relative;
  overflow: hidden;
}

.inner-block {
  background-image: url('./dsa.jpg');
  width: 100%;
  height: 200px;
  margin: 10px;
  background-size: contain;
  background-attachment: fixed;
  background-repeat: no-repeat;
}

But this implementation has a drawback. When scrolling the page, the image remains fixed relative to the page.
Task: make sure that when scrolling, the internal blocks and the image move synchronously (expected behavior)

>Solution :

Use pseudo element positioned relatively to the container and clipped at child level

.outer-block {
  position: relative; /* relative on the container */
  width: 300px;
}
.inner-block {
  height: 150px;
  clip-path: inset(0); /* clip-path on the child */
  margin: 10px;
}
/* your background */
.inner-block:before {
  content:"";
  position: absolute;
  inset: 0;
  background: url(https://picsum.photos/id/1/200/600) center/cover;
}
/**/

body {
  background: lightblue;
}
<div class="outer-block">
  <div class="inner-block"></div>
  <div class="inner-block"></div>
  <div class="inner-block"></div>
</div>
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