search
HomeWeb Front-endCSS TutorialDetailed introduction to the new features of attribute selectors in CSS3

Zero, Overview

The attribute selector supported by CSS2 uses an expression [{attribute | attribute {= | |= | ~=} value}]
1.[class="a"] can only match elements with class="a"
2.[class~="a"] can match elements with class="a" and class="a b"
3.[lang|=en] can match the elements of lang="en" and lang="en-us".

CSS3 adds three new matching methods of *=, ^= and $= [{attribute | attribute {*= | ^= | $=} value}]:
1.*= indicates fuzzy matching , [href="163"] can match href="163.com", href="mail.163.com" and other elements;
2.^= means starting with the specified character, [href^="/" ] matches href="/a/a.htm", href="/b" elements
3.$= means ending with the specified character, [scr$=".png"] matches all png images, For example, src="logo.png"

CSS3 attribute selectors mainly include the following types:
1.E[attr]: only uses the attribute name, but does not determine any attribute value;
2.E[attr="value"]: Specify the attribute name, and specify the attribute value of the attribute;
3.E[attr~="value"]: Specify the attribute name, and have the attribute value. This attribute The value is a word list separated by spaces. The word list contains a value word, and the "?" in front of the equal sign must be written;
4.E[attr^="value"]: Specify The attribute name is specified, and there is an attribute value, and the attribute value starts with value;
5.E[attr$="value"]: The attribute name is specified, and there is an attribute value, and the attribute value ends with value. ;
6.E[attr*="value"]: The attribute name is specified, and there is an attribute value, and the attribute value contains value;
7.E[attr|="value"]: Specified The attribute name is specified, and the attribute value is value or a value starting with "value-" (for example, zh-cn);

1. E[attr]:The attribute selector is CSS3 The simplest type of attribute selector. If you want to select elements with a certain attribute, regardless of what the attribute value is, you can use this attribute selector:

.demo a[id] {background: blue; color:yellow;font-weight:bold;}

You can also use multiple attributes to select elements, For example, E[attr1][attr2], so that all elements with both attributes will be selected:

.demo a[href][title] {background: yellow; color:green;}

Note: IE6 does not support this selector.

2. E[attr="value"]: Specifies the attribute value "value"

.demo a[id="first"] {background: blue; color:yellow;font-weight:bold;}

Note: The attribute and attribute value must be Exact match, especially when the attribute value is in the form of a word list, such as:
test

.demo a[class="links"]{color:red};   
.demo a[class="links item"]{color:red};

IE6 does not support this selector.

3. E[attr~="value"]: If you want to select elements based on a word in the word list in the attribute value, then you need to use this attribute selection Selector: E[attr~="value"], this kind of attribute selector is that the attribute value is a list of one or more words. If it is a list, they need to be separated by spaces, as long as one of the attribute values ​​matches. The element can be selected. The E[attr="value"] mentioned earlier means that the attribute value needs to match completely before it can be selected. The difference between them is that one has a "?" sign and the other does not have a "?" sign.

.demo a[title~="website"]{background:orange;color:green;}

Note: When there is a wave (?) in the attribute selector, the attribute value will be matched. When there is no wave (?), the attribute value will be matched only when it is completely value. IE6 does not support the E[attr~="value"] attribute selector.

4. E[attr^="value"]: Select all elements whose attr attribute value starts with "value". In other words, the selected attribute has the corresponding attribute value. It starts with "value".

.demo a[href^="http://"]{background:orange;color:green;}

Note: IE6 does not support the E[attr^="value"] selector.

5. E[attr$="value"]: E[attr$="value"] attribute selector is just the opposite of E[attr^="value"] selector , E[attr$="value"] means to select all elements whose attr attribute value ends with "value". In other words, it selects the attr attribute of the element, and its attribute value ends with value. This application is used for It is very convenient for you to add background images to some special links. For example, to add different icons to different files such as pdf, png, doc, etc., we can use this attribute to achieve it.

.demo a[href$="png"]{background:orange;color:green;}

Note: IE6 does not support the E[attr$="value"] attribute selector.

6. E[attr*="value"]: Select all elements whose attr attribute value contains the substring "value". That is to say, as long as the attribute you select has this "value" value in its attribute value, it will be selected.

.demo a[title*="site"]{background:black;color:white;}

Note: IE6 does not support the E[attr*="value"] selector.

7. E[attr|="value"]: is called a specific attribute selector. This selector will select all elements whose attr attribute value is equal to value or starts with value-.

.demo a[lang|="zh"]{background:gray;color:yellow;}

Note: It is often used to match languages. IE6 does not support the E[attr|="value"] selector.

Note:
1. Except for IE6, which does not support the attribute selector, other browsers can support it.
2.E[attr="value"] and E[attr*="value"] are the most practical, among which E[attr="value"] can help us locate different types of elements, especially forms. Element operations, such as input[type="text"], input[type="checkbox"], etc., and E[attr*="value"] can help us match different types of files in the website, such as your Links to different file types on your website need to use different icons to help your website improve user experience. Just like the previous example, you can use this attribute to ".doc", ".pdf", ".png" ",".ppt" configure different icons.

For more detailed introductions to the newly added features of attribute selectors in CSS3, please pay attention to the PHP Chinese website for related articles!

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
Simulating Mouse MovementSimulating Mouse MovementApr 22, 2025 am 11:45 AM

If you've ever had to display an interactive animation during a live talk or a class, then you may know that it's not always easy to interact with your slides

Powering Search With Astro Actions and Fuse.jsPowering Search With Astro Actions and Fuse.jsApr 22, 2025 am 11:41 AM

With Astro, we can generate most of our site during our build, but have a small bit of server-side code that can handle search functionality using something like Fuse.js. In this demo, we’ll use Fuse to search through a set of personal “bookmarks” th

Undefined: The Third Boolean ValueUndefined: The Third Boolean ValueApr 22, 2025 am 11:38 AM

I wanted to implement a notification message in one of my projects, similar to what you’d see in Google Docs while a document is saving. In other words, a

In Defense of the Ternary StatementIn Defense of the Ternary StatementApr 22, 2025 am 11:25 AM

Some months ago I was on Hacker News (as one does) and I ran across a (now deleted) article about not using if statements. If you’re new to this idea (like I

Using the Web Speech API for Multilingual TranslationsUsing the Web Speech API for Multilingual TranslationsApr 22, 2025 am 11:23 AM

Since the early days of science fiction, we have fantasized about machines that talk to us. Today it is commonplace. Even so, the technology for making

Jetpack Gutenberg BlocksJetpack Gutenberg BlocksApr 22, 2025 am 11:20 AM

I remember when Gutenberg was released into core, because I was at WordCamp US that day. A number of months have gone by now, so I imagine more and more of us

Creating a Reusable Pagination Component in VueCreating a Reusable Pagination Component in VueApr 22, 2025 am 11:17 AM

The idea behind most of web applications is to fetch data from the database and present it to the user in the best possible way. When we deal with data there

Using 'box shadows' and clip-path togetherUsing 'box shadows' and clip-path togetherApr 22, 2025 am 11:13 AM

Let's do a little step-by-step of a situation where you can't quite do what seems to make sense, but you can still get it done with CSS trickery. In this

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

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.