Home Web Front-end CSS Tutorial Detailed introduction to the new features of attribute selectors in CSS3

Detailed introduction to the new features of attribute selectors in CSS3

Mar 07, 2017 pm 03:08 PM

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;}
Copy after login

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;}
Copy after login

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;}
Copy after login

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};
Copy after login

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;}
Copy after login

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;}
Copy after login

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;}
Copy after login

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;}
Copy after login

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;}
Copy after login

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 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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles