Use of CSS attribute selector for Fuzzy Matching

Source: Internet
Author: User
Document directory
  • Difference between the partial score attribute selector and the point class name Recording Method
  •  
  • Substring matching attribute Selector

Conversion from: http://www.w3school.com.cn/css/css_selector_attribute.asp,?selector

Select based on partial attribute values

If you want to select a word from the word list in the attribute value, use the Tilde (~).

If you want to select an element that contains important in the class attribute, you can use the following selector to do this:

p[class~="important"] {color: red;}

Try it yourself

If the Tilde is ignored, a full value match is required.

Difference between the partial score attribute selector and the point class name Recording Method

This selector is equivalent to the Point class name method discussed in the class selector.

That is to say, P. Important and P ["important"] are equivalent when applied to HTML documents.

So why do we need "~ = "What about the attribute selector? Because it can be used for any attribute, not just a class.

For example, you can have a document containing a large number of images, only a part of which is an image. You can use a partial property Selector Based on the title document to select only the images:

img[title~="Figure"] {border: 1px solid gray;}

This rule Selects all images whose title text contains "figure. Images that do not have the title attribute or contain "figure" do not match.

Try it yourself

Note: The value here must be a complete word, such as the figure in the current position. If you use IMG [Title ~ = "Figu"] {border: 1px solid gray;} Then CSS becomes invalid, and some matching fails. Substring matching attribute Selector

The following describes a more advanced selector module, which was released after css2 was completed, and contains more partial score attribute selectors. According to the standard, it should be called the "substring matching attribute selector ".

Many modern browsers support these selectors, including ie7.

The following table provides a brief summary of these selectors:

Type Description
[ABC ^ = "def"] Select all elements whose ABC attribute value starts with "def"
[ABC $ = "def"] Select all elements whose ABC attribute values end with "def"
[ABC * = "def"] Select all elements in the ABC property value that contain the sub-string "def"

It can be imagined that these options have many purposes.

For example, if you want to apply a style to all links pointing to w3school, you do not have to specify a class for all these links and then write a style based on this class. Instead, you only need to write the following rules:

a[href*="w3school.com.cn"] {color: red;}
 
SIGHS: Powerful Fuzzy Matching of attributes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.