Issue
Are there any CSS selectors (CSS3+ is fine) that will apply certain styles to an element when there is only a single occurrence?
For example, the following CSS rule:
border: 1px solid black;
...would only apply to .info if there is only one occurrence of .info in the HTML.
So,
<p class="info">This would have a border because there is only one</p>
and,
<p class="info">This would not have a border because there are two</p>
<p class="info">And neither would this</p>
I'm guessing I'm going to have to either programatically apply an additional class such as: .single-occurrence or count the number of occurrences with Javascript?
Edit:
Let me just clear a few things up.
When I mention Javascript as a solution to what I'm trying to do - that does not mean I'm going to use it. I try to avoid JS for anything that is not behaviour. So I don't want a Javascript answer, that's incredibly easy to accomplish - my question is about CSS.
Also, to those getting confused: the reason why there would on occasion be only a single class on the page, is because the actual class I'm using is: search-result. Sometimes there would only be one result. But just because there's only one does not mean that the item cannot be part of the class of objects known as search-result. Semantically (and logically), of course there can be a class of one item. Sometimes, I think people should think a little more about semantics, instead of applying blanket rules.
Solution
Depending on the structure of your page, you might be able to use the :only-of-type
pseudo-class, which matches if the element has no siblings of the same tag name. I don't think there's a way to get more specific than that.
Answered By - Cheran Shunmugavel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.