What 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
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
<div data-colour="green"></div>
[data-colour="green"] { /* 一些性质 */ }
3, [
Use the
<div data-colour="green yellow blue"></div>
[data-colour~="green"] { /* 一些性质 */ }
4, [
Use the
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
Note:
<div data-colour="greenish-yellow"></div> <div data-colour="greengoblin"></div>
[data-colour^="green"] { /* 一些性质 */ }
6, [
Substring matching selector. Use the
Note:
<div data-colour="yellowish-green"></div> <div data-colour="seagreen"></div>
[data-colour$="green"] { /* 一些性质 */ }
7, [
Substring matching selector. Use the
Note:
<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!

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

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.

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

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

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

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

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,


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools