This article is a mirror article of machine translation, please click here to jump to the original article.

View: 12439|Reply: 1

[CSS/DIV] Analyze the usage of the four major CSS attribute value selectors

[Copy link]
Posted on 10/15/2014 9:53:14 PM | | |

This article will describe it to youCSS property value selectorThe format of the attribute selector is element followed by parentheses, with attributes in parentheses, or attribute expressions (I don't know if the description is correct, self-created words), such as h1[title], h1[title="logo"], etc., I believe you will learn a lot in this article.

CSS Tutorial: CSS Attribute Value Selector

CSS AttributeSelectors, perhaps you should not be unfamiliar with attribute selectors, in essence, id and class selectors are actually attribute value selectors, just select the id or class value.

The format of the attribute selector is element followed by parentheses, with attributes in parentheses, or attribute expressions (I don't know if the description is correct, self-created words), such as h1[title], h1[title="Logo"], etc., you can see 4 specific forms from my discussion below.

1. Simple CSS attribute value selector

Regardless of the name, this is the characteristic of the simple attribute selector.

h1[class]{color:silver; will act on any h1 element with class, regardless of the value of the class. Therefore<h1class="hoopla">Hello</h1>, <h1class="severe">Serenity</h1>, <h1class="fancy"> h1 of Fooling</h1> will be affected by this rule.

Of course, this "property" is not just a class or id, but can be all legitimate properties of the element, such as img's alt, such as img[alt]{cssdeclarationshere; will apply to any img element with the alt attribute. Then a[href][title]{font-weight:bold; What about }? As you know, this works on elements with both href and title attributes, such as <atitle="W3CHome">W3C</a>.

2. Precise CSS attribute value selector

id and class are essentially exact attribute value selectors, yes, h1#logo is equal to h1[id="logo"]. As mentioned earlier, we don't limit ourselves to id or class, we can use any property! For example, a[][title="W3CHome"]{font-size:200%; } will be applied to <atitle="W3CHome">W3C</a>.

3. Some CSS attribute value selectors

As the name suggests, as long as the attribute value partially matches (in this case, the part that actually matches the entire word) will act on the element. Let's look at an example:

  1. <pclasspclass="urgentwarning">
  2. Whenhandlingplutonium,  
  3. caremustbetakentoavoidtheformationofacriticalmass.</p>
  4. p[class~="warning"]{font-weight:bold;}  
  5. 和p[class~="urgent"]{font-weight:bold;}
Copy code

Any of these can make the font of the p bold.

This selector is very useful, for example, if you want to style an illustration with the string "Figure" in its title, e.g. title="Figure5:xxx description", you can use img[title~="Figure"].

It should be noted that as I emphasized in the first sentence, you need to match the whole word, and img[title~="Figure"] will not match title="Figure5:xxx description".

Also, I did a little test, you change "Figure" to "Illustration" in the example, and change img[title~="Figure"] to img[title~="Illustration"], and it still matches in Firefox, regardless of whether the encoding is GB2312 or UTF-8. It seems that CSS support for Chinese is not bad.

4. Special CSS attribute value selector

Kind of weird, this selector. It works like this, well, it's easier to give an example than to describe it.

*[lang|="en"]{color:white; This rule will select the value of the attribute lang or the element that starts with en-. That is, it can match <h1lang="en">Hello!</h1>, <plang="en-us">Greetings!</p>and <divlang="en-au">G'day!</div>and does not match<plang="fr">Bonjour!</p>and <h3lang="cy-en">Jrooana!</h3>.






Previous:Perfectly solves CSS page horizontal centering
Next:A piece of Javascrip{filtering}t code to let others open the web page and shut it down within 3 minutes!
Posted on 12/21/2023 10:22:38 AM |
Prompt:Authors are banned or removed content is automatically blocked
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com