CSS 中的样式优先级

本文通过实例演示了CSS中不同类型的样式优先级,包括内部样式表、外部样式表、ID选择器、Class选择器及内联样式的优先级关系。

心血来潮对比了一下CSS中样式优先级

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css 优先级</title>
    <!-- 外部样式表 External Style Sheet-->
    <link rel="stylesheet" href="style.css">
    <style>
        /*内部样式表Internal Style Sheet*/

        .testClass{color: blue;} /*低于行内样式style="color:red;"*/
        /*.testClass{color: blue !important;}  */ /*高于行内样式style="color:red;"*/

        #testId{color: yellow;}  /*高于class低于行内低于!improtant*/
    </style>
</head>
<body>
    <div class="testClass" style="color:red;"><!-- 内联样式-行内样式 -->
        测试Css中的Important优先级
    </div>
    <div id="testId" class="testClass">
        比较ID选择器和Class选择器优先级
    </div>
</body>
</html>

总结如下:
!important > 内联样式 > 内部样式表{ID选择器>Class选择器>标签选择器} > 外部样式表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值