我的编程之路

CSS选择器介绍

2019-03-22  本文已影响0人  依风伴雨

写给正在学习选择器的小伙伴们!

不懂的可以加我联系方式,一起讨论!

QQ:951434130

序选择器很重要,学习时记得不懂的查MDN,或者W3C官方文档,因为序选择器有些我的理解也不是很对

标签选择器

  1. 根据指定标签名称,在当界面中找到指定标签的所以标签,然后设置属性
  2. 无论标签藏得多深都能找到选中
  3. 不能单独选中某个标签,如果使用标签选择器,那样同名标签都会被设值,例如p标签,当使用标签选择器时,当前页面所有p标签都会被设值
 <style>
       p{
          color: green;
       }
    </style>

id选择器

<style>
       #id1{
          color: green;
       }
    </style>

注意点:

  1. 每个HTML标签都有一个属性叫做id, 也就是说每个标签都可以设置id
  2. 在同一个界面中id的名称是不可以重复的
  3. 在编写id选择器时一定要在id名称前面加上#
  4. id的名称是有一定的规范的
  5. 1 id的名称只能由字母/数字/下划线a-z 0-9 _
  6. 2 id名称不能以数字开头
  7. 3 id名称不能是HTML标签的名称不能是a h1 img input ...
  8. 在企业开发中一般情况下如果仅仅是为了设置样式, 我们不会使用id ,因为在前端开发中id是留给js使用的

以上这段注意点是来自我老师 李南江的解答!

类选择器

<style>
       .header{
          color: green;
       }
    </style>

注意点:
1.每个HTML标签都有一个属性叫做class, 也就是说每个标签都可以设置类名
2.在同一个界面中class的名称是可以重复的
3.在编写class选择器时一定要在class名称前面加上.
4.类名的命名规范和id名称的命名规范一样
5.类名就是专门用来给CSS设置样式的
6.在HTML中每个标签可以同时绑定多个类名
格式:<标签名称 class="类名1 类名2 ...">
错误写法:<p class="para1" class="para2">

以上这段注意点是来自我老师 李南江的解答!

交集选择器

交集选择题的注意点?

1 .选择器和选择器之间没有任何的连接符号
2 .选择器可以使用标签名称/id名称/class名称
3 .交集选择器仅仅作为了解, 企业开发中用的并不多
4 .交集选择器是给所有选择器选中的标签中, 相交的那部分标签设置属性

<style>
        .para1#identity{
            color: blue;
        }
</style>

并集选择器

什么是并集选择器?

1 .给所有选择器选中的标签设置属性
2 .并集选择器必须使用,来连接
3 .选择器可以使用标签名称/id名称/class名称

<style>
        .ht,.para{
            color: red;
        }
</style>

兄弟选择器

相邻兄弟选择器 CSS2

1 .给指定选择器后面紧跟的那个选择器选中的标签设置属性
2 .相邻兄弟选择器必须通过+连接
3 .相邻兄弟选择器只能选中紧跟其后的那个标签, 不能选中被隔开的标签

<style>
     h1+p{
            color: red;
        }
</style>
通用兄弟选择器 CSS3
给指定选择器标签后面的所有选择器标签选中设置属性

1 .通用兄弟选择器必须用~连接
2 .通用兄弟选择器选中的是指定选择器后面某个选择器选中的所有标签, 无论有没有被隔开都可以选中

<style>
      h1~p{
            color: red;
        }
</style>

序选择器

:first-child
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>序选择器</title>
    <style>
        p:first-child{
            color: red;
        }
    </style>
</head>
<body>
<p>我是段落1</p>
<p>我是段落2</p>
<div>
    <p>我是段落3</p>
    <p>我是段落2</p>
    <p>我是段落4</p>
    <p>我是段落5</p>
</div>
</body>
</html>
:last-child
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>序选择器</title>
    <style>
        p:last-child{
            color: red;
        }
    </style>
</head>
<body>
<p>我是段落1</p>
<p>我是段落2</p>
<div>
    <p>我是段落3</p>
    <p>我是段落4</p>
</div>
</body>
</html>
:nth-child(n)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>序选择器</title>
    <style>
        p:nth-child(3){
            color: red;
        }
    </style>
</head>
<body>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
</body>
</html>
:nth-last-child(n)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>序选择器</title>
    <style>
        p:nth-last-child(3){
            color: #8d28ff;
        }
    </style>
</head>
<body>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
</body>
</html>
:only-child
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:only-child{
            color: #8d28ff;
        }
      
    </style>
</head>
<body>
    <p>我是段落</p>
    <p>我是段落</p>

    <div>
        <p>我是段落</p>
    </div>

</body>
</html>
:first-of-type
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:first-of-type{
            color: #8d28ff;
        }

    </style>
</head>
<body>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>


    <div class="header">
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
    </div>

</body>
</html>
:last-of-type
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:last-of-type{
            color: #8d28ff;
        }

    </style>
</head>
<body>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>


    <div class="header">
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
    </div>

</body>
</html>
:nth-of-type(n)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:nth-of-type(3){
            color: #8d28ff;
        }

    </style>
</head>
<body>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>


    <div class="header">
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
    </div>

</body>
</html>
:nth-last-of-type(n)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:nth-last-of-type(3){
            color: #8d28ff;
        }

    </style>
</head>
<body>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>


    <div class="header">
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
        <p>我是段落</p>
    </div>

</body>
</html>
:only-of-type
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:only-of-type{
            color: #8d28ff;
        }
    </style>
</head>
<body>
    <p>我是段落</p>

    <div class="header">
        <p>我是段落</p>
    </div>

</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:only-of-type{
            color: #8d28ff;
        }
    </style>
</head>
<body>
    <p>我是段落</p>

    <div class="header">
        <p>我是段落</p>
        <p>我是段落</p>
    </div>

</body>
</html>
:nth-child(xn+y)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:nth-child(2n+0){
            color: #8d28ff;
        }
    </style>
</head>
<body>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
</body>
</html>
:nth-child(even)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:nth-child(even){
            color: #8d28ff;
        }
    </style>
</head>
<body>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
</body>
</html>
:nth-child(odd)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p:nth-child(odd){
            color: #8d28ff;
        }
    </style>
</head>
<body>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
<p>我是项目</p>
</body>
</html>

属性选择器

[attribute]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        p[id]{
            color: red;
        }

    </style>
</head>
<body>
    <p id="id1">我是段落</p>
    <p id="id2">我是段落</p>
    <p>我是段落</p>
    <p id="id3">我是段落</p>
    <p>我是段落</p>
    <p id="id4">我是段落</p>
    <p>我是段落</p>
</body>
</html>
[attribute=value]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       input[type=password]{
           border:2px solid;
           border-radius:25px;
             }
    </style>
</head>
<body>

    <input type="password">

</body>
</html>
属性的取值是以什么开头的
[attribute|=value] CSS2
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       img[alt|=abc]{
            color: red;
        }
    </style>
</head>
<body>

<img src="" alt="abcdef">
<img src="" alt="abc-www-www">
<img src="" alt="abc ppp">

</body>
</html>
[attribute^=value] CSS3
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       img[alt^=abc]{
            color: red;
        }
    </style>
</head>
<body>
<img src="" alt="abcdef">
<img src="" alt="abc-www-www">
<img src="" alt="abc ppp">
<img src="" alt="defabc">
<img src="" alt="ppp abc">
<img src="" alt="www-abc">
</body>
</html>
属性的取值是以什么结尾的
[attribute$=value] CSS3
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
      img[alt$=abc]{
            color: skyblue;
        }
    </style>
</head>
<body>

    <img src="" alt="abcdef">
    <img src="" alt="abc-www-www">
    <img src="" alt="abc ppp">
    <img src="" alt="defabc">
    <img src="" alt="ppp abc">
    <img src="" alt="www-abc">

</body>
</html>

属性的取值是否包含某个特定的值得

[attribute~=value] CSS2
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
     img[alt~=abc]{
            color: skyblue;
        }
    </style>
</head>
<body>

    <img src="" alt="abcdef">
    <img src="" alt="abc-www-www">
    <img src="" alt="abc ppp">
    <img src="" alt="defabc">
    <img src="" alt="ppp abc">
    <img src="" alt="www-abc">

</body>
</html>
[attribute*=value] CSS3
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
     img[alt*=abc]{
            color: skyblue;
        }
    </style>
</head>
<body>

    <img src="" alt="abcdef">
    <img src="" alt="abc-www-www">
    <img src="" alt="abc ppp">
    <img src="" alt="defabc">
    <img src="" alt="ppp abc">
    <img src="" alt="www-abc">

</body>
</html>

通配符选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       *{
          color: deeppink;
       }
    </style>
</head>
<body>
    <p>我是段落</p>
    <p>我是段落</p>
    <p>我是段落</p>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读