I have four grid blocks in my website. I need to paint red on first and last. Other 2 in the middle should be left as white.
How can I accomplish this without using Javascipt?
You can use CSS first-child and last-child property. Try running the snippet below.
.containers {
height: 50px;
width: 50px;
border: 1px solid;
display: inline-block
}
.containers:first-child,
.containers:last-child {
background: red;
}
<div class="blocks">
<div class="containers"></div>
<div class="containers"></div>
<div class="containers"></div>
<div class="containers"></div>
</div>
You could also use nth-child here as well and target the blocks you want to.
.wrap {
width: 100%;
}
.wrap div {
width: 25px;
height: 25px;
border: 1px solid #ccc;
display: inline-block;
margin: 15px;
}
.wrap div:nth-child(1),
.wrap div:nth-child(4) {
background: red;
}
<div class='wrap'>
<div></div>
<div></div>
<div></div>
<div></div>
</div>