Despite the convenience of using comma-separated selectors to apply the same styles to multiple elements, this approach cannot be applied to vendor-specific pseudo-elements and classes. This inconsistency arises from a fundamental rule outlined in CSS2.1.
The CSS2.1 specification states that any selector that cannot be parsed by the user agent must be ignored along with its corresponding declaration block. This applies to unrecognized vendor prefixes in pseudo-class and pseudo-element selectors.
Since different browsers use different prefixes, certain user agents will be unable to parse pseudo-elements and classes with unrecognized prefixes. Consequently, these browsers must drop any rules containing those unrecognized prefixes, leading to the need for repetitive declarations.
Consider the following code snippet, which aims to style placeholder text using vendor-specific selectors:
input:-moz-placeholder { font-style: italic; text-align: right; } input::-moz-placeholder { font-style: italic; text-align: right; } input:-ms-input-placeholder { font-style: italic; text-align: right; } input::-webkit-input-placeholder { font-style: italic; text-align: right; }
An attempt to combine these rules using commas would result in:
input:-moz-placeholder, input::-moz-placeholder, input:-ms-input-placeholder, input::-webkit-input-placeholder { font-style: italic; text-align: right; }
However, this combined rule set will fail because:
Due to browser parsing limitations, it is not possible to combine vendor-specific pseudo-elements and classes into a single coma-separated selector. This leads to the necessity of repetitive declarations when styling elements across different browsers.
The above is the detailed content of Why Can't I Combine Vendor-Specific Pseudo-elements and Classes in CSS Using Comma Separated Selectors?. For more information, please follow other related articles on the PHP Chinese website!