css 各类选择器优先性

1 篇文章 0 订阅

网上看到很多关于选择器优先级问题。

主要存疑在 伪类选择器 类选择器 属性选择器

于是做了如下实验;

得出结果

!important > 行内样式 > ID选择器 > 伪类选择器 > 类选择器 > 标签 > 属性选择器 > 通配符 > 继承 > 浏览器默认属性

 

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

<head>
    <meta charset="UTF-8">
    <title>伪类选择器</title>
    <style type="text/css">
        h4 {
            color: blue !important;
        }
        * {
            margin: 0;
            padding: 0;
            color: orange;
        }

        #text {
            color: red;
        }

        [title] {
            color: brown;
        }

        div {
            color: blue;
        }

        p:nth-child(4),
        p:nth-child(5) {
            color: aqua;
        }

        .text {
            color: green;
        }


    </style>
</head>

<body>
    <h1> !important > 行内样式 > ID选择器 > 伪类选择器 > 类选择器 > 标签 > 属性选择器 > 通配符 > 继承 > 浏览器默认属性</h1>
    <h4 style="color:gray">
        important(blue) > 行内
    </h4>
    <p id="text" style="color:gray">
        行内(gray) > id
    </p>
    <p id="text">
        id(red) > 伪类选择器aqua </p>

    <p class="text">
        伪类选择器aqua> class
    </p>
    <p class="text" title="1">
        类选择器green > 属性选择器brown
    </p>
    <div title="1">
        属性选择器brown > 标签
    </div>
</body>

</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值