What are the uses of CSS attribute selectors? Introduction to the usage of css attribute selector (code)

不言
Release: 2018-09-05 17:51:50
Original
2954 people have browsed it

What this article brings to you is what are the uses of CSS attribute selectors? The usage introduction (code) of CSS attribute selector has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Existence and value attribute selectors

1. Existence and value attribute selectors

        /*存在和值属性选择器*/   
        [attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何。
        [attr=val]:该选择器仅选择 attr 属性被赋值为 val 的所有元素。
        [attr~=val]:表示带有以 attr 命名的属性的元素,并且该属性是一个以空格作为分隔的值列表,其中至少一个值为val。
Copy after login

2. Code example:
01 _Existence and value attribute selector.html

<head>
        <meta charset="UTF-8">
        <title>存在和值属性选择器</title>
        <style type="text/css">
            /*
             [attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何。
            */
            [name]{                background: pink;            }
        </style>
    </head>
    <body>
        <div name="atguigu_llc">李立超</div>
        <div name="atguigu_xfz">晓飞张</div>
        <div name="atguigu_bhj">白浩杰</div>
        <div name="atguigu_sym">腿长1米8</div>
        <div>佟刚</div>
        <div>陈雷</div>
        <div>李贺飞</div>
    </body>
Copy after login

02_Existence and value attribute selector.html

<head>
        <meta charset="UTF-8">
        <title>存在和值属性选择器</title>
        <style type="text/css">
            /*
             [attr=val]:该选择器仅选择 attr 属性被赋值为 val 的所有元素。
            */
            [name="atguigu_llc"]{                background: pink;            }
        </style>
    </head>
    <body>
        <div name="atguigu_llc">李立超</div>
        <div name="atguigu_xfz">晓飞张</div>
        <div name="atguigu_bhj">白浩杰</div>
        <div name="atguigu_sym">腿长1米8</div>
        <div>佟刚</div>
        <div>陈雷</div>
        <div>李贺飞</div>
    </body>
Copy after login

03_Existence and value attribute selector.html

<head>
        <meta charset="UTF-8">
        <title>存在和值属性选择器</title>
        <style type="text/css">
            /*
             [attr~=val]:该选择器仅选择 attr 属性的值(以空格间隔出多个值)中有包含 val 值的所有元素,
                比如位于被空格分隔的多个类(class)中的一个类。
            */
            [name~="atguigu"]{                background: pink;            }
        </style>
    </head>
    <body>
        <div name="atguigu_llc atguigu">李立超</div>
        <div name="atguigu_xfz">晓飞张</div>
        <div name="atguigu_bhj atguigu">白浩杰</div>
        <div name="atguigu_sym">腿长1米8</div>
        <div>佟刚</div>
        <div>陈雷</div>
        <div>李贺飞</div>
    </body>
Copy after login

2. Substring value attribute selector

1. Substring value attribute selector

        /*子串值属性选择器*/
        [attr|=val] : 选择attr属性的值是val(包括val)或以val-开头的元素。
        [attr^=val] : 选择attr属性的值以val开头(包括val)的元素。
        [attr$=val] : 选择attr属性的值以val结尾(包括val)的元素。
        [attr*=val] : 选择attr属性的值中包含字符串val的元素。
Copy after login

2. Code example:

<head>
        <meta charset="UTF-8">
        <title>存在和值属性选择器</title>
        <style type="text/css">
            /*
             [attr|=val] : 选择attr属性的值以val(包括val)或val-开头的元素
             [attr^=val] : 选择attr属性的值以val开头(包括val)的元素。
             [attr$=val] : 选择attr属性的值以val结尾(包括val)的元素。
             [attr*=val] : 选择attr属性的值中包含字符串val的元素。
            */
            [name^="atguigu"]{                background: pink;            }
        </style>
    </head>
    <body>
        <div name="atguigu-llc atguigu">李立超</div>
        <div name="atguigu-xfz">晓飞张</div>
        <div name="atguigu-bhj atguigu">白浩杰</div>
        <div name="atguigu_sym">腿长1米8</div>
        <div>佟刚</div>
        <div>陈雷</div>
        <div>李贺飞</div>
    </body>
Copy after login

Related recommendations:

CSS3 Selector Attribute Selector

Advanced usage of CSS attribute selector and selector priority issues_html/css_WEB-ITnose

The above is the detailed content of What are the uses of CSS attribute selectors? Introduction to the usage of css attribute selector (code). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template