Home Web Front-end CSS Tutorial What is the attribute selector of css? Using attribute selectors

What is the attribute selector of css? Using attribute selectors

Nov 09, 2018 pm 03:49 PM
css3 attribute selector

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

css code selection:

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

2, [=]

Locate all elements whose attribute value is .

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

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>
Copy after login
[data-colour~="green"] { 
    /* 一些性质 */ 
}
Copy after login

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>
Copy after login
[data-colour|="green"] { 
    /* 一些性质 */ 
}
Copy after login

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>
Copy after login
[data-colour^="green"] { 
    /* 一些性质 */ 
}
Copy after login

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>
Copy after login
[data-colour$="green"] { 
    /* 一些性质 */ 
}
Copy after login

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>
Copy after login
[data-colour*="green"]{ 
    /* 一些性质 */ 
}
Copy after login

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

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"] { 
    /* 一些性质 */ 
}
Copy after login

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="
Copy after login

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

Video Face Swap

Video Face Swap

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

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)

How to achieve wave effect with pure CSS3? (code example) How to achieve wave effect with pure CSS3? (code example) Jun 28, 2022 pm 01:39 PM

How to achieve wave effect with pure CSS3? This article will introduce to you how to use SVG and CSS animation to create wave effects. I hope it will be helpful to you!

Use CSS skillfully to realize various strange-shaped buttons (with code) Use CSS skillfully to realize various strange-shaped buttons (with code) Jul 19, 2022 am 11:28 AM

This article will show you how to use CSS to easily realize various weird-shaped buttons that appear frequently. I hope it will be helpful to you!

How to hide elements in css without taking up space How to hide elements in css without taking up space Jun 01, 2022 pm 07:15 PM

Two methods: 1. Using the display attribute, just add the "display:none;" style to the element. 2. Use the position and top attributes to set the absolute positioning of the element to hide the element. Just add the "position:absolute;top:-9999px;" style to the element.

How to implement lace borders in css3 How to implement lace borders in css3 Sep 16, 2022 pm 07:11 PM

In CSS, you can use the border-image attribute to achieve a lace border. The border-image attribute can use images to create borders, that is, add a background image to the border. You only need to specify the background image as a lace style; the syntax "border-image: url (image path) offsets the image border width inward. Whether outset is repeated;".

It turns out that text carousel and image carousel can also be realized using pure CSS! It turns out that text carousel and image carousel can also be realized using pure CSS! Jun 10, 2022 pm 01:00 PM

How to create text carousel and image carousel? The first thing everyone thinks of is whether to use js. In fact, text carousel and image carousel can also be realized using pure CSS. Let’s take a look at the implementation method. I hope it will be helpful to everyone!

css3 what is adaptive layout css3 what is adaptive layout Jun 02, 2022 pm 12:05 PM

Adaptive layout, also known as "responsive layout", refers to a web page layout that can automatically recognize the screen width and make corresponding adjustments; such a web page can be compatible with multiple different terminals instead of making a specific version for each terminal. . Adaptive layout was born to solve the problem of mobile web browsing, and can provide a good user experience for users using different terminals.

Cleverly use CSS3 filters to create text flash switching animation effects! Cleverly use CSS3 filters to create text flash switching animation effects! Jul 20, 2022 am 10:55 AM

This article will show you how to use CSS3 filters to achieve a high-end text flash switching animation effect. I hope it will be helpful to you!

Elegantly use jQuery to find elements whose name attribute is not undefined Elegantly use jQuery to find elements whose name attribute is not undefined Feb 27, 2024 pm 01:42 PM

Title: Elegantly use jQuery to find elements whose name attribute is not undefined. When developing web pages, we often need to use jQuery to operate DOM elements, and we often need to find elements based on specific conditions. Sometimes we need to find elements with specific attributes, such as finding elements whose name attribute is not undefined. This article will introduce how to elegantly use jQuery to achieve this function, and attach specific code examples. First, let's take a look at how to use jQ

See all articles