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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



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!

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!

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.

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;".

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!

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.

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!

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
