0

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?

enter image description here

2 Answers 2

5

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks guys, really helped.
2

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>

1 Comment

Thanks Daniel, This technique is more generic...Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.