sunshine
管理员
管理员
  • 最后登录2023-10-30
  • 发帖数170
  • 社区居民
阅读:7021回复:0

[css]在有多个css样式文件时 如有重复的class 优先级是如何决定的

楼主#
更多 发布于:2018-04-25 10:07
比如一个页面引用了2个css如 style1.css style2.css 而两个.css中都有一个

style1.css 中.sidebar 代码如下:

.sidebar{
   background-color: red;
   font-size:24px;
 }


style2.css 中


.sidebar 代码如下
.sidebar{
  background-color: green;
}

这种时候 class="sidebar" 是以哪个为准
这种优先级是以什么决定的


解:
当类名相同时,选择器的优先级是一样的,相同的样式属性,就以:后面的覆盖前面的样式(如上的background-color),使用最后设置的属性。不同的样式就直接作用元素了(如上的:font-size 不管先后顺序,都会应用)

如下:

<link rel="stylesheet" href="style2.css"/>
<link rel="stylesheet" href="style1.css"/>


style1.css 在style2.css后引用,background-color 就会应用style1.css中的样式。

不过,如果你在style2.css中,使用如下代码:

background-color: green !important;

那,不管引用谁在前,谁在后,都会使用background-color中的样式。

问题来源:
https://zhidao.baidu.com/question/527625402.html
游客


返回顶部

公众号

公众号