search
HomeWeb Front-endCSS TutorialWhat is the attribute selector of css? Using attribute selectors

What is the attribute selector of css? This article will talk to you about the CSS attribute selector, so that you can understand what the attribute selector does and how to use it. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is the css attribute selector? What is the use?

Elements in HTML can have attributes, which are additional values ​​that display or modify their behavior. HTML contains many attributes, but not all attributes are applicable to all HTML elements. Properties unrelated to the element have no effect on it.

Regardless of whether the property is applied correctly, you can still select it via CSS. However, using invalid HTML attributes anywhere on your website is a very bad practice because different browsers interpret invalid HTML differently. You can't blame the browser for making your site look weird, they're just trying to explain your error code by filling in the gaps.

However, the CSS attribute selector allows us to select elements with specific attributes or specific value attributes, that is: we can find the corresponding tag according to the specified attribute name, and then set the attribute. [Related video tutorials recommended: css3 tutorial]

Usage of css attribute selector

Attribute values ​​in html Must be an identifier or a string. The specification is rather vague, as it states that case sensitivity of property names and values ​​in selectors depends on the document language. Due to the fact that browsers behave inconsistently, the safest thing to do is to ensure that the cases you use are consistent between CSS and HTML.

Attribute selectors can be matched in 7 ways (starting from the CSS3 specification):

1, []

Position all An element containing the attribute, regardless of its value.

Example:

<div data-colour="green"></div>
<div data-colour="blue"></div>
<div data-colour="yellow"></div>

css code selection:

[data-colour] { 
   /* 一些性质,例:color..... */ 
}

2, [=]

Locate all elements whose attribute value is .

<div data-colour="green"></div>
[data-colour="green"] { 
    /* 一些性质 */ 
}

3, [~=]

Use the attribute to locate the element, its value is a space-separated list of words , one of which must be .

itself cannot contain spaces or be an empty string.

<div data-colour="green yellow blue"></div>
[data-colour~="green"] { 
    /* 一些性质 */ 
}

4, [|=]

Use the attribute to locate an element whose value is exactly or Begins with , followed by "-".

Note: Mainly used for language subcode matching, such as "en", "en-US" and "en-UK".

<div data-colour="green-table"></div>
<div data-colour="green-chair"></div>
<div data-colour="green-bottle"></div>
[data-colour|="green"] { 
    /* 一些性质 */ 
}

5, [^=]

Substring matching selector. Use the attribute to locate elements whose values ​​begin with .

Note: cannot be an empty string.

<div data-colour="greenish-yellow"></div>
<div data-colour="greengoblin"></div>
[data-colour^="green"] { 
    /* 一些性质 */ 
}

6, [$=]

Substring matching selector. Use the attribute to locate elements whose values ​​end with .

Note: cannot be an empty string.

<div data-colour="yellowish-green"></div>
<div data-colour="seagreen"></div>
[data-colour$="green"] { 
    /* 一些性质 */ 
}

7, [*=]

Substring matching selector. Use the attribute to locate elements whose value contains an instance of .

Note: cannot be an empty string.

<div data-colour="goblingreenish"></div>
[data-colour*="green"]{ 
    /* 一些性质 */ 
}

Combined attribute selectors

Attribute selectors have the same specific level as classes and pseudo-classes; you can combine attribute selectors with Other selectors such as element, class or ID are combined together. Example:

div[data-colour="green"] {
  /* 特异性为11 */
}
.swatch[data-colour="green"] {
  /* 特异性为20 */
}
#tile25[data-colour="green"] {
  /*特异性为110 */
}

You can also combine multiple attribute selectors to match specific patterns. For example, if you only wanted to target 2x images with alt text containing the word "green", your selector would look like this:

img[srcset~="2x"][alt*="green"] { 
    /* 一些性质 */ 
}

Additionally, since the attribute value is read as a string, You don't have to worry about escaping special characters to make them match, unlike with classes or IDs. This means we are free to have the following:

[data-emotion="

The above is the detailed content of What is the attribute selector of css? Using attribute selectors. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools