目录

CSS样式覆盖规则

CSS三大特性

The cascade

What selectors win out in the cascade depends on three factors(these are listed in order of weight---earlier ones will overrule later ones):

  1. Importance

  2. Specificity

  3. Source order(资源顺序)

Importance

In CSS, there is a special piece of syntax you can use to make sure that a certain rule will win over all others: !important. Adding this to the end of a property value will give it superowers.

Let's look at example:

<p class="better">This is a paragraph.</p>
<p class="better" id="winning">One selector to rule them all!</p>
#winning {
background-color: red;
border: 1px solid black;
} .better {
background-color: gray;
border: none !important;
} p {
background-color: blue;
color: white;
padding: 5px;
}

This produces the following:

This is a paragraph.(backgound: gray;color: white;padding: 5px;border: none;)

One selector to rule them all!(background: red;color: white;padding:5px;border: none;)

Let's walk through this to see what's happening.

  1. You'll see that the third rule's color and padding values have been applied, but the background-color hasn't. why? Really all three should surely apply, because rules later in the source order generally override earlier rules.(因为资源顺序后面的规则通常会继承前面的规则。)

  2. However, The rules above it win, because ID/class selectors have higher specificity than element selectors(You will learn way more about this in the next section.)(ID/class选择器比元素选择器有更高的特异性.)

  3. Both elements have a class of better, but the second one has an id of winning too.Since IDs have an even higher specificity than classes(you can only have one ID on a page, but many classes --- ID selectors are very specific in what they target), the red background color and 1 pixel black border should both be applied to the second element, with first element getting the gray background color, and no border, as specified by the class.(ID选择器比class选择器有更高的特异性。)

  4. The second element does get the red background color, but no border.Why? Because of the !important declaration in the second rule --- including this after border;none means that this declaration will win over the border value in the previous rule, even though the ID has high specifity.(即使ID有更高的特异性,但是因为!important的存在。!important赢了。)

Note: The only way to override this !important declaration would be to include another !important declaration of the same specificity, later in the source order.

We would advertise you to never use !important unless you absolutely have to.

Conflicting declarations will be applied in the following order, with later ones overriding earlier ones:

  1. Declarations in user agent style sheets(e.g. the browser's default styles, used when no other styling is set.)(用户代理样式表声明, 比如浏览器默认样式)

  2. Normal declarations in user style sheets(custom styles set by a user.) (用户样式表普通声明, 用户定制的样式)

  3. Normal declarations in author style sheets(these are the styles set by us, the web developers!)(开发者的普通声明)

  4. Important declarations in author style sheets(开发者的important声明)

  5. Important declarations in user style sheets(用户的important声明)

Specificity

The amount of specificity a selector has is based on something called the selector weight. This has four different values, which can be thought of as thousands, hundreds, tens and ones --- four single digits in four columns.The selector weight is calculated based on the selector types of its sub-parts:

  1. Thousands: One if the matching selector is inside a < style> element or the declaration is inside a style attribute(such declarations do not have selectors, so their specificity is always simple 1000.) Otherwise 0.

  2. Hundreds: One for each ID selector contained inside the overall(全部的) selector.

  3. Tens: One for each class selector, attribute selector or pseudo-class contained inside the overall selector.

  4. Ones: one for each element selector and pseudo-element contained inside the overall selector.

Note: Universal selector( * ), combinators(+, >, ~, '') and nagation pseudo-class(:not) have no effect on specificity.

Source order

As mentioned above, if multiple competing selectors have the same importance and specificity, the third factor that comes into play to help decide which rule wins is source order ---late rules will win over earlier rules.

A note on rule mixing

One thing you should bear in mind when considering all this cascade theroy, and what styles get applied over other styles, is that all this happens at the property level --- properties override other properties, but you don't get entire rules overriding other rules. When several CSS rules match the same element, they are all applied to that element. Only after that are any conflicting properties evaluated to see which individual styles will win over others.(当几个CSS规则匹配同样的元素时,它们都将被用于元素。只有当它们是相互冲突的属性时,它们才会被判断出哪一个单独的样式会将其他的所有样式打败。)

Inheritance

The idea is that some property values applied to an element will be inherited by that element's children, and some won't.

  1. For example, it makes sense for font-family and color to be inherited.

  2. As other example, it makes sense for margin, padding, border and background-image to NOT be inherited.

Controlling inheritance

CSS provides three special values to handle inheritance.

  1. inherite: This value sets the property value applied to a selected element to be the same as that of its parent element.

  2. initial: This value sets the property value applied to a selected element to be the same as the value set for that element in the browser's default style sheet. If no value is set by the browser's default style sheet and the property is naturally, then the property value is set to inherit instead.(属性被设置为浏览器的默认样式,如果浏览器没有默认样式,并且该属性是自然继承的,那么属性值将被设置为inherit)

  3. unset: This value resets the property to its natural value, which means that if the property is natually inherited it acts like inherit, otherwise it acts like initial.(要么是inherite, 要么是initial)

引申:a元素为什么不继承父元素的字体颜色。

阅读MDN文档之层叠与继承(二)的更多相关文章

  1. 阅读MDN文档之CSS选择器介绍(一)

    本文为阅读MDN文档笔记 目录 Different types of Selectors Attribute Selectors Presence and value attribute select ...

  2. 阅读MDN文档之StylingBoxes(五)

    目录 BoxModelRecap Box properties Overflow Background clip Background origin Outline Advanced box prop ...

  3. 阅读MDN文档之布局(四)

    Introducing positioning Static positioning Relative positioning Introducing top, bottom, left and ri ...

  4. 阅读MDN文档之布局(四)

    Introducing positioning Static positioning Relative positioning Introducing top, bottom, left and ri ...

  5. 阅读MDN文档之基本盒模型(三)

    Box properties Margin collapsing Adjacent siblings(相邻兄弟) Parent and first/last child Empty blocks Ac ...

  6. Emacs阅读chm文档

    .title { text-align: center; margin-bottom: .2em } .subtitle { text-align: center; font-size: medium ...

  7. 前端开发必备之MDN文档

    想下载MDN文档的看前面的内容就可以了. HTML 源码下载 MDN官方下载地址:https://developer.mozilla.org/media/developer.mozilla.org.t ...

  8. MDN 文档高级操作进阶教程

    MDN 文档高级操作进阶教程 MDN 文档, 如何优雅的使用 MDN 文档上的富文本编辑器 pre & 语法高亮器 code & note box source code 上传附件 i ...

  9. 关于MDN,HTML入门来自MDN文档

    由开发者和作者组成的开源社区:推动web发展, MDN(Mozilla Developer Network) 维基,共同维护做贡献: 需要使用到github账号进行验证,以此再创建MDN账号: HTM ...

随机推荐

  1. 4.2 例题: 统计字符数 poj2247

    问题描述 判断一个由 a-z 这 26 个字符组成的字符串中哪个字符出现的次数最多 输入:第 1 行是测试数据的组数 n,每组测试数据占 1 行,是一个由 a-z 这 26 个字符组 成的字符串,每组 ...

  2. km算法入门

    本文知识均由笔者自学,文章有错误之处请不吝指出. 笔者刷数模题的时候有一道题考到了"二分图最大权分配",需要用到KM算法,但是书上对KM算法的介绍又臭又长,更何况有些同学" ...

  3. 在ThinkPHP中使用常量解决路由常规地址不安全传送数据问题

    在ThinkPHP搭建项目的同时,会考虑到后期对静态页面的维护问题, 在项目的不断完善的同时,会有大量图片,css文件,以及js文件等一些容易修改.添加.或者删除的资源 如果在中后期对各个静态页面,j ...

  4. 利用vertical-align:middle垂直居中

    以前总是以为vertical-align与text-align是同样的道理,一个是垂直居中,一个是水平居中,结果在这里一点效果也没有.事实上vertical-align与text-align完全不一样 ...

  5. 实践作业1:测试管理工具实践 Day2

    1.尝试配置TestLink所需环境 安装配置php+apache+mysql时遇到一系列稀奇古怪的错误. 2.百度之后发现有可行的替代工具:Vertrigoserv(VertrigoServ是一个W ...

  6. UWP 保存Image的图片到本地文件

    上一篇说显示一张图片到Image控件,比较简单. 那个假设我Image控件有图片了,想保存到本地,这个就要花心思了,不过也不复杂的... var rtb = new RenderTargetBitma ...

  7. 4、C#基础 - C# 的 常见概念简述

    在上篇文章中,你跟着我写了一个HelloWorld,本篇中,我们来谈谈一些C#程序中的小概念 1.C# 程序结构 一个 C# 程序主要包括以下部分: 命名空间声明(Namespace declarat ...

  8. Qt---自定义界面之QStyle

    最近想学习下Qt的自定义界面,因此花了点时间看了下QStyle,,,,结果很难受,这一块涉及到一大块GUI的具体实现方式,看得我很头疼.想看第一手资料并且英语功底不错的可以直接上qt文档,下面我会以易 ...

  9. 过渡与动画 - steps调速函数&CSS值与单位之ch

    写在前面 上一篇中我们熟悉五种内置的缓动曲线和(三次)贝塞尔曲线,并且基于此完成了缓动效果. 但是如果我们想要实现逐帧动画,基于贝塞尔曲线的调速函数就显得有些无能为力了,因为我们并不需要帧与帧之间的过 ...

  10. Tomcat (安装及web实现 基础)

     Tomcat服务器配置 安装:解压对应的版本就行 注意;不要把Tomcat放到有中文的和有空格的目录中 验证是否安装成功:进入安装盘的安装文件的bin目录下 执行startup bat 成功  80 ...