仅为元素中的最后两个跨度添加样式
您好,我有这段代码,每个单词都包含在 span 标记中,我只想向最后两个 span 添加样式,即使
标记具有
标签里面并且它有
标签,条件应该是相同的,最后两个 span 在
标签中。
PS 没有 em 标签就可以正常工作
.st span:nth-last-child(-n + 2) {
background-color: yellow;
}
<p class="st">
<span>With</span>
<span>ipsum</span>
<em>
<span>dolor</span>
<span>sit</span>
</em>
<span>amet,</span> consectetur adipisicing elit.
</p>
<p class="st">
<span>Lorem</span>
<span>ipsum</span>
<em>
<span>dolor</span>
<span>sit</span>
</em>
<span>amet,</span>
<span>consectetur</span>
<span>adipisicing</span>
<span>elit.</span>
</p>
应该在没有 em 标签的情况下工作,或者可以用另一个标签代替
Hi I have this code and each word is wrapped into span tag and I want to add styles only to the last two spans, even if <p>
tag has <em>
tag inside and it has <span>
tags the condition should be the same, the last two spans in a <p>
tag.
P.S. Without em tag it's working normally
.st span:nth-last-child(-n + 2) {
background-color: yellow;
}
<p class="st">
<span>With</span>
<span>ipsum</span>
<em>
<span>dolor</span>
<span>sit</span>
</em>
<span>amet,</span> consectetur adipisicing elit.
</p>
<p class="st">
<span>Lorem</span>
<span>ipsum</span>
<em>
<span>dolor</span>
<span>sit</span>
</em>
<span>amet,</span>
<span>consectetur</span>
<span>adipisicing</span>
<span>elit.</span>
</p>
Should work without em tag or maybe another tag can be instead
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
立即检查以
这种方式定位 CSS
https:// codesandbox.io/embed/ged-violet-5w8gyt?fontsize=14&hidenavigation=1&theme=dark
Check Now
target CSS This way
https://codesandbox.io/embed/aged-violet-5w8gyt?fontsize=14&hidenavigation=1&theme=dark
您的帖子不清楚
元素如何内部直接
p.st >应该选择 em
元素 - 我的答案假设您只想选择元素的最后一个兄弟对当且仅当< /strong> 这对兄弟姐妹也是其父代
p.st
的最后一个子代(不是后代)。在一般情况下,您所要求的(目前,截至 2022 年 2 月)不可能在 CSS(没有 JavaScript)中实现。
为了选择“最后 2 个
元素”,您需要一个可以同时选择这两个元素的选择器:
,并且前面还有另一个
。
元素,并且紧随其后的是另一个
。
情况 1 很简单:它只是
span + span:last-child:last-of-type { }
或span + span:nth-last-child(1) { }
.但是,情况 2 是不可能的:CSS 只有 2 个同级选择器(
x + y
和x ~ y
),并且这两个选择器都只能用于选择y
同级,不选择x
同级。:has()
伪元素函数,但只能与querySelector
/querySelectorAll
一起使用:它不能在 CSS 规则中使用。:has()
的受限形式,这是令人兴奋的东西,但不要屏住呼吸 - 我不希望看到它成为主流至少还要再等几年。:scope
关键字),您就可以使用:所以这是我们现在能做的最好的事情:
红色
和黄色
,以便您可以看到哪个规则专门选择哪个元素。但是,虽然我们无法在一般情况下解决这个问题,但有可能滥用
:not()
和其他选择器函数来阻止选择与其他模式匹配的元素。这种方法只有在您能够控制 HTML 并且知道 HTML 始终具有特定的结构或模式时才可行,在这种情况下我们可以这样做:Your post is unclear about how
<span>
elements inside an immediatep.st > em
element should be selected - my answer assumes you only want to select the last sibling pair of<span>
elements if and only if the sibling pair is also the last children (not descendants) of their parentp.st
.What you're asking is (currently, as of February 2022) impossible to achieve in CSS (without JavaScript) in the general case.
In order to select "the last 2
<span>
elements" you need a selector that can select both:<span>
that's also preceded by another<span>
.<span>
element that's also immediately followed by another<span>
.Case 1 is straightforward: it's just
span + span:last-child:last-of-type { }
orspan + span:nth-last-child(1) { }
.Case 2, however, is impossible: CSS only has 2 sibling selectors (
x + y
andx ~ y
), and both of which can only be used to select they
sibling, not thex
sibling.:has()
pseudo-element function which you can use today, but only withquerySelector
/querySelectorAll
: it cannot be used in CSS rules.:has()
into Chromium, and it's exciting stuff, but don't hold your breath - I'm not expecting to see it go mainstream for at least another couple of years.:scope
keyword) then you could use this:So this is the best we can do, right now:
red
andyellow
so you can see which rule is selecting which element specifically.However, while we cannot solve this in the general-case, it is possible to abuse
:not()
and other selector functions to prevent selecting elements that match other patterns. This approach is only feasible when you have control over the HTML and know that the HTML will always have a particular structure or pattern, in which case we can do this: