盒模型
- 盒模型从内到外分别为
- content
- padding
- border
- margin
- shadow(与margin共享空间)
- outline(与margin共享空间)
- 盒模型用于判断
width
和height
的作用范围,如:box-sizing: content-box
为 宽高的设定作用在内容上。 - 盒模型分为:
content-box
padding-box
(不起作用)border-box
marging-box
(不起作用)
box-sizing
在 Emmet 中只有bxs:bb
和bxs:cb
- 把所有元素设置为盒模型
*,
*::before,
*::after{
box-sizing: border-box;
}
选择器
选择器分为
- 通配符:
*
- 标签名:
div
section
等 - 类名:
.class-name
- ID名:
#id_name
- 属性:
[data-name]
[data-name='value']
[data-name*='al']
[data-name^='val']``[data-name$='ue']
- 伪类:
:hover
:marker
等 - 伪元素:
::before
::after
等 - 子孙元素:
.level-1 .level-N
- 直系子元素:
.level-1 > .level-1-1
- 共同父元素的相邻元素:
.box .item-1 + .item
- 共同父元素的后续元素:
.box .item-1 ~ .item
级联
级联是指在CSS中用于处理多个样式规则冲突的作用算法
级联的四个规则
- 规则位置(
<link>
<<style>
<<a style=""
)和顺序(后者覆盖前者) - 具体的选择器(tag < class < id)选择器太复制会导致样式难以覆盖,要保持使用简单的选择器。
- 规则的来源:浏览器样式、浏览器插件样式、自己写的样式(明确度由低到高)
- User Agent base styles:来自浏览器的样式
- Local User styles:来自系统、浏览器插件的样式
- Authored CSS:写入的CSS
- Local User Styles
!important
来自系统、浏览器插件的!important
样式 - User Agent
!important
来自浏览器的!important
样式
重要程度(由低到高)
*
=> 0div
::section
=> 1.class-name
:hover
[data-key]
=> 10 (其中:not
不计分)#id
=> 1,000!important
=> 10,000
其中,选择器叠加则分数叠加
a.class-name
=> 11.class-name.class-name-1
=> 20a#id.class-name li.class-name-1
=> 1 + 1000 + 10 + 1 + 10 => 1022
还可以用 x-x-x 的方式,更直观的表达:
a.class-name
=> 0-1-1.class-name.class-name-1
=> 0-2-0a#id.class-name li.class-name-1
=> 1-2-2
继承
- 继承的逻辑是,当一个元素设置了一个可以被继承的属性时,这个元素的子孙辈的元素都能继承这个属性值。
- 点击这里查看可以被继承的属性
inherit
这个值可以使得元素从父元素中继承样式,如,<p>
设置了color: red
,而<aside>
设置了color: blue
,此时<aside>
中的<p>
是红色的,设置aside p
的color: inherit
则可以使得<aside>
中的<p>
为蓝色。这样做的好处是无需关注<p>
本身的颜色和需要给aside p
设置什么颜色,只需关注<aside>
的颜色即可initial
这个值可以使得属性值还原。每个属性都有一个初始值,这个值可以使得我们无需关注属性的初始值是什么,直接还原这个属性值。unset
这个值使得可以继承的属性设置为inherit
,使得不能继承的属性设置为initial
。可以配合all
使用:all: unset
。颜色
16进制颜色
#AABBCC
对应 RGB 的三个颜色,还有#AABBCCDD
对应 RGBA 的三个颜色与透明度rgb()
rgba()
:其中rgb()
可以rgb(0 0 0)
和rgb(0 0 0 / 50%)
不使用逗号和表示透明度hsl
第一个参数 0 - 360 或者是角度(deg) ,也可以像rgb
一样不使用逗号和追加/ 50%
表示透明度- 还有一些 颜色关键字
Flexbox
Grid
伪元素(pseudo-elemets)
::before
和::after
::first-letter
:一个块级元素的一个字符::first-line
:一个块级元素的的第一行字符::backdrop
::markder
:ol li
,ul li
和summary
自带的标签::selection
:选中字符的样式::placeholder
:<input />
,<textarea>
标签的提示语-
伪类
:hover
:鼠标在元素上时:active
:鼠标在元素上按下时(onmousedown):focus
:按钮,输入框,选中时(鼠标):focus-within
:当这个元素的子元素选中时,该元素会有这个伪类:focus-visible
:按钮(键盘),输入框,选中时:target
:转跳到锚点处时:link
:<a>
标签中含有href属性时:visited
:<a>
标签被访问后的样式:disabled
:表单元素中有 disabled 属性的元素时:enabled
:表单元素中没有 disabled 属性的元素时:checked
:<input type="checkbox" />
选中时:indeterminate
:<input type="checkbox" />
半选中,<progress>
没有固定值时:placeholder-shown
:placeholder 有值且元素本身值为空时:valid
:如<input type="email" />
符合正则规则时:invalid
:如<input type="email" />
不符合正则规则时:in-range
:如<input type="number" min="1" max="10" />
值在范围内时:required
:如<input required />
:optional
:如<input />
first-child
last-child
only-child
:子元素中第一个,最后一个是当前标签时生效,不是当前标签时不生效first-of-type
last-of-type
:子元素中第一个,最后一个当前标签nth-child(x)
:第几个子元素,若指定元素标签如div:nth-child(x)
则第x个子元素为<div>
才会生效nth-of-type(x)
:如div:nth-of-type(x)
为子元素中的第x个标签为<div>
的元素only-of-type
:如strong:only-of-type
为子元素中若只有一个<strong>
标签则生效:empty
:当一个元素没有子元素时(没有html标签,没有字符,甚至没有空格才行):is()
:可以简化CSS写法,如.post img, post video{}
=>.post :is(img, video){}
:not()
:可以排除一些选择器,如post :not(img)
Border(border-image待)
缩写:
border: 1px solid #eee
border-style
:- 八种 style 样式:
dotted
dashed
solid
double
groove
ridge
inset
outset
- 可以四个方向分别设置:
border-top-style
border-right-style
border-bottom-style
border-left-style
- 八种 style 样式:
border-color
:- 默认颜色为当前元素的字体颜色(来自
color
或者 继承的字体颜色) - 可以四个方向分别设置:
border-top-color
border-right-color
border-bottom-color
border-left-color
- 默认颜色为当前元素的字体颜色(来自
border-width
:- 三个关键字:
thin
medium
thick
- 可以使用长度单位,如:
px
em
rem
%
- 可以四个方向分别设置:
border-top-width``border-right-width``border-bottom-width
border-left-width
- 三个关键字:
border-radius
:- 可以四个方向分别设置:
border-top-left-width``border-top-right-width``border-bottom-right-width
border-bottom-left-width
- 缩写:
boder-radius: 5px 4px 3px 2px
- 还能画椭圆:
boder-radius: 5px 4px 3px 2px / 2px 3px 4px 5px
- 可以四个方向分别设置:
border-image
:Shadow
box-shadow
box-shadow: <水平偏移量> <垂直偏移量> <阴影模糊半径> <阴影扩散半径> <阴影颜色>
- 默认颜色为当前元素的字体颜色(来自
color
或者 继承的字体颜色) - 可以多个方向,多个颜色的叠加
- 阴影形状受到
border-radius
的影响 -
text-shadow
text-shadow: <水平偏移量> <垂直偏移量> <阴影模糊半径> <阴影颜色>
- 默认颜色为当前元素的字体颜色(来自
color
或者 继承的字体颜色) - 可以多个方向,多个颜色的叠加
Drop shadow(待)
Focus(待)
Z-index(待Creating a stacking context)
- 控制Z方向上的顺序
- 在一般情况下,设置了
z-index
但是不生效时,需要给元素position
设置一个static
以外的值(flexbox和grid不需要)。 - 可以通过给
z-index
设置负值,使得元素Child展示在元素Parent后面,但需要元素Pz-index: auto
,如果不行,参考2 z-index
的堆叠根据父节点的z-index
,父元素不同时,根据父元素的优先级来堆叠,同一父元素,根据元素本身堆叠- Creating a stacking context
Function(clamp等 待)
- 大多数都是纯函数
- 在某些情况下可以互相嵌套
- 定义属性需要写在
:root
中,且变量需要已--
开头,如--color-red
,且大小写敏感 - 使用
var()
调用自定义的属性,可以传入两个参数,当属性不存在时,返回第二个参数,如var(--color-red, red)
attr()
可以返回标签上的属性值url()
常用于 img 等calc()
计算数字,min()
取最小值,max()
取最大值clamp()
待clip-path
裁剪,配合circle
等使用,创建各种形状的显示区域transform
形变rotarteX()
rotateY()
rotateZ()
scaleX()
scaleY()
scaleZ()
translateX()
translateY()
translateZ()
skew
skew()
skewX()
skewY()
-
渐变
linear-gradient()
线性渐变,可以传入两个或多个颜色,还可以传入一个方向(角度45deg、关键字to right)radial-gradient()
圆形渐变,从圆心开始,圆心位置首background-position
影响conic-gradient()
圆锥型渐变,conic-gradient(form <起始角度> at <锥形中心x坐标> <锥形中心y坐标>, <颜色> [角度] [角度])
- 重复
repeating-linear-gradient()
repeating-radial-gradient()
repeating-conic-gradient()
混合 如
backgrand: linear-gradient(), linear-gradient();
动画Animation
@keyframe
定义动画的名称和某个时刻的状态,如
@keyframe bling {form:{} to:{}}
可以使用
from {} to{}
也可以使用百分比10%{} 50%{} 100%{}
animation properties
animation-during
一次完整的动画的持续时间,完成一次@keyframe
过程的时间animation-timing-function
变化曲线,如linear
ease
ease-in
等,还可以自定义曲线cubic-bezier()
animation-iteration-count
动画的重复次数,可以传入具体次数或一直重复(10/infinite
)animation-direction
动画方向,如normal
reverse
alternate
alternate-reverse
animation-delay
动画开始的延迟时间animation-play-state
动画运行中、暂停的状态,running
paused
animation-fill-mode
CSS动画在执行之前和之后如何将样式应用于其目标更多内容
CSS reset