如何在CSS中的另一个特定元素之前设置最后一个元素的样式?

如何在CSS中的另一个特定元素之前设置最后一个元素的样式?

问题描述:

所以我想在CSS中实现这一点,因为我知道它是如何简单地在jQuery做。

So I would like to achieve this in CSS only, since I know how simply it is to do in jQuery.

基本上,我有一个长的列表 p 和 h4 元素,我想对每一个 p 元素放在下一个 h4 之前。例如:

Basically, I have a long list of p and h4 elements, and I would like to style every last p element before the next h4. For example:

<h4>Heading</h4>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<h4>Heading</h4>
<p>Paragraph</p>
<p>Paragraph</p>
<h4>Heading</h4>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>
<p>Paragraph</p>

在该列表中,每个 p h4 之前进行样式。

In that list, every last p needs to be styled before the following h4.

p h4 标签总是随机的。


这样的东西可以单独使用CSS吗?

Is something like this achieveable with CSS alone?

尚未。

如果主题选择器,那么您可以使用:

If the subject selector gets implemented, then you'd be able to use:

!p + h4 {
    ...
}



However that specification is still in an early draft state, so implementation is a ways off. You'd be better off using classes for the time being.