0
点赞
收藏
分享

微信扫一扫

css优先级

unadlib 2022-03-27 阅读 73
vue.jscssjs

CSS 优先规则1

最近的祖先样式比其他祖先样式优先级高。

<body>
    <div style="color: red;">
        <div style="color: yellow;">
            <p >我是p标签</p>
        </div>
    </div>
</body>

在这里插入图片描述

CSS 优先规则2:

直接样式"比"祖先样式"优先级高。

如果 <div> 有个属性 color:yellow,则这个属性将被 <p> 继承,即 <p> 也拥有属性 color: red

<body>
  <div style="color: yellow;">
     <p style="color: red;">我是p标签</p>
  </div>
</body>

在这里插入图片描述

选择器的优先级
ID 选择器, 如 #id{}

类选择器, 如 .class{}

属性选择器, 如 a[href=“segmentfault.com”]{}

伪类选择器, 如 :hover{}

伪元素选择器, 如 ::before{}

标签选择器, 如 span{}

通配选择器, 如 *{}

CSS 优先规则3

优先级关系:内联样式 > ID 选择器 > 类选择器 = 属性选择器 = 伪类选择器 > 标签选择器 = 伪元素选择器

属性选择器:就是通过属性来选择
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

详见:https://www.w3school.com.cn/css/css_attribute_selectors.asp
伪元素选择器
在这里插入图片描述
在这里插入图片描述

CSS 优先规则4

在这里插入图片描述

CSS 优先规则5

:属性后插有 !important 的属性拥有最高优先级

摘抄:https://www.runoob.com/w3cnote/css-style-priority.html

举报

相关推荐

0 条评论