CSS Attribute Selectors: Quotation Mark Quandary
Attribute selectors allow CSS authors to target elements based on their attributes. However, the presence or absence of quotation marks surrounding the attribute value has been a source of confusion.
According to the CSS spec, attribute values can be quoted using either single or double quotes, or can be unquoted entirely. The following examples illustrate these variants:
a[rel="nofollow"] a[rel='nofollow'] a[rel=nofollow]
Generally, unquoted attribute values are allowed if the value consists only of alphanumeric characters, hyphens (-), or underscores (_). However, certain special characters, such as spaces and periods, require quotation marks to be valid.
For instance, the following selector would not work without quotation marks:
a[href^=http://]
This is because the period character would be interpreted as a separator between the attribute and the value. Using quotation marks ensures that the period is recognized as part of the value:
a[href^="http://"]
As a best practice, it is advisable to use quotation marks around attribute values to avoid potential conflicts. This ensures that the selector will function as intended, even if the attribute value contains special characters.
The above is the detailed content of CSS Attribute Selectors: When Are Quotation Marks Necessary?. For more information, please follow other related articles on the PHP Chinese website!