Reputation: 527
I have the following:
<div class="chapter">
<p>chapter 1, paragraph 1</p>
<p>chapter 1, paragraph 2</p>
<div class="section">
<p>chapter 1, section 1, paragraph 1</p>
<p>chapter 1, section 1, paragraph 2</p>
</div>
</div>
<div class="chapter">
<p>chapter 2, paragraph 1</p>
<div class="section">
<p>chapter 2, section 1, paragraph 1</p>
<p>chapter 2, section 1, paragraph 2</p>
</div>
<div class="section">
<p>chapter 2, section 2, paragraph 1</p>
<p>chapter 2, section 2, paragraph 2</p>
</div>
</div>
Using CSS, is it possible to select all elements <p>
, except the first, for each element .chapter
and .section
? For instance, I'd like to select <p>chapter 1, paragraph 2</p>
, <p>chapter 1, section 1, paragraph 2</p>
, <p>chapter 2, section 1, paragraph 2</p>
, <p>chapter 2, section 2, paragraph 2</p>
, and so on.
Thanks.
Upvotes: 2
Views: 156
Reputation: 253308
One further approach, as part of a drive of completism, perpaps:
.chapter p:first-child ~ p {
color: red;
}
.chapter {
border: 1px solid #000;
}
.section {
border-top: 1px solid #999;
margin-left: 2em;
}
<div class="chapter">
<p>chapter 1, paragraph 1</p>
<p>chapter 1, paragraph 2</p>
<div class="section">
<p>chapter 1, section 1, paragraph 1</p>
<p>chapter 1, section 1, paragraph 2</p>
</div>
</div>
<div class="chapter">
<p>chapter 2, paragraph 1</p>
<div class="section">
<p>chapter 2, section 1, paragraph 1</p>
<p>chapter 2, section 1, paragraph 2</p>
</div>
<div class="section">
<p>chapter 2, section 2, paragraph 1</p>
<p>chapter 2, section 2, paragraph 2</p>
</div>
</div>
References:
Upvotes: 1
Reputation: 390
You can use element+element selector for your task and here is example.
.chapter p + p,
.section p + p { };
It will select all next elements after first p tag.
Upvotes: 0
Reputation: 195972
You should use the :first-of-type
combined with the :not
.chapter > p:not(:first-of-type),
.section > p:not(:first-of-type){
color:#ccc
}
<div class="chapter">
<p>chapter 1, paragraph 1</p>
<p>chapter 1, paragraph 2</p>
<div class="section">
<p>chapter 1, section 1, paragraph 1</p>
<p>chapter 1, section 1, paragraph 2</p>
</div>
</div>
<div class="chapter">
<p>chapter 2, paragraph 1</p>
<div class="section">
<p>chapter 2, section 1, paragraph 1</p>
<p>chapter 2, section 1, paragraph 2</p>
<p>chapter 2, section 1, paragraph 3</p>
<p>chapter 2, section 1, paragraph 4</p>
</div>
<div class="section">
<p>chapter 2, section 2, paragraph 1</p>
<p>chapter 2, section 2, paragraph 2</p>
</div>
</div>
Upvotes: 2
Reputation: 157
You can use nth-child
<style>
.chapter p:nth-child(2) {
color: red;
}
.section p:nth-child(2) {
color: blue
}
</style>
<div class="chapter">
<p>chapter 1, paragraph 1</p>
<p>chapter 1, paragraph 2</p>
<div class="section">
<p>chapter 1, section 1, paragraph 1</p>
<p>chapter 1, section 1, paragraph 2</p>
</div>
</div>
<div class="chapter">
<p>chapter 2, paragraph 1</p>
<div class="section">
<p>chapter 2, section 1, paragraph 1</p>
<p>chapter 2, section 1, paragraph 2</p>
</div>
<div class="section">
<p>chapter 2, section 2, paragraph 1</p>
<p>chapter 2, section 2, paragraph 2</p>
</div>
</div>
Upvotes: 0
Reputation: 22992
Use :not(:first-child)
.chapter p:not(:first-child) {
color: red;
}
<div class="chapter">
<p>chapter 1, paragraph 1</p>
<p>chapter 1, paragraph 2</p>
<div class="section">
<p>chapter 1, section 1, paragraph 1</p>
<p>chapter 1, section 1, paragraph 2</p>
</div>
</div>
<div class="chapter">
<p>chapter 2, paragraph 1</p>
<div class="section">
<p>chapter 2, section 1, paragraph 1</p>
<p>chapter 2, section 1, paragraph 2</p>
</div>
<div class="section">
<p>chapter 2, section 2, paragraph 1</p>
<p>chapter 2, section 2, paragraph 2</p>
</div>
</div>
Upvotes: 1