简体   繁体   English

外部样式表是否会降低 in.css 文件覆盖以前的样式表?

[英]Do external stylesheets lower down in .css file override previous ones?

<link rel="stylesheet" href="css/css.css">
<link rel="stylesheet" href="css/css2.css">

imagine in the first stylesheet is says h1 color blue.想象在第一个样式表中说 h1 颜色为蓝色。 Imagine the second stylesheet says h1 color red.想象一下第二个样式表说 h1 color red。 Which one wins?哪一个赢了?

there exists an order for style sheets according to their priority根据优先级存在样式表的顺序

Browser Defaults
External Style Sheets (Linked or Imported)
Internal Style Sheets (Embedded)
Inline Styles

In any of the methods, if the same style names are used, the last priority is higher than all the same names在任何一种方法中,如果使用相同的样式名称,则最后的优先级高于所有相同的名称

I'm actually not sure, what happens if two css have the same name.我实际上不确定,如果两个 css 具有相同的名称会发生什么。 I would assume it doesn't matter.我会假设这没关系。

What does matter for sure is the order of css files.重要的是 css 文件的顺序。 Files, which are included later have a higher priority in case of same selectors.在相同选择器的情况下,稍后包含的文件具有更高的优先级。 So yes, the second one would win.所以是的,第二个会赢。

More important is even the selector.更重要的是选择器。 If a selector is more specific than another one, the more specific wins.如果一个选择器比另一个更具体,则更具体的获胜。 Eg body h1 is more specific than h1 and would win - even if included before the h1 .例如body h1h1更具体,并且会获胜 - 即使在h1之前包含。

The only way to go around that, is to find either a more specific selector - or the same specific selector if included later - or to use !important . go 的唯一方法是找到一个更具体的选择器 - 或者如果稍后包含相同的特定选择器 - 或者使用!important

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM