Вы можете использовать селектор +
вместо ~
, поэтому вы можете выбрать только нужную p
и избежать проблем со спецификой / порядком:
h3 + p,
h3 + p + p{
color: blue;
}
h4 + p,
h4 + p + p{
color: red;
}
<h3>Heading 1</h3>
<p>This section of the paragraphs only to be shown as blue...</p>
<p>This section of the paragraphs only to be shown as blue...</p>
<h4>Heading 2</h4>
<p>This section of the paragraphs only to be shown as red...</p>
<p>This section of the paragraphs only to be shown as red...</p>
<h3>Heading 1 (Again)</h3>
<p>This section of the paragraphs only to be shown as blue...</p>
<p>This section of the paragraphs only to be shown as blue...</p>
Или вы можете рассмотреть более сложный селектор, используя ~
, если число p
не определено (порядок CSS важенздесь)
h3 ~ p{ /*target the first ones*/
color: blue;
}
h4 ~ p{ /*target the next ones*/
color: Orange;
}
h4 ~ h3 ~ p { /*target the last ones*/
color: red;
}
<h3>Heading 1</h3>
<p>This section of the paragraphs only to be shown as blue...</p>
<p>This section of the paragraphs only to be shown as blue...</p>
<h4>Heading 2</h4>
<p>This section of the paragraphs only to be shown as orange...</p>
<p>This section of the paragraphs only to be shown as orange...</p>
<p>This section of the paragraphs only to be shown as orange...</p>
<p>This section of the paragraphs only to be shown as orange...</p>
<h3>Heading 1 (Again)</h3>
<p>This section of the paragraphs only to be shown as red...</p>
<p>This section of the paragraphs only to be shown as red...</p>
<p>This section of the paragraphs only to be shown as red...</p>