css - 在一个元素中使用多个类和特异性

标签 css css-specificity

只是想知道您何时在一个元素上使用多个类,例如 class="foo bar" 并且这些类的设置如下:

.foo {
    margin-right: 10px;
}


.bar {
    margin-right: 0px;
}

哪个类会有特殊性? margin 是 10px 还是 0px?

最佳答案

它基于 CSS 中的优先级工作。因此,最近出现的元素将覆盖任何以前的样式。

案例 1

.foo  { background : red; }
.bar  { background : blue; }

class = 'foo bar' 在此实例中为蓝色

案例 2

.bar  { background : blue; }
.foo  { background : red; } 

class = 'foo bar' 在此实例中将是红色

Working Example

关于css - 在一个元素中使用多个类和特异性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8451522/

相关文章:

CSS:放置绝对定位元素,以便它从外部接触其父元素

css - 为什么在使用 grid-auto-flow 时在 CSS Grid 中添加了额外的列?

html - 谷歌浏览器 : Text will not minimize correctly

CSS 选择器层次结构?

html - Joomla 模块 CSS 覆盖

伪类和伪元素在特异性计算中的 CSS 区别?

css - 为什么(禁用)颜色为 :inherit not follow disabled style? 的按钮

css - 这两个元素 (<li>) 之间的随机额外空间是什么?

html - CSS 样式优先级未按预期工作

javascript - 问题获取菜单滑动