CSS样式表以什么顺序覆盖? [英] In which order do CSS stylesheets override?

查看:125
本文介绍了CSS样式表以什么顺序覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTML标题中,我有:

In an HTML header, I've got this:

<head>
<title>Title</title>
<link href="styles.css" rel="stylesheet" type="text/css"/>
<link href="master.css" rel="stylesheet" type="text/css"/>

styles.css 是我的页面特定的工作表。 master.css 是我在我的每个项目中使用的表,以覆盖浏览器默认值。以下哪些样式表优先?
示例:第一个表包含特定

styles.css is my page-specific sheet. master.css is a sheet I use on each of my projects to override browser defaults. Which of these stylesheets takes priority? Example: first sheet contains specific

body { margin:10px; }

和相关边框,但第二个包含我的重设

And associated borders, but the second contains my resets of

html, body:not(input="button") {
    margin: 0px;
    padding: 0px;
    border: 0px;
}

实际上,CSS的级联元素在样式表引用,因为它在典型的CSS功能?

In essence, does the cascading element of CSS work the same in terms of stylesheet references as it does in typical CSS functions? Meaning that the last line is the one displayed?

推荐答案

CSS规则级联的规则很复杂,而不是试图改写他们很糟糕,我会简单地引用你的规格:

The rules for CSS rule cascading are complex -- rather than trying to paraphrase them badly, I'll simply refer you to the spec:

http://www.w3.org/TR/2011/REC-CSS2-20110607/cascade.html#cascade

简而言之:更具体的规则覆盖更一般的规则。特定性是基于涉及多少ID,类和元素名称以及是否使用了!important 声明来定义的。当存在具有相同特异性水平的多个规则时,无论哪一个出现最后一次。

In short: more specific rules override more general ones. Specificity is defined based on how many IDs, classes, and element names are involved, as well as whether the !important declaration was used. When multiple rules of the same "specificity level" exist, whichever one appears last wins.

这篇关于CSS样式表以什么顺序覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆