anjia / blog

博客,积累与沉淀
106 stars 4 forks source link

CSS 基础 #92

Open anjia opened 2 years ago

anjia commented 2 years ago

目录

  1. CSS 选择器一览表
    1. 基本选择器:#id, .class, elementName, [attr], *
    2. 组合选择器:A B, A > B, A + B, A ~ B
    3. 伪选择器::, ::
    4. 分组选择器:A, B
  2. CSS 数据类型+函数
    1. 数据类型
      • 基本 + 尺寸(有单位) + 百分比
      • 文件 + 图像 + 颜色 + 形状(都有函数)
      • 函数:转换 + 时间 + 滤镜(均是函数)
      • display
    2. CSS 函数
      • 数学 + 引用 + 计数器 + Grid
      • 字体
  3. CSS 属性一览表
    1. 布局相关
      • 盒模型:
        • box-sizing, width, height
        • padding, border, margin
      • 布局
        • 层级 + 浮动 + 绝对定位
        • 多列布局 + Flex + Grid
      • 行内相关
        • 字体 + 对齐 + 书写模式
        • 断行
    2. 非布局类
      • UI
        • 表格 + 列表 + ruby
        • 颜色 + 背景 + 混色 + 裁切
        • 文本 + 字体
        • 覆盖系统样式 + 其它
      • 滚动:溢出 + 滚动行为
      • 动画
        • 变换 + 过渡 + 动画
        • 3d + path
  4. CSS At-rules
    • 元数据规则:字符集, 外部导入
    • 条件组规则:媒体查询, 特性查询
    • 描述性规则:动画, 字体, 打印, 列表+颜色
anjia commented 2 years ago

一. CSS 选择器

CSS 选择器是 CSS 规则的一部分,CSS 规则描述了该规则将匹配文档中的哪些元素,匹配到的元素将应用规则的指定样式。

CSS 选择器有 4 大类,如下:

分类 选择器 说明 备注
基本选择器 ID 选择器 #id
类选择器 .class
类型选择器 elementName 可带命名空间
属性选择器 根据特定属性的存在或值
通配符 * 匹配所有类型 可带命名空间
组合选择器 A B 全后代   来的都是亲戚(后辈)
A > B 直接子 > 直系后一代(子女)
A + B 紧跟后面的那一个亲兄弟 + 手拉手的那一个(兄弟)
A ~ B 后面的全部兄弟们 ~ 一条船上的(兄弟)
伪选择器 伪类 : 元素的特定状态
伪元素 :: 元素的特定部分 后产生的所以有两个:
分组选择器 A, B 多个独立选择器以 , 分隔 很直观

可以使用命名空间的选择器(类型选择器和通配符)在处理包含多个命名空间的文档时会很有用,比如 HTML5 里带有内联的 SVG 和 MathML,或者 XML 元素。

  • basic selectors, 基本选择器
  • combinators /ˈkɒmbɪneɪtər/, 组合/复合符
  • pseudo /ˈs(j)uːdoʊ/, 伪
  • grouping selectors, 分组选择器

下面详细介绍下属性选择器、伪类和伪元素。

属性选择器(9个)

属性选择器根据给定属性的存在或值来匹配元素。

格式 说明 例子
[attr] 存在属性 [required]
[attr=value] 属性值等于 value input[type="text"]
[attr^=value] 值以 value 开头 a[href^="#"]
[attr$=value] 值以 value 结尾 a[href$="github.com"]
[attr*=value] 值包含 value 字符串 a[href*="/docs/"]
[attr~=value] 空格分隔的值,其中有个等于 value div[class~="g-title"]
[attr\|=value] 值等于 value
值以 value 开头且后跟连字符 -
div[lang\|="zh"]
[attr operator value i] 属性选择器后面跟字符 iI
表示比较不分大小写(ASCII 字符)
[attr operator value s] 属性选择器后面跟字符 sS
表示比较区分大小写(ASCII 字符)
a[href*="cAsE" s]

case-sensitive, 大小写敏感
case-insensitive, 大小写不敏感

属性选择器可以将多个连起来使用,比如 a[href^="https"][href$=".com"]

伪类

伪类根据所选元素的指定状态来匹配元素。

分类 伪类 说明
用户操作(5个) :hover
:active
:focus
:focus-visible
:focus-within
位置(7个) :link
:visited
:any-link
:local-link
没被访问的链接
已被访问
包含以上两种
指向相同文档的链接
:target
:target-within
当前文档里的目标元素
:scope 选择器的参考点
DOM 树结构(14个) :first-child
:last-child
:only-child
:nth-child()
:nth-last-child()
兄弟
(An+B)
:first-of-type
:last-of-type
:only-of-type
:nth-of-type()
:nth-last-of-type()
也是兄弟
(An+B)
:empty 没孩子,除了空白字符
:root 根节点
:nth-col()
:nth-last-col()
输入(17个) :disabled
:enabled
:checked
:required
:optional
:read-write
:read-only
禁用状态
启用

必填
可选
可读写(可编辑)
只读(不能修改)
:placeholder-shown
:blank
:valid
:invalid
:user-invalid
:in-range
:out-of-range
:autofill
显示占位符
内容是空
内容有效
内容无效
输入无效(仅交互时)
值在范围内
值超范围了
内容被浏览器自动填充了
:default
:indeterminate
一组表单元素中的默认元素
状态不确定的表单元素
语言(2个) :lang()
:dir()
多媒体(7个) :past
:current
:future
WebVTT,比如字幕
:playing
:paused
多媒体的状态
:fullscreen
:picture-in-picture
Web Components(4个) :defined
:host
:host()
:host-context()
Paged Media(3个) :first
:left
:right
其它(5个) :is()
:where()
:not()
:has()
老版本 matches(), :any()


:state()

pseudo-classes

  • user action ... 用户操作
  • location ... 位置
  • tree-structural ... 树结构
  • the input ... 输入
  • linguistic ... 语言
  • time-dimensional ... 时间维度
  • resource state ... 资源状态

伪元素

伪元素用于选择元素的特定部分,我们只能在选择器中使用一个伪元素,且它必须在简单选择器之后。

分类 伪元素 说明
最常用(8个) ::before
::after
::first-letter
::first-line
::selection 被用户选中的内容,比如在文本上单击或拖拽鼠标
::placeholder 占位符文本,<input><textarea>
::file-selector-button type="file"<input>
::marker 列表项的标记框,通常是个圆点或数字
其它(4个) ::slotted()
::part()
Web Components
::cue
::cue-region
WebVTT 提示,比如字幕
实验中(4个) ::backdrop 全屏模式下,比如使用了全屏 API 或是 <dialog> 元素
::target-text 滚动到的文本,对于支持滚动到文本片段的浏览器
::spelling-error 浏览器标记为拼写错误的文本片段
::grammar-error 浏览器标记为语法错误的文本片段

看几个例子

/* 命名空间 */
@namespace svg url(http://www.w3.org/2000/svg);
svg|* {
  color: blue;
}
svg|a {
  color: red;
}

/* 通配符 */
article *:first-child {
  font-weight: bold;
}
article:first-child {
  font-weight: bold;
}
.floating + * {
  clear: left;
}

/* 属性选择器 */
div[lang] {
  color: green;
}
div:not([lang]) {
  font-style: italic;
}
div[data-lang="zh-TW"] {
  color: purple;
}
div[lang|="zh"] {
  color: red;
}
a[href^="https"][href$=".com"] {
  text-decoration: underline;
}

/* 组合符 */
img + p {
  font-weight: bold;
}
h1 ~ p {
  font-weight: bold;
  background-color: #333;
  color: #fff;
  padding: 0.5em;
}
ul.my-things li {
  margin: 2em;
}

/* 伪类+伪元素 */
a:link,
a:visited {
  color: rebeccapurple;
  font-weight: bold;
}
a:hover {
  color: hotpink;
}
article p:first-child {
  font-size: 120%;
  font-weight: bold;
}
article p:first-child::first-line {
  font-size: 120%;
  font-weight: bold;
}
.box::after {
  content: " ➥";
}

/* 多个简单选择器可连成一个 */
h1#heading {
  color: rebeccapurple;
}
li.spacious.elegant {
  margin: 2em;
}
ul > li[class="a"] {
}
a[href^="https"][href$=".com"] {
  text-decoration: underline;
}

/* 分组选择器 */
h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: helvetica;
}

#main,
.content,
article {
  font-size: 1.1em;
}

小结

本文简要介绍了 CSS 选择器,旨在对其有个整体印象。

选择器分类 选择器 内容 说明 相关链接
基本选择器 #id
.class
elementName
[] 存在/值 [attr]
[attr=value]
[attr^=value], [attr$=value]
[attr*=value], [attr~=value]
[attr\|=value]
[... i], [... s]
存在
值等于
开始 + 结束
包含字符串, 字符项
值等于 or 值以它-开头
是否区分大小写
*
组合选择器 A B 全后代(后辈)
A > B 直接子(子女)
A + B 手拉手的那一个(兄弟)
A ~ B 一条船上的(兄弟)
伪选择器 : 用户操作 + 位置
DOM结构 + 输入
语言 + 多媒体 + 函数
Web Components + Paged Media
特定状态(64个)
:: before/after, 首字母/行
选中, 占位符, 列表标记
Web Components, 多媒体
特定部分(16个)
分组选择器 A, B

扩展阅读

NE-SmallTown commented 2 years ago

新年快乐 😂

anjia commented 2 years ago

新年快乐 😂

哈哈哈 新年快乐!虎年大吉~!

anjia commented 2 years ago

二. CSS 数据类型+函数

每个 CSS 声明都包含一个属性:值对。根据不同的属性,其值可能是一个关键字或数值,也可能是一系列关键字或值,它们有的带单位有的不带单位。在 CSS 中,就有一组 CSS 属性可以接受的通用数据类型(值和单位)。

CSS 数据类型定义了 CSS 属性和函数所能接受的典型值(值和单位)。CSS 函数也是一种 CSS 值,它能表示更复杂的数据类型或是调用特殊的数据处理和计算逻辑。

最常用的数据类型和函数都定义在《CSS Values and Units》规范中,其它的数据类型就定义在与其相关联的规范中。

CSS data type, CSS 数据类型
CSS functional notation, CSS 功能符号, 通常叫 CSS 函数

本文只介绍被 <> 括起来的数据类型(不包含关键字)和常见的 CSS 函数。

数据类型

分类 数据类型 说明 相关函数(61个)
基本(4个) <string> 字符串
<number>
<integer>
数值
<position> top, right, bottom, left
center
<length-percentage>
尺寸(8个) <dimension> <number>+单位 组成 指定距离/时间/频率/分辨率等尺寸
<length> <number>+单位 组成 绝对单位(长度):
px, pc, pt
in
cm, mm, Q

相对单位(字体):
em, rem
ex, rex
cap, rcap
ch, rch
ic, ric

相对单位(视口):
vw, vh
vmin, vmax
<time> <number>+s|ms 组成 单位 s, ms
<angle> <number>+单位 组成 单位 deg, grad, rad, turn
<flex> <number>+fr 组成 单位 fr
<resolution> 分辨率,像素密度 单位 dpi, dpcm, dppx, x
<ratio> <number>/<number> 组成
纵横比,媒体查询,eg.16/9
<percentage> <number>+% 组成
%(3个) <length-percentage> <length><percentage>
<time-percentage> <time><percentage>
<angle-percentage> <angle><percentage>
文件 <url> an absolute URL
a relative URL
a data URI
url()
二维图像(2个) <image> 二维图像,综合型 可以是以下类型:
<url>
<gradient>

可以是以下函数:
element()
image(), image-set()
cross-fade()
paint()
<gradient> linear-gradient()
conic-gradient()
radial-gradient()
repeating-linear-gradient()
repeating-radial-gradient()
repeating-conic-gradient()
颜色(2个) <color> 综合型
可以是各种关键字
rgb(), rgba()
hsl(), hsla()
hwb(), lch(), lab()
color()
color-mix()
color-contrast()
<blend-mode> 当元素重叠时如何显示颜色
用在如下属性:
background-blend-mode
mix-blend-mode
形状 <basic-shape> 在以下属性中使用:
clip-path
shape-outside
offset-path
inset() 矩形
circle()
ellipse()
polygon()
path()
<shape>(已废弃) 用在 clip 属性(已废弃)
已被 clip-path 属性替代
rect() 已废弃
函数(3个) <transform-function> rotate()
rotate3d()
rotateX()
rotateY()
rotateZ()
scale()
scale3d()
scaleX()
scaleY()
scaleZ()
skew()
skewX()
skewY()
translate()
translate3d()
translateX()
translateY()
translateZ()
matrix()
matrix3d()
perspective()
<timing-function> ease
ease-in
ease-out
ease-in-out
cubic-bezier()
step-start
step-end
steps()
<filter-function> 图像效果 grayscale()
opacity()
blur()
brightness()
hue-rotate()
contrast()
drop-shadow()
invert()
saturate()
sepia()
display(6个) <display-outside> block, inline, run-in
<display-inside> flow, flow-root
table, flex, grid
ruby
<display-listitem> block, inline, run-in ? &&
flow, flow-root ? &&
list-item
<display-internal> table-header-group
table-footer-group
table-column-group
table-row-group
table-caption
table-column
table-row
table-cell
ruby-base, ruby-text
ruby-base-container
ruby-text-container
<display-box> contents, none
<display-legacy> inline-block
inline-table
inline-flex
inline-grid
inline-list-item

其它(6个):

CSS 函数

除了上述表格中列出的与特定类型相关的函数之外,还有以下 CSS 函数。

分类 函数 说明
数学(21个) calc()
clamp()
min(), max()
abs(), sign()
sqrt()
pow(), exp()
log()
round()
sin(), cos(), tan()
asin(), acos(), atan(), atan2()
hypot()
mod(), rem()
引用(4个) attr() 属性
var() 自定义变量
env() 环境变量
url()
计数器(3个) counter()
counters()
content 属性
symbols() list-style 属性, counter styles
Grid(3个) repeat()
minmax()
fit-content()
字体(8个) stylistic()
styleset()
character-variant()
swash()
ornaments()
annotation()
font-variant-alternates 属性
local(), format() @font-facesrc 描述符的值

其它(7个):

小结

本文主要对 CSS 的通用数据类型(值和单位)和常见函数做了下分类整理,旨在对其有个整体印象。

分类 内容 说明
数据类型(36 个) 基本 + 尺寸(有单位) + 百分比
文件 + 图像 + 颜色 + 形状(都有函数)
函数:转换 + 时间 + 滤镜(均是函数)
display
相关函数(61 个)
CSS 函数(46 个) 数学 + 引用 + 计数器 + Grid
字体

扩展阅读

// https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
let datatypeArr = [], funcArr = [], unitArr = [];
document.querySelectorAll(".index code").forEach(item => {
  if(/^:/.test(item.innerText)) {}
  else if (/<[a-z-]+>/.test(item.innerText)) datatypeArr.push(item.innerText);
  else if (/\(\)$/.test(item.innerText)) funcArr.push(item.innerText);
  else if (/#/.test(item.innerText)) unitArr.push(item.innerText);
});
// 37(36+1), 82, 30
console.log(datatypeArr.length, funcArr.length, unitArr.length);
anjia commented 2 years ago

三. CSS 属性

CSS 的基本目标是让浏览器使用特定的功能(比如定位/颜色/装饰等)来绘制页面上的元素,它的语法就很好地反映了这一目标。CSS 的基本构建块如下:

每一个属性(property)都有一组有效值,它们由形式语法和语义来定义,并由浏览器引擎来实现。CSS 语言的核心功能就是给属性设置特定的值,大约有超过 100 种不同的 CSS 属性和几乎不限数量的 CSS 值。

本文就带大家来一起看看 CSS 属性,我们先介绍与布局相关的,再介绍非布局类的。

布局相关

盒模型(94个)

属性 相关属性 四方向属性 逻辑属性 逻辑方向属性
box-sizing
width
height
max-width
max-height
min-width
min-height
block-size
inline-size
max-block-size
max-inline-size
min-block-size
min-inline-size
padding padding-top
padding-right
padding-bottom
padding-left
padding-block
padding-inline
padding-block-start
padding-block-end
padding-inline-start
padding-inline-end
border border-top
border-right
border-bottom
border-left
border-block
border-inline
border-block-start
border-block-end
border-inline-start
border-inline-end
border-width border-top-width
border-right-width
border-left-width
border-bottom-width
border-block-width
border-inline-width
border-block-start-width
border-block-end-width
border-inline-start-width
border-inline-end-width
border-style border-top-style
border-right-style
border-left-style
border-bottom-style
border-block-style
border-inline-style
border-block-start-style
border-block-end-style
border-inline-start-style
border-inline-end-style
border-color border-top-color
border-right-color
border-bottom-color
border-left-color
border-block-color
border-inline-color
border-block-start-color
border-block-end-color
border-inline-start-color
border-inline-end-color
border-radius border-top-left-radius
border-top-right-radius
border-bottom-left-radius
border-bottom-right-radius
border-start-start-radius
border-start-end-radius
border-end-end-radius
border-end-start-radius
border-image border-image-outset
border-image-repeat
border-image-slice
border-image-source
border-image-width
margin margin-top
margin-right
margin-bottom
margin-left
margin-block
margin-inline
margin-block-start
margin-block-end
margin-inline-start
margin-inline-end

布局(62个)

布局 属性 缩写 相关属性 说明
层级(1个) z-index
浮动(2个) float clear
绝对定位
(12个)
position top
right
bottom
left
inset
inset-block
inset-inline
inset-block-start
inset-block-end
inset-inline-start
inset-inline-end
多列布局
(17个)
column-width
column-count
columns 多列布局
column-fill
column-span
column-rule-width
column-rule-style
column-rule-color
column-rule
row-gap
column-gap
gap 间距(这3个是标准的)
flex, grid 也能用
align-items(flex)
justify-items
place-items flex, grid 也能用
align-content(flex)
justify-content(flex)
place-content flex, grid 也能用
align-self(flex)
justify-self
place-self flex, grid 也能用
Flex(12个) flex-direction
flex-wrap
flex-flow flex container
justify-content
align-items
align-content
main-axis, flex container
cross-axis
cross-axis, multi-line
flex container(多列的3个gap)
flex-grow
flex-shrink
flex-basis
flex flex item
align-self flex item, cross-axis
order flex item
Grid(18个) grid-template-rows
grid-template-columns
grid-template-areas
grid-auto-rows
grid-auto-columns
grid-auto-flow
grid-template
grid
grid container
grid-row-gap(已废弃)
grid-column-gap(已废弃)
grid-gap(已废弃) 最新见多列的3个gap
masonry-auto-flow
align-tracks
justify-tracks
grid container(实验中)
grid-row-start
grid-row-end
grid-column-start
grid-column-end
grid-row
grid-column
grid-area
grid item

行内(43个)

分类 属性 缩写 说明
字体(8个) font-family
font-size(它)
font-stretch
font-style
font-variant(缩写)
font-weight
line-height(它)
font
对齐(10个) vertical-align
text-align
text-align-last
text-justify
text-indent
文本
object-fit
object-position
image-orientation
image-rendering
image-resolution
图片
书写模式(5个) writing-mode
direction
unicode-bidi
text-orientation
text-combine-upright
断行(20个) overflow-wrap(新名)
word-wrap(旧名)
word-break
line-break(CJK)
hyphens(兼容性不好)
text-overflow
white-space
-webkit-line-clamp
line-clamp
hyphenate-character(连字符)
hyphens(断行时)
box-decoration-break(文本段)
break-after
break-before
break-inside
page-break-after
page-break-before
page-break-inside
orphans
widows
孤儿
寡母

非布局类

UI(120个)

分类 属性 缩写 说明
表格(5个) table-layout
caption-side
border-collapse
border-spacing
empty-cells
列表(7个) list-style-type
list-style-position
list-style-image
list-style
counter-set
counter-reset
counter-increment
ruby(3个) ruby-position
ruby-align
ruby-merge
<ruby>元素
颜色(3个) color
color-scheme
color-adjust
背景(12个) background-attachment
background-clip
background-color
background-image
background-origin
background-position
background-repeat
background-size
background
background-position-x
background-position-y
background-blend-mode(混色)
混色(2个) isolation
mix-blend-mode
是否创建独立堆叠上下文
如何混合(和父亲/自己背景)
裁切(24个) clip-path
clip(已废弃)
clip
shape-outside
shape-margin
shape-image-threshold
filter
backdrop-filter
滤镜
mask-clip
mask-composite
mask-image
mask-mode
mask-origin
mask-position
mask-repeat
mask-size
mask mask
mask-border-mode
mask-border-outset
mask-border-repeat
mask-border-slice
mask-border-source
mask-border-width
mask-border
mask-type
文本(21个) text-decoration-color
text-decoration-line
text-decoration-style
text-decoration-thickness
text-decoration 装饰线
text-decoration-skip
text-decoration-skip-ink
text-underline-offset
text-underline-position
text-emphasis-style
text-emphasis-color
text-emphasis 强调符
text-emphasis-position
word-spacing
letter-spacing
text-transform
text-shadow
tab-size
initial-letter
initial-letter-align
单词间距
字符间距
大小写
阴影
tab
首字母
首字母
hanging-punctuation 标点符号的位置(行首/尾)
字体(14个) font-variant-alternates
font-variant-caps
font-variant-east-asian
font-variant-ligatures
font-variant-numeric
font-variant 可变字体
font-variant-position
font-variation-settings
font-feature-settings
font-size-adjust
上下标
控制可变字体
OpenType字体
小写字母的相对大小
font-language-override
font-optical-sizing
font-kerning
font-synthesis
覆盖系统(11个) outline-color
outline-style
outline-width
outline outline
outline-offset
caret-color
accent-color
输入光标的颜色
复选框/单选框等的颜色
scrollbar-width
scrollbar-color
scrollbar-gutter
滚动条
appearance 平台原生样式
其它(18个) touch-action
pointer-events
user-select
resize
触屏交互
能否成为事件目标
能否被选中
能否改变大小
will-change
contain
content-visibility
text-rendering
性能相关
content
quotes
很好用
cursor
opacity
visibility
box-shadow
鼠标
不透明度
隐藏(占位)
阴影
paint-order 绘制顺序(fill, stroke, markers)
aspect-ratio 首选纵横比
--* 自定义变量
all 所有的 CSS 属性
除了unicode-bidi, direction
和自定义变量

滚动(39个)

分类 属性 相关属性 逻辑属性 逻辑方向属性
溢出 (8个) overflow overflow-x
overflow-y
overflow-block
overflow-inline
overflow-wrap
overflow-anchor
overflow-clip-margin
滚动 (26个) scroll-behavior
scroll-snap-type
scroll-snap-align
scroll-snap-stop
scroll-margin scroll-margin-top
scroll-margin-right
scroll-margin-bottom
scroll-margin-left
scroll-margin-block
scroll-margin-inline
scroll-margin-block-start
scroll-margin-block-end
scroll-margin-inline-start
scroll-margin-inline-end
scroll-padding scroll-padding-top
scroll-padding-right
scroll-padding-bottom
scroll-padding-left
scroll-padding-block
scroll-padding-inline
scroll-padding-block-start
scroll-padding-block-end
scroll-padding-inline-start
scroll-padding-inline-end
滚动行为 (5个) overscroll-behavior overscroll-behavior-x
overscroll-behavior-y
overscroll-behavior-block
overscroll-behavior-inline

动画(30个)

分类 属性 缩写 说明
变换(7个) transform
transform-origin
transform-box
transform-style(是否 3d)
translate
scale
rotate
可单独指定
2d/3d(3个) perspective
perspective-origin
backface-visibility
过渡(5个) transition-delay
transition-duration
transition-property
transition-timing-function
transition
动画(9个) animation-name
animation-duration
animation-timing-function
animation-delay
animation-direction
animation-iteration-count
animation-fill-mode
animation-play-state
animation
path(6个) offset-position
offset-path
offset-distance
offset-anchor
offset-rotate
offset
早期叫 motion

其它(8个)

小结

本文简要梳理了下 CSS 属性,一共涵盖了 396 个。如下:

分类 内容
布局相关(199 个) 盒模型 94 个 box-sizing, width, height
padding, border, margin
布局 62 个 层级 + 浮动 + 绝对定位
多列布局 + Flex + Grid
行内相关 43 个 字体 + 对齐 + 书写模式
断行
非布局类(197 个) UI 120 个 表格 + 列表 + ruby
颜色 + 背景 + 混色 + 裁切
文本 + 字体
覆盖系统样式 + 其它
滚动 39 个 溢出 + 滚动行为
动画 30 个 变换 + 过渡 + 动画
3d + path
其它 8 个

扩展阅读

// https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
let attrArr = [];
document.querySelectorAll(".index code").forEach(item => {
  let code = item.innerText;
  if(/^:/.test(code)) {}  // 伪类+伪元素, 62: + 15::
  else if (/\<[a-z-]+\>/.test(code)) {} // 数据类型, 37
  else if (/\(\)$/.test(code)) {}     // 函数, 82
  else if (/\#/.test(code)) {}         // 单位, 30
  else if (/\@/.test(code)) {}         // at-rule, 69
  else attrArr.push(code); // 正常属性, 401
});
console.log(attrArr.length); // 401  -6 = 395
// 4个关键字:unset, inherit, initial, revert
// 2个链接标题:'Pseudo-classes', 'Pseudo-elements'
anjia commented 2 years ago

四. CSS At-rules

CSS 里的绝大部分内容都是规则集,即选择器和属性:值的集合。当然,也有一些功能是规则集覆盖不到的,比如开发者想在样式表里指定字符集、字体、列表计数器等信息,或是想导入外部样式表。要想实现上述功能,就得用到 CSS 里的另一种语句了。

CSS 语句是一个构建块,它以任何非空格字符开始,以 }; 结束。有两种类型的 CSS 语句,分别是规则集和 At-rules。任何不是规则集也不是 At-rules 的语句都是无效的,会被忽略。本文将介绍 At-rules 语句。

还有一组语句叫嵌套语句,它们可以用在部分 At-rules 里,比如条件组的规则。此外,《CSS Nesting Module》 规范也列出了 CSS 原生嵌套的功能,目前尚处于工作草案阶段,更多详情可查阅历史文章 #1。

At-rules 以 @ 开始,后面跟一个标识符,然后一直持续到语句末尾,要么是 } 要么是 ;。每种 At-rules 的类型由其标识符定义的,它们都有自己的内部语法和语义。At-rules 可以用来传达元数据信息(如@charset, @import)、条件信息(如@media)和描述性信息(如@font-face)等。

At-rules(27个)

分类 At-rules 相关规则 说明
元数据(3个) @charsetbr>`@import`<br@namespace 字符集(均为非嵌套语句)
外部导入
定义 XML 命名空间
条件组(3个) @mediabr>`@supports`<br@document(已废弃) 媒体查询
特性查询
根据 URL
动画(2个) @keyframes
@scroll-timeline
字体(8个) @font-face
@font-feature-values @swashbr>`@annotation`<br@ornamentsbr>`@stylistic`<br@styleset
@character-variant
打印(5个) @page @top-centerbr>`@bottom-center`<br@left-bottom
@right-bottom
其它(3个) @counter-style 可用在 list-style,
counter(), counters()
@color-profile 可用在 color()
@property CSS Houdini

其它(3个):

扩展阅读

// https://developer.mozilla.org/en-US/docs/Web/CSS/Reference
let ruleArr = [], descriptorArr = [];
document.querySelectorAll(".index code").forEach(item => {
  let code = item.innerText;
  if (/^\@/.test(code)) ruleArr.push(code);
  else if (/\@/.test(code)) descriptorArr.push(code);
});
console.log(ruleArr.length, descriptorArr.length); // 25, 44