I am trying to produce the following result:
Desired outcome
However I am getting this so far and I am stuck.
What I am getting
This is my code:
.base {
background: #e2e2e2;
width: 1020px;
height: 350px;
position: absolute;
top: 100px;
left: 130px;
}
<div class="section5" style="width:100%;background-color:#f5f5f5;height:550px;position: relative;">
<div class="base"></div>
<div class="square1" style="width:255px;height:193px;background-color:#969696; z-index:1;"></div>
</div>
2
Answers
It might help, it’s derived with the help of
z-index
, you can adjust the rest of the boxes.We start with the base of our HTML:
And style:
Now we can use
nth-of-type
to make theodd
squares white:To calculate the absolute position of the squares, if we want the first and last square to have a
50px
gap from the.section5
square, and we haven
squares, we can calculate the distanced
between two adjacent squares like this:d = (W - 2x - w) / (n - 1)
where
W
is the width of the.section5
andx
is the50px
gap we mentioned earlier. So thed
in this case will be110px
, so theleft
of each square needs to be increased by110px
from the previous one.The same logic applies to the
top
.And by applying those, we get the final result (open it in full view)
An alternative approach using CSS variables: