很多时候一个元素的样式并非直接对元素定义,而是对其祖先元素定义,然后通过继承性得到样式。例如,在body中定义的文字的颜色(color)属性,在p元素中也是有效的。这就是样式的继承。
如果一个元素在多个地方定义了某个样式属性,最后哪个起作用呢?
例如:在body中定义了字体的大小,元素p的属性style中定义了文字颜色,内部样式表和外部样式表中也都定义了元素p的文字颜色,而且某段文字还用属性id和类名都定义了文字颜色。这样会导致复杂的样式层叠关系,到底谁可以起作用呢?
如果一个元素被具有相同属性名的多个样式重复作用,也就是出现了样式层叠,CSS需要通过这些样式的选择器的特殊性来进行选择其中的一个样式。选择器越特殊优先权越高,选择器优先权最高的样式被选中。如果最高优先权的有多个,则要看这些样式中哪个样式离这个元素最近,离得最近的被选中。
选择器的特殊性可以根据下面的规则来确定:
(1)对于id选择器,每个特殊性加0100;
(2)对于类选择器,属性选择器,伪类选择器,每个特殊性加0010;
(3)对于标签选择器和伪元素选择器,每个特殊性加0001;
(4)每个元素只能定义一个行内样式,行内样式的特殊性为1000;
(5)对于结合符和通用选择器,它对特殊性没有贡献,其特殊性为0000;
(6)对于继承得来的选择器没有任何特殊性,其特殊性为0000;
(7)加上!important的样式具有最高的优先权。
优先权比较:
0100 > 0020;
0021 > 0020;
特殊性计算例子

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment
