Within CSS2.1 guidelines, an element is limited to only one of each type of pseudo-element at any given time. This restriction means that while an element can have both a :before
and an :after
pseudo-element, it cannot have multiple instances of the same kind.
When multiple :before
rules are applied to the same element, they will all merge together and impact a single :before
pseudo-element, similar to how styles are handled for regular elements. In practical terms, this results in only the declaration with the highest precedence taking effect, as illustrated in the following example:
.circle.now:before {
content: "Now";
font-size: 19px;
color: black;
}
In this case, only the content
property with the greatest specificity remains active, while the others are disregarded — consistent with standard CSS property behavior.
The specifics of this behavior are explained within the Selectors section of CSS2.1:
Pseudo-elements behave akin to actual elements within CSS, subject to some exceptions described in various sources.
This implies that selectors involving pseudo-elements follow the same patterns as those for normal elements, including cascading mechanisms. Notably, CSS2.1 appears to be the primary authority on this matter, as references in newer specifications like css3-selectors and css3-cascade do not address this issue, leaving room for potential reevaluation in future updates.
In situations where an element can satisfy multiple selectors featuring the same pseudo-element, additional CSS rules with combined selectors may be required to clarify browser behavior. While offering a complex example here would be challenging without context, crafting a rule that combines selectors such as .circle.now:before
or .now.circle:before
could aid in specifying desired outcomes, particularly regarding the content
property.
For further guidance or examples, referring to related queries such as the one mentioned here might provide additional insight.
An outdated specification from 2003 known as legacy css3-content outlines the use of multiple ::before
and ::after
pseudo-elements in alignment with CSS2.1 principles. However, the lack of recent updates and implementation progress renders it obsolete. Noteworthy efforts are being made through initiatives like css-content-3 and css-pseudo-4 to revisit these concepts, albeit without explicit inclusion of the multiple pseudo-elements feature, likely due to limited industry interest.