2css选择器的优先级-css选择器世界

html-css04

2css选择器的优先级-css选择器世界,第1张

css优先级有6级。

0级:通配选择器、选择符和逻辑组合伪类。

通配选择器是 * 。

选择符有+、-、~、空格和 || 。

逻辑组合伪类有 :not()、:is()和 :where。这些伪类影响优先级的是括号里的选择器。

eg: :not([rel=nofollow])

1级:标签选择器。

2级:类选择器、属性选择器和伪类。

3级:ID选择器

4级:内联选择器

5级:!important

(1)数值计算法

0级--优先级为0;

1级--优先级为1;

2级--优先级为10;

3级--优先级为100。

(2)后来居上

优先级数值相同情况下,越后面优先级越高。

1.增加css选择器优先级的方法

(1)重复选择选择器自身

.foo.foo {}

(2)借助必然会存在的属性选择器

.foo[class] {}

#foo[id] {}

除了这两种方法,常用的是增加父级嵌套和增加标签选择器,但这种方法增加了耦合,降低了可维护性,不建议使用。

2.数值计算法的点评

不同级别的选择器优先级差距并不是10比1,差距比这大得多,而是因为实际开发中不会连续写10个而已。

在ie浏览器下,256个类选择器的优先级大于id选择器的优先级,原因是类名是以8字节存储的,8字节所能容纳的最大值就是255.

除了ie外,其他都使用16进制,不会出现这种情况。

了解css选择器的优先级后,日常工作就知道是怎么回事了。

是因为默认的优先级高于设置的优先级,所以显示不正确。

继承得到的样式的优先级是最低的;最近的祖先样式比其他祖先样式优先级高;"直接样式"比"祖先样式"优先级高。

1、无继承性的属性

①  display:规定元素应该生成的框的类型

②  文本属性:

vertical-align:垂直文本对齐

text-decoration:规定添加到文本的装饰

text-shadow:文本阴影效果

white-space:空白符的处理

unicode-bidi:设置文本的方向

③  盒子模型的属性:width、height、margin 、margin-top、margin-right、margin-bottom、margin-left、border、 border-style、border-top-style、border-right-style、border-bottom-style、border-left-style、border-width、border-top-width、border-right-right、border-bottom-width、border-left-width、border-color、border-top-color、border-right-color、border-bottom-color、border-left-color、border-top、border-right、border-bottom、border-left、padding、padding-top、padding-right、padding-bottom、padding-left

④  背景属性:background、background-color、background-image、background-repeat、background-position、background-attachment

⑤  定位属性:float、clear、position、top、right、bottom、left、min-width、min-height、max-width、max-height、overflow、clip、z-index

⑥  生成内容属性:content、counter-reset、counter-increment

⑦  轮廓样式属性:outline-style、outline-width、outline-color、outline

⑧  页面样式属性:size、page-break-before、page-break-after

⑨  声音样式属性:pause-before、pause-after、pause、cue-before、cue-after、cue、play-during

2、有继承性的属性

①  字体系列属性

font:组合字体

font-family:规定元素的字体系列

font-weight:设置字体的粗细

font-size:设置字体的尺寸

font-style:定义字体的风格

font-variant:设置小型大写字母的字体显示文本,这意味着所有的小写字母均会被转换为大写,但是所有使用小型大写 字体的字母与其余文本相比,其字体尺寸更小。

font-stretch:对当前的 font-family 进行伸缩变形。所有主流浏览器都不支持。

font-size-adjust:为某个元素规定一个 aspect 值,这样就可以保持首选字体的 x-height。

② 文本系列属性

text-indent:文本缩进

text-align:文本水平对齐

line-height:行高

word-spacing:增加或减少单词间的空白(即字间隔)

letter-spacing:增加或减少字符间的空白(字符间距)

text-transform:控制文本大小写

direction:规定文本的书写方向

color:文本颜色 a元素除外

③  元素可见性:visibility

④  表格布局属性:caption-side、border-collapse、border-spacing、empty-cells、table-layout

⑤  列表布局属性:list-style-type、list-style-image、list-style-position、list-style

⑥  生成内容属性:quotes

⑦  光标属性:cursor

⑧  页面样式属性:page、page-break-inside、windows、orphans

⑨  声音样式属性:speak、speak-punctuation、speak-numeral、speak-header、speech-rate、volume、voice-family、 pitch、pitch-range、stress、richness、、azimuth、elevation

3、所有元素可以继承的属性

①  元素可见性:visibility

②  光标属性:cursor

4、内联元素可以继承的属性

①  字体系列属性

②   除text-indent、text-align之外的文本系列属性

5、块级元素可以继承的属性

     text-indent、text-align

CSS 7 种基础的选择器:

ID 选择器, 如 #id{}

类选择器, 如 .class{}

属性选择器, 如 a[href="segmentfault.com"]{}

伪类选择器, 如 :hover{}

伪元素选择器, 如 ::before{}

标签选择器, 如 span{}

通配选择器, 如 *{}

优先级关系 :内联样式 >ID 选择器 >类选择器 = 属性选择器 = 伪类选择器 >标签选择器 = 伪元素选择器

属性后插有 !important 的属性拥有最高优先级

:is()和:not():其伪类在计算选择器数量时当做普通选择器进行计数。