CSS三种机制及优先级问题_html/css_WEB-ITnose
为了解决冲突,CSS利用三种机制来达到目的:继承、层叠和特指。
1.继承
CSS中很多可以继承的属性都和文本有关系,比如说字体啊,颜色啊,字号这些,不能继承的也有一些,比如说盒子模型里面外边距(margin)、内边距(padding)、边框(border)等。
对于能够继承的属性,比如说字号大小,我们一般有两种方法来写单位,一种是用像素(px),另一种是用百分比(例如60%),在利用百分比的时候要注意下,因为这个百分比会影响到它的后代,也就是说后代又是前者的60%,那这个后代的字体就是36%了。
2.层叠
层叠是CSS的核心机制,也是一种可以方便实用的改变CSS样式的方法。这里面说的层叠就是样式表中的层叠,在文本样式中可以一层一层叠加上去,这样做有一个好处是可以让浏览器面对很多个属性的时候可以通过层层筛选达到最终想要的那个值。
既然层叠这么好用,我们来说一下它有什么规则吧。
a.首先是要对每个元素和属性的声明
b.然后按照顺序和权重排序
c.按特指度排序
d.顺序决定权重
现在再来说一下各个来源样式的优先顺序:作者行内样式>作者嵌入式样式>作者连接样式>用户样式>浏览器默认样式
当然了,作者就是设计这个东西的人(就是你),用户就是读者,看这个东西的人,如果说用户是特殊人士,需要放大字号或者改变颜色,可以强制浏览器加载成想要的样式。
3.特指
一个简单的计分原则是,0.0.0.0,1.0.0.0比0.11.0.0大,排在前面的1比后面的100都管用。
第一个0那里是检验是否是内联元素,如果是,则置1
第二位是id位,有几个就加几
第三位是class位,有几个类、伪类和属性就加几
第四位是Element位,有几个元素就加几
下面是几个栗子:
a {} 特指度为0.0.0.1
.a{} 特指度为0.0.1.0
#a{} 特指度为0.1.0.0
.a.b c{} 特指度为0.0.2.1
#a b.c{} 特指度为0.1.1.1
头一天开博客,有点鸡冻。先到这里吧,欢迎留言讨论 ^ 。^

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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 <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

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

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

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

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
