Is It Ok to Use Multiple Pseudo-Elements in CSS

Can I have multiple :before pseudo-elements for the same element?

In CSS2.1, an element can only have at most one of any kind of pseudo-element at any time. (This means an element can have both a :before and an :after pseudo-element — it just cannot have more than one of each kind.)

As a result, when you have multiple :before rules matching the same element, they will all cascade and apply to a single :before pseudo-element, as with a normal element. In your example, the end result looks like this:

.circle.now:before {
content: "Now";
font-size: 19px;
color: black;
}

As you can see, only the content declaration that has highest precedence (as mentioned, the one that comes last) will take effect — the rest of the declarations are discarded, as is the case with any other CSS property.

This behavior is described in the Selectors section of CSS2.1:

Pseudo-elements behave just like real elements in CSS with the exceptions described below and elsewhere.

This implies that selectors with pseudo-elements work just like selectors for normal elements. It also means the cascade should work the same way. Strangely, CSS2.1 appears to be the only reference; neither css3-selectors nor css3-cascade mention this at all, and it remains to be seen whether it will be clarified in a future specification.

If an element can match more than one selector with the same pseudo-element, and you want all of them to apply somehow, you will need to create additional CSS rules with combined selectors so that you can specify exactly what the browser should do in those cases. I can't provide a complete example including the content property here, since it's not clear for instance whether the symbol or the text should come first. But the selector you need for this combined rule is either .circle.now:before or .now.circle:before — whichever selector you choose is personal preference as both selectors are equivalent, it's only the value of the content property that you will need to define yourself.

If you still need a concrete example, see my answer to this similar question.

The legacy css3-content specification contains a section on inserting multiple ::before and ::after pseudo-elements using a notation that's compatible with the CSS2.1 cascade, but note that that particular document is obsolete — it hasn't been updated since 2003, and no one has implemented that feature in the past decade. The good news is that the abandoned document is actively undergoing a rewrite in the guise of css-content-3 and css-pseudo-4. The bad news is that the multiple pseudo-elements feature is nowhere to be found in either specification, presumably owing, again, to lack of implementer interest.

Is it ok to use multiple pseudo-elements in css?

Sure you can - http://jsfiddle.net/WQBxk/

p:before {
content: "BEFORE ";
display: block;
}

p:first-child:before {
content: "1ST";
display: block
}

The bad - it won't work in IE7 and below. Not because of the multiple pseudo selectors, but because of non-supported :before - http://kimblim.dk/css-tests/selectors/

Just tested in IE8 - works well.

CSS multiple pseudo classes and pseudo elements

Yes it works, see the snippet below.

footer ul.footer-menu li:not(:first-child):after {    content: 'added some text';    color: red;}
<footer>    <ul class="footer-menu">        <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut tempora voluptatum praesentium rem culpa, cupiditate quas animi commodi voluptates? Cupiditate error cum suscipit dolorum deleniti? Non dolore, cumque assumenda voluptatum.</li>        <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut tempora voluptatum praesentium rem culpa, cupiditate quas animi commodi voluptates? Cupiditate error cum suscipit dolorum deleniti? Non dolore, cumque assumenda voluptatum.</li>        <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut tempora voluptatum praesentium rem culpa, cupiditate quas animi commodi voluptates? Cupiditate error cum suscipit dolorum deleniti? Non dolore, cumque assumenda voluptatum.</li>        <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut tempora voluptatum praesentium rem culpa, cupiditate quas animi commodi voluptates? Cupiditate error cum suscipit dolorum deleniti? Non dolore, cumque assumenda voluptatum.</li>    </ul></footer>

After-Before pseudo in CSS3 for multiple classes

Consider this HTML structure, where you have a <div> which have children <span> and <p>. And another <span> and <p> as siblings.

<div>
<span>abc</span>
<p>xyz</p>
</div>
<span>123</span>
<p>456</p>

For example, if we need to change the colour of the children, we could write on your way,

div span, p{
color: red;
}

This problem with this is that, it will change the colour of the sibling <p>456</p> too as the style is applied globally to all the paragraph tags.

And the solution is to follow specificity as we did with the <span> and write the selectors as

div span, 
div p{
color: red;
}

The same rule applies to pseudo-elements as well. Hence the solution is,

[class*="divclass-"]::before, 
[class*="divclass-"]::after{
border-radius: 50%;
width: 30%;
height : 30%;
border: 3px solid red;
}

Note 1

If you are working on SASS, your syntax could be,

[class*="divclass-"]{
&::before,
&::after{
border-radius: 50%;
width: 30%;
height : 30%;
border: 3px solid red;
}
}

Note 2

The before and after pesudo-elements require the content property.

Hope this helps.

Css pseudo-element ::before(2); :before and ::before

The CSS spec on content describes all three syntaxes.

  • :before -- outdated syntax for pseudo elements. Use if older browser support is needed such as IE8. IE9 supports the new syntax. It also seems like iOS Safari does not support the new syntax
  • ::before -- new pseudo element syntax. This is equivalent to ::before(1)
  • ::before(n) -- used to create multiple before elements that can be before other ::befores. Details are in the same spec.
  • As far as I can tell, no browser supports this.
    • http://jsfiddle.net/535Rf/

What's the right way of combining CSS multiple pseudo-elements?

All selectors which should share the same properties and values can simply be comma separated. You can write them all on one line though a more preferred style is to put each one its own line to aid readability:

#black:target::before, #red:target::before { background: #ACAA92; }

#black:hover .text,
#com:hover .text {
display:block;
}

https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Combinators_and_multiple_selectors#Groups_of_selectors_on_one_rule

Why can't I combine pseudo-element selectors?

You are combining multiple vendor-specific selectors into a single CSS rule.

This means that if one of the selectors is not recognised by the browser, the entire CSS block is ignored. In this particular case, Chrome does not recognize ::-moz-range-track, because it is specific to Firefox/Gecko. This is not a quirk, but intended behaviour and part of the CSS standard.

The solution would be to split the declarations. Like so:

.range2::-webkit-slider-runnable-track {
height: 6px;
border-radius: 3px;
border: 1px solid black;
}
.range2::-moz-range-track {
height: 6px;
border-radius: 3px;
border: 1px solid black;
}

Updated CodePen

Can't add multiple pseudo-classes to a :not() pseudo-class

Multiple classes selectors to :not is only supported in CSS selectors level 4 and it doesn't work in all browsers. It currently only works in Safari, instead you could use multiple :not

.div-list div:not(:last-child):not(:first-child) {
border:1px solid blue;
}

Check the browser support doc



Related Topics



Leave a reply



Submit