简体   繁体   English

是否可以选择容器中的第一个元素,否则纯文本而不使用纯CSS中的类或标识符?

[英]Is it possible to select the very first element within a container that's otherwise pure text without using classes or identifiers in pure CSS?

This is a purely hypothetical situation. 这纯粹是假设的情况。 When answering a question yesterday I came across a problem which I hadn't encountered before. 昨天回答问题时,我遇到了一个我之前没有遇到过的问题。 The question itself was pretty flawed in that you simply wouldn't do what it was trying to achieve, but this problem may pop up in other situations. 这个问题本身存在很大缺陷,因为你根本不会做它想要实现的目标,但这个问题可能会在其他情况下出现。 I wasn't able to give a working answer that didn't involve changing the markup, and I'd like to know if it's at all possible without changing the markup. 我无法给出一个不涉及更改标记的工作答案,我想知道是否可以在更改标记的情况下完成。

Take this example: 举个例子:

<p><strong>Lorem</strong> ipsum dolor <strong>sit</strong> amet...</p>

To style the first strong element you'd simply use p > strong:first-child or something similar. 要设置第一个strong元素的样式,您只需使用p > strong:first-child或类似的东西。 That's great and works well, but what if the first strong was optional and it was only the first strong that would require styling? 这很棒并且效果很好,但是如果第一个strong是可选的并且它只是第一个需要造型的strong呢?

<p>Lorem ipsum dolor <strong>sit</strong> amet...</p>

Is it possible to ensure that only the very first strong tag here would be styled without using classes, identifiers or anything that could differentiate the two strong tags through the markup? 是否有可能确保只使用这里的第一个strong标签, 而不使用类,标识符或任何可以通过标记区分两个strong标签的东西?

Here's a JSFiddle of the above . 这是上面JSFiddle

Note: from the initial answers the question may have been a little misleading. 注意:从最初的答案来看,这个问题可能有点误导。 The paragraph doesn't have to be the first on the page, so p:first-child isn't a solution. 该段落不必是页面上的第一段,因此p:first-child不是解决方案。 The first paragraph may not have this leading strong element - alternatively the second paragraph could also have this leading strong element. 第一段可能没有这个strong元素 - 或者第二段也可能具有这一领先的strong元素。

Edit: I've updated the JSFiddle example to give a little more clarity of what should and shouldn't be styled. 编辑:我已经更新了JSFiddle示例,以便更清楚地说明应该和不应该设置哪些样式。

Unfortunately, it's not possible. 不幸的是,这是不可能的。

The problem is that CSS selects only element nodes , and text nodes are not element nodes. 问题是CSS只选择元素节点 ,而文本节点不是元素节点。 So, as far as CSS is concerned, <p>blah <b>blah</b> blah <b>blah</b> blah</p> might as well be <p><b></b><b></b></p> . 所以,就CSS而言, <p>blah <b>blah</b> blah <b>blah</b> blah</p>也可能是<p><b></b><b></b></p>

In other words, your strong tag is the first element inside its parent regardless of whether there's text in front of it, even though it's not the first node when there's text in front. 换句话说,您的强标记是其父级内部的第一个元素 ,无论其前面是否有文本,即使它不是前面有文本的第一个节点

也许,这就是你想要做的:

p:first-child > strong:first-child

How about using * for any container, which has strong as its first element should be in different color: 如何使用*作为第一个元素强大的任何容器应该是不同的颜色:

* > strong:first-child { color:#f30; }

Or like this: 或者像这样:

 * > p > strong:first-child { color:#f30; }

EDIT - as pointed out in the comments, this solution falls apart pretty quickly. 编辑 - 正如评论中指出的那样,这种解决方案很快就会崩溃。 There isn't a CSS-only way to achieve this that I can see. 我无法用CSS实现这一目标。

I've come up with a pure CSS solution . 我想出了一个纯CSS解决方案 Here's the CSS (based on your amended example): 这是CSS(基于您修改的示例):

p > strong { color:blue; }
p > strong:first-child { color:red; }
p > strong:nth-last-child(-n+1) { color:blue; }

Which renders: 哪个呈现:

最后一个孩子的例子

This makes use of the :nth-last-child selector, which is supported in all good browsers (and IE9+). 这使用了:nth-​​last-child选择器,所有好的浏览器(和IE9 +)都支持它。 That selector: 那个选择器:

…matches an element that has an+b-1 siblings after it in the document tree, for a given positive or zero value for n, and has a parent element ...匹配在文档树中具有+ b-1兄弟的元素,对于n的给定正值或零值,并且具有父元素

The order of those selectors is important because the specificity is the same for each, so we need to be over-riding the previous targeted strong 's with each subsequent selector. 这些选择器的顺序很重要,因为每个选择器的特异性是相同的,所以我们需要在每个后续选择器上覆盖先前的目标strong

I have tried a lot more but I could not find any method that your need would be fulfilled, but anyway I have added a trick for your fiddle. 我已经尝试了很多,但我找不到任何方法可以满足你的需要,但无论如何我为你的小提琴添加了一个技巧。 See this fiddle just add <span></span> inside to your first <strong> element and define your css... is this Ok for you? 看到这个小提琴只需将<span></span>添加到您的第一个<strong>元素中并定义您的CSS ... 这对您有用吗?

Edit Or you could define a blank strong inside your div <strong></strong> to make this first element. 编辑或者您可以在div <strong></strong>定义一个空格,以构建第一个元素。

我想这就是你要找的......

p strong:first-child { ... }

看看这可能会解决你的问题: http//jsfiddle.net/RNpsG/1/

body > p:first-child > strong:first-child { color:#f30; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM