How to Target Labels with Specific 'for' Attributes in CSS?

Mary-Kate Olsen
Release: 2024-11-13 05:47:02
Original
616 people have browsed it

How to Target Labels with Specific 'for' Attributes in CSS?

Selecting Labels by 'for' Attribute in CSS

To modify the layout of specific labels based on their 'for' attribute, CSS provides attribute selectors.

Attribute Selector for Label

The syntax for selecting a label by its 'for' attribute is:

label[for="XYZ"]
Copy after login

where "XYZ" represents the desired attribute value.

Usage

CSS:

label[for="email"]
{
    /* ...layout definitions here... */
}
Copy after login

JavaScript (DOM):

var element = document.querySelector("label[for=email]");
Copy after login

JavaScript (jQuery):

var element = $("label[for=email]");
Copy after login

Attribute Value Matching

If the attribute value contains characters not allowed in CSS identifiers (e.g., spaces, brackets), enclose it in single or double quotes:

label[for="field[]"]
{
    /* ...definitions here... */
}
Copy after login

Browser Compatibility

Note that attribute selectors may not be supported in older browsers like IE versions below 8. For cross-browser compatibility, consider using classes or other structural methods.

The above is the detailed content of How to Target Labels with Specific 'for' Attributes in CSS?. 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