'Select the last of type before another element [duplicate]
I want to add a bottom margin to the last paragraphs. It should be selected if there are other element types after the paragraph. For visual purpose I will use red backgrounds in my example. I would like the <p> before the second <h2> to be also red. I can't see how to select this element. :nth-child() will not work, as the number of elements could vary. Thank you!
p:last-of-type {
background: red;
}
<h2>Title</h2>
<p>This should not be red</p>
<p>This should be red</p>
<h2>Title</h2>
<p>This should not be red</p>
<p>This should be red</p>
Solution 1:[1]
you can give them a shared class and control this class like this example
.p_red {
background-color : red;
}
<h2>Title</h2>
<p>This should not be red</p>
<p class = "p_red">This should be red</p>
<h2>Title</h2>
<p>This should not be red</p>
<p class= "p_red">This should be red</p>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gowtham |
