W3C文档 5 . selector 选择器,自己的学习笔记,欢迎指正

选择器

通配符选择器

元素选择器(type selectors)

后代选择器

用空格分隔

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./text.css">
</head>

<body>
    <ul>
        <li>111
            <h1>111</h1>
            <h1>111
                <p>我是个p</p>
            </h1>
        </li>
        <li>111</li>
        <li>111</li>
        <li>111<h1>111</h1>
        </li>
        <h1>111</h1>
        <h1>111</h1>
        <h1>111</h1>
    </ul>
</body>

</html>
li  h1 {
    color: red;
}
li  p {
    color: pink;
}

在这里插入图片描述

子选择器

用大于号来链接

后代选择器可以跨多个级别

子选择器只能选择他的亲孩子

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./text.css">
</head>

<body>
    <ul>
        <li>111
            <h1>被选中</h1>
            <h1>被选中
                <p>我是p
                    <---这里p会变颜色是因为继承->
                    <a href="#">aaa
                    </a>
                </p>
            </h1>
        </li>
        <li>111</li>
        <li>111</li>
        <li>111<h1>被选中</h1>
        </li>
        <h1>111</h1>
        <h1>111</h1>
        <h1>111</h1>
    </ul>
</body>

</html>
li > h1 {
    color: red;
}
li > a {
    color: pink;/*没有被选中了*/
}

在这里插入图片描述

相邻兄弟选择器

语法是e1 + e2

  • e2是被选择的
  • 如果e1是e2他俩父级是一个标签,并且e1在e2前面
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./text.css">
</head>

<body>
    <ul>
        <li>111</li>
        <li>111</li>
        <li>111</li>
        <li>111<h1>111</h1>
        </li>
        <h1>被选中</h1>
        <li>111</li>
        <h1>被选中</h1>
        <h1>111</h1>
        <h1>111</h1>
    </ul>
</body>

</html>
li + h1 {
    color: red;
}

在这里插入图片描述

属性选择器

  1. [att] <--选择所有的带这个属性的元素-->
    
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="./text.css">
    </head>
    
    <body>
        <a href="">aaa</a>
    </body>
    
    </html>
    
    [href] {
        color: red;
    }
    
  2. [att=val]

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="./text.css">
    </head>
    
    <body>
        <a href="www.baidu.com">aaa</a>
        <a href="#" class="example">aaa</a>
    </body>
    
    </html>
    
    [href] {
        color: red;
    }
    [class = example] {
        color: pink;
    }
    

在这里插入图片描述

  1. [att~=val]

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="./text.css">
    </head>
    
    <body>
        <a href="www.baidu.com">aaa</a>
        <a href="#" class="example text">aaa</a>
    </body>
    
    </html>
    
    [href] {
        color: red;
    }
    [class ~= example]{
        color: pink;
    }
    

在这里插入图片描述

  1. [att|=val] 暂时还不懂

类选择器

.cls

id选择器

伪元素和伪类选择器

伪类选择器

:first-child pseudo-class
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./text.css">
</head>

<body>
    <div>
        <p>The :first-child pseudo-class matches an element that is the first child element of some other element.

            In the following example, the selector matches any P element that is the first child of a DIV element. The
            rule suppresses indentation for the first paragraph of a DIV:</p>
        <p>第二段:The :first-child pseudo-class matches an element that is the first child element of some other
            element.

            In the following example, the selector matches any P element that is the first child of a DIV element.
            The rule suppresses indentation for the first paragraph of a DIV:</p>
    </div>
</body>

</html>
div > p:first-child {
    color: red;
}/*只有第一段变成红色了*/

这样的话,第二个p就不会被选中了

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./text.css">
</head>

<body>
    <P> The last P before the note.
        <DIV class="note">
            <H2>Note</H2>
            <P> The first P inside the note.
        </DIV>
</body>

</html>
div > p:first-child {
    color: red;
}

例子3

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./text.css">
</head>

<body>
    <P>abc <EM>default</EM></P>
</body>

</html>
p:first-child em {
    font-weight: bold
}
/*
p em:first-child {
    font-weight: bold
}
这两个效果一样
*/
The link pseudo-classes: :link and :visited
  1. a:link表示未被访问的链接;a:visited表示已访问的链接

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="./text.css">
    </head>
    
    <body>
        <A class="external" href="http://out.side/">external link</A>
    </body>
    
    </html>
    
    
    a.external:link {
        color: red
    }
    a.external:visited {
        color: pink;
    }
    
    
The dynamic pseudo-classes: :hover, :active, and :focus
  1. a:hover 鼠标悬停上面的
a.external:link {
    color: red
}
a.external:visited {
    color: pink;
}
a:hover {
    background-color: #484848;
}


  1. a:active选择活动的链接
a.external:link {
    color: red
}
a.external:visited {
    color: pink;
}
a:active {
    background-color: #484848;
}


3.:lang

<!DOCTYPE html>
<html>
<head>
<style>
p:lang(en)
{ 
background:yellow;
}
</style>
</head>

<body>

<p>我是唐老鸭。</p>
<p lang="en">I live in Duckburg.</p>

<p><b>注释:</b>对于在 IE8 中工作的 :lang,必须声明 DOCTYPE。</p>

</body>
</html>


伪元素

:first-line
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./text.css">
</head>

<style>
    p:first-line {
        text-transform: uppercase
    }
</style>

<body>
    <P>This is a somewhat long HTML
        paragraph that will be broken into several
        lines. The first line will be identified
        by a fictional tag sequence. The other lines
        will be treated as ordinary lines in the
        paragraph.</P>
</body>

</html>

:first-letter

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./text.css">
</head>

<style>
    p {
        line-height: 1.1
    }

    p:first-letter {
        font-size: 3em;
        font-weight: normal
    }

    span {
        font-weight: bold
    }
</style>

<body>
    <p><span>Het hemelsche</span> gerecht heeft zich ten lange lesten<br>
        Erbarremt over my en mijn benaeuwde vesten<br>
        En arme burgery, en op mijn volcx gebed<br>
        En dagelix geschrey de bange stad ontzet.

</body>

</html>

在这里插入图片描述

The :before and :after pseudo-elements

会在选择标签后面插入内容

<!DOCTYPE html>
<html>
<head>s
<meta charset="utf-8"> 
<title>菜鸟教程(runoob.com)</title> 
<style>
p:after
{ 
content:"- 注意我";
}
</style>
</head>

<body>
<p>我的名字是 Donald</p>
<p>我住在 Ducksburg</p>

<p><b>注意:</b> :after在IE8中运行,必须声明 !DOCTYPE </p>

</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值