Home > Web Front-end > CSS Tutorial > Can You Have Multiple :before Pseudo-Elements on One Element?

Can You Have Multiple :before Pseudo-Elements on One Element?

Barbara Streisand
Release: 2024-11-11 16:38:03
Original
729 people have browsed it

Can You Have Multiple :before Pseudo-Elements on One Element?

Multiple :before Pseudo-Elements

Can an element possess multiple :before pseudo-elements? This question arises when applying styles to the same element using jQuery, where only the latest style seems to take effect.

CSS2.1 Limitations

In CSS2.1, an element can only have one pseudo-element of a specific type at any given time. Thus, an element can have both :before and :after pseudo-elements, but not more than one of each.

Cascade Behavior

Multiple :before rules for the same element are cascaded and applied to a single :before pseudo-element. The rule with the highest precedence, typically the last one, prevails. In the provided example:

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

Only the content declaration in the last rule, "Now", will be applied. The remaining declarations are discarded, similar to normal element properties.

Combining Selectors

If multiple selectors match the same element and you desire to apply all their styles, generate additional CSS rules with combined selectors. For the given example, this would be .circle.now:before or .now.circle:before.

Legacy CSS3-Content Specification

The deprecated css3-content specification suggests a notation to insert multiple ::before and ::after pseudo-elements compatible with CSS2.1 cascade. However, this feature is obsolete and has not been implemented by any browser.

The above is the detailed content of Can You Have Multiple :before Pseudo-Elements on One Element?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template