Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the last child of each repeated series of classes using CSS?

I am trying to create a chat application. Inside the chat container, I will add both sent and received messages. It is clear that the messages will not be in a specific order.

I need to add some margin at the end of each repeated series of sent or received messages.

<div class="container">
   <div class="sent_message"></div>
   <div class="sent_message"></div>
   <!-- NEED MARGIN -->
   <div class="received_message"></div>
   <div class="received_message"></div>
   <div class="received_message"></div>
   <!-- NEED MARGIN -->
   <div class="sent_message"></div>
   <!-- NEED MARGIN -->
   <div class="received_message"></div>
   <!-- NEED MARGIN -->
   <div class="sent_message"></div>
   <div class="sent_message"></div>
   <!-- NEED MARGIN -->
</div>

Is there any CSS selector to select the last-child of each of the series of classes, or should I change the structure of the HTML?

like image 482
Reza Amya Avatar asked Oct 09 '19 10:10

Reza Amya


People also ask

How do I get the last 4 child in CSS?

Syntax. The nth-last-child pseudo-class is specified with a single argument, which represents the pattern for matching elements, counting from the end. :nth-last-child( <nth> [ of <complex-selector-list> ]? )

How do you target the last two child in CSS?

The examples of an+b are as follows: :nth-last-child(odd) /* represents all odd foo elements in their parent element, counting from the last one */ :nth-last-child(-n+2) /* represents the two last elements */

How do I select the first and last child in CSS?

How to select an element that is the first or last child of its parent. The :first-child pseudo class means "if this element is the first child of its parent". :last-child means "if this element is the last child of its parent". Note that only element nodes (HTML tags) count, these pseudo-classes ignore text nodes.


1 Answers

You could select each .received_message follwed by a .sent_message and vice-versa, instead, using the selector .received_message + .sent_message, .sent_message + .received_message.

.received_message + .sent_message, .sent_message + .received_message{
  margin-top: 20px;
}

.container div{
  width: 120px;
  height: 40px;
  border: 1px dashed white;
}
.received_message{
  background: rebeccapurple;
}
.sent_message{
  background: orange;
}
<div class="container">
   <div class="sent_message"></div>
   <div class="sent_message"></div>
   <!-- NEED MARGIN -->
   <div class="received_message"></div>
   <div class="received_message"></div>
   <div class="received_message"></div>
   <!-- NEED MARGIN -->
   <div class="sent_message"></div>
   <!-- NEED MARGIN -->
   <div class="received_message"></div>
   <!-- NEED MARGIN -->
   <div class="sent_message"></div>
   <div class="sent_message"></div>
   <!-- NEED MARGIN -->
</div>
like image 66
Sebastian Simon Avatar answered Sep 20 '22 22:09

Sebastian Simon