CSS选择器

CSS选择器:

 

基本选择器:

选择器优先级:id选择器>类选择器>标签选择器

标签选择器:标签{}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css选择器-标签选择器</title>
    <style>
        h1{
            color: red;
        }
    </style>
</head>
<body>
<h1>我是h1标签</h1>
</body>
</html>

类选择器:.类{}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css选择器-类选择器</title>
    <style>
        .c1{
            color: red;
        }
    </style>
</head>
<body>
<h1 class="c1">我是h1标签</h1>
</body>
</html>

id选择器:#id{}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css选择器-id选择器</title>
    <style>
        #i1{
            color: red;
        }
    </style>
</head>
<body>
<h1 id="i1">我是h1标签</h1>
</body>
</html>

 

层次选择器:

后代选择器:元素 元素{}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css选择器-后代选择器</title>
    <style>
        body .c1{
            color: red;
        }
    </style>
</head>
<body>
<h1 class="c1">我是h1标签</h1>
</body>
</html>

子选择器:元素>元素{}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css选择器-子选择器</title>
    <style>
        body>h1{
            color: red;
        }
    </style>
</head>
<body>
<h1 class="c1">我是h1标签</h1>
<ul>
    <li>li1</li>
    <li>li2</li>
</ul>
</body>
</html>

相邻兄弟选择器(向下一个):元素 + 元素{}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css选择器-相邻兄弟选择器</title>
    <style>
        .li2 + li{
            color: red;
        }
    </style>
</head>
<body>
<h1 class="c1">我是h1标签</h1>
<ul>
    <li>li1</li>
    <li class="li2">li2</li>
    <li>li3</li>
</ul>
</body>
</html>

通用兄弟选择器(选择的元素下面所有的):元素 ~ 元素{}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css选择器-通用兄弟选择器</title>
    <style>
        .li2 ~ li{
            color: red;
        }
    </style>
</head>
<body>
<h1 class="c1">我是h1标签</h1>
<ul>
    <li>li1</li>
    <li class="li2">li2</li>
    <li>li3</li>
    <li>li4</li>
</ul>
</body>
</html>

 

  结构伪类选择器:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css选择器</title>
    <style>
        ul li:first-child{
            color: red;
        }
        ul li:last-child{
            color: blue;
        }
        /* p父元素的第一个子p元素 受类型干扰*/
        p:nth-child(1){
            color: yellow;
        }
        /* p父元素的第一个子p元素 不受类型干扰*/
        p:nth-of-type(1){
            color: palegoldenrod;
        }
    </style>
</head>
<body>
<h1 class="c1">我是h1标签</h1>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<ul>
    <li>li1</li>
    <li>li2</li>
    <li>li3</li>
    <li>li4</li>
</ul>
</body>
</html>

属性选择器(可以加正则表达式通配符):元素[ 属性]{}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css选择器</title>
    <style>
       li[id="id1"]{
           color: yellow;
       }
    </style>
</head>
<body>
<ul>
    <li id="id1">li1</li>
    <li>li2</li>
    <li>li3</li>
    <li>li4</li>
</ul>
</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值