如何在CSS中的另一个特定元素之前设置最后一个元素的样式? [英] How to style the last element before another specific element in CSS?

查看:235
本文介绍了如何在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.

这篇关于如何在CSS中的另一个特定元素之前设置最后一个元素的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆