内联 > ID 选择器 > 类选择器 > 标签选择器
具体到计算层面,优先级由 A、B、C、D 的值来决定,计算规则如下:
比较时,权重从左到右依次减小。
ul ol li .red {} /* {0, 0, 1, 3}*/
#red {} /* {0, 1, 0, 0} */
*
通用元素选择器,匹配任何元素Element
标签选择器.className
class 选择器#footer
id 选择器E, F
多元素选择器,同时匹配所有 E 元素 或 F 元素E F
后代选择器,匹配所有属于 E 元素后代的 F 元素E > F
子元素选择器,匹配所有 E 元素的子元素 FE + F
相邻元素选择器,匹配所有紧随着 E 元素之后的同级元素 FE ~ F
CSS3,匹配任何在 E 元素之后的同级 F 元素E[attr]
匹配所有具有 attr 属性的 E 元素(E 可以省略,如 [checked]
)E[attr=val]
匹配所有 attr 属性值为 val 的 E 元素E[attr~=val]
匹配所有 attr 属性具有多个空格分隔的值、其中一个值等于 val 的 E 元素,如具有多个 class 名的元素E[attr^="val"]
属性值 attr 以 "val" 开头的元素E[attr$="val"]
属性值 attr 以 "val" 结尾的元素E[attr*="val"]
属性值 attr 包含 "val" 字符串的元素因为浏览器的兼容问题,不同浏览器对有些标签的默认值是不同的,如果没对CSS初始化往往会出现浏览器之间的页面显示差异
初始化样式会对SEO有一定的影响,但鱼和熊掌不可兼得,但力求影响最小的情况下初始化
推荐
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td { margin:0; padding:0; }
body, button, input, select, textarea { font:12px/1.5tahoma, arial, \5b8b\4f53; }
h1, h2, h3, h4, h5, h6{ font-size:100%; }
address, cite, dfn, em, var { font-style:normal; }
code, kbd, pre, samp { font-family:couriernew, courier, monospace; }
small{ font-size:12px; }
ul, ol { list-style:none; }
a { text-decoration:none; }
a:hover { text-decoration:underline; }
sup { vertical-align:text-top; }
sub{ vertical-align:text-bottom; }
legend { color:#000; }
fieldset, img { border:0; }
button, input, select, textarea { font-size:100%; }
table { border-collapse:collapse; border-spacing:0; }
E:first-child
匹配父元素的第一个子元素E:link
匹配所有未被点击的链接E:visited
匹配所有已被点击的链接E:active
匹配鼠标已经在其上按下、还没有释放的 E 元素E:hover
匹配鼠标悬停其三的 E 元素E:focus
匹配获得当前焦点的 E 元素E:lang(c)
匹配 lang 属性等于 c 的 E 元素E:enabled
匹配表单中激活的元素E:disabled
匹配表单中禁用的元素E:checked
匹配表单中被选中的radio(单选框)或checkbox(复选框)元素E::selection
匹配用户当前选中的元素E:root
匹配文档的根元素,对于HTML文档,就是HTML元素E:nth-child(n)
匹配其父元素的第n个子元素,第一个编号为1E:nth-of-type(n)
与:nth-child()作用类似,但是仅匹配使用同种标签的元素E:nth-last-of-type(n)
与:nth-last-child() 作用类似,但是仅匹配使用同种标签的元素E:last-child
匹配父元素的最后一个子元素,等同于:nth-last-child(1)E:first-of-type
匹配父元素下使用同种标签的第一个子元素,等同于:nth-of-type(1)E:last-of-type
匹配父元素下使用同种标签的最后一个子元素,等同于:nth-last-of-type(1)E:only-child
匹配父元素下仅有的一个子元素,等同于:first-child:last-child或 :nth-child(1):nth-last-child(1)E:only-of-type
匹配父元素下使用同种标签的唯一一个子元素,等同于:first-of-type:last-of-type或 :nth-of-type(1):nth-last-of-type(1)E:empty
匹配一个不包含任何子元素的元素,注意,文本节点也被看作子元素E:not(s)
匹配不符合当前选择器的任何元素E:first-line
匹配 E 元素的第一行E:first-letter
匹配 E 元素的第一个字母E:before
在 E 元素之前插入生成的内容E:after
在 E 元素之后插入生成的内容值 | 说明 |
---|---|
block | 块类型。默认宽度为父元素宽度,可设置宽高,换行显示 |
none | 缺省值。象行内元素类型一样显示 |
inline | 行内元素类型。默认宽度为内容宽度,不可设置宽高,同行显示 |
inline-block | 默认宽度为内容宽度,可以设置宽高,同行显示 |
list-item | 像块类型元素一样显示,并添加样式列表标记 |
table | 此元素会作为块级表格来显示 |
inherit | 规定应该从父元素继承 display 属性的值 |
<input>
<img>
<button>
<textarea>
<label>
margin: 0 auto
水平居中
<html>
<body>
<div class="center-0">
水平居中
</div>
</body>
</html>
<style>
.center-0 {
height: 200px;
width: 200px;
margin: 0 auto;
border: 1px solid red;
}
</style>
felx 布局
<html>
<body>
<div class="center">
<div class="flex-div">1</div>
<div class="flex-div">2</div>
</div>
</body>
</html>
<style>
.center {
display: flex;
justify-content: center;
}
.flex-div {
border: 1px solid red;
}
</style>
table
table-center
<html>
<body>
<div class="table-center">
table-center
</div>
</body>
</html>
<style>
.table-center {
display: table;
margin: 0 auto;
border: 1px solid red;
}
</style>
static
relative
absolute
fixed
sticky 粘性定位,近似于 relative 和 fixed 的合体(示例如下)
<html>
<body>
<div class="container">
<h1 class="sticky-box">标题一</h1>
<h1 class="sticky-box">标题二</h1>
<h1 class="sticky-box">标题三</h1>
<h1 class="sticky-box">标题四</h1>
</div>
</body>
</html>
<style>
.container {
background: #eee;
width: 200px;
height: 1000px;
margin: 0 auto;
}
.sticky-box {
position: -webkit-sticky;
position: sticky;
height: 60px;
margin-bottom: 30px;
background: #ff7300;
top: 0px;
}
h1 {
font-size: 30px;
text-align: center;
color: #fff;
line-height: 60px;
}
</style>
z-index
值不为 'auto'
的 绝对/相对 定位z-index
值不为 'auto'
的 flex item,即父元素 display: flex | inline-flex
opacity
属性值小于 1
的元素transform
属性值不为 'none'
的元素mix-blend-mode
属性值不为 'normal'
的元素filter
值不为 'none'
的元素perspective
值不为 'none'
的元素isolation
属性被设置为 'isolate'
的元素position:fixed
will-change
中指定了任意 CSS 属性,即便没有直接指定这些属性的值-webkit-overflow-scrolling
属性被设置为 'touch'
的元素position:fixed/absolute
float
不为 none
overflow
不为 hidden
display
的值为 inline-block
、table-cell
、table-caption
margin
发生折叠 DEMO (opens new window)清除浮动实际上是清除父元素的高度塌陷。因为子元素脱离了父元素的文档流,所以父元素失去了高度,导致了塌陷。要解决这个问题,就是让父元素具有高度。
<div style="clear:both;"></div>
:after
伪元素和 IEhack 触发 hasLayout在 flex 成为主流布局之后,浮动越来越少见了,因为它的副作用较大
好处:
缺点:
是什么
怎么使用
true
或 false
true
,那么该媒体查询的结果为 true
,那么媒体查询内的样式会生效。示例
<!-- link 元素中的 CSS 媒体查询 -->
<link rel="stylesheet" media="(max-width: 800px)" href="example.css">
<!-- 样式表中的 CSS 媒体查询 -->
<style>
@media (max-width: 600px) {
.facet_sidebar {
display: none;
}
}
</style>
盒模型是什么
content
、padding
、border
、margin
组成。标准盒模型和怪异盒模型的区别
margin-left
+ border-left
+ padding-left
+ width
+ padding-right
+ border-right
+ margin-right
margin-left
+ width
+ margin-right
(width
= border-left
+ padding-left
+ content
+ padding-right
+ border-right
)现代浏览器默认使用 W3C 的标准和模型,可以在 CSS3 中使用 box-sizing 自定义
box-sizing: content-box; /* 标准盒模型 */
box-sizing: border-box; /* 怪异盒模型 */
box-sizing: padding-box; /* 火狐的私有模型 */
translate() 是 transform 的一个值。
改变 transform 或 opacity 不会触发浏览器重新布局(reflow)或重绘(repaint),只会触发复合(compositions)。
而改变绝对定位会触发重新布局,进而触发重绘和复合。
transform 使浏览器为元素创建一个 GPU 图层,但改变绝对定位会使用到 CPU。因此 translate() 更高效,可以缩短平滑动画的绘制时间。
translate 改变位置时,元素依然会占据原始空间,绝对定位不会发生这种情况。
多数显示器默认频率是 60 Hz,即 1 秒刷新 60 次,所以理论上最小间隔为 1/60*1000ms = 16.7ms
被点击访问过的超链接样式不再具有 hover 和 active 了,解决方法是改变 CSS 属性的排列顺序: L-V-H-A(link,visited,hover,active)
rgba()
和 opacity
都能实现透明效果,但最大的不同是 opacity
作用于元素,以及元素内的所有内容的透明度,而 rgba()
只作用于元素的颜色或其背景色。(设置 rgba
透明的元素的子元素不会继承透明效果!)