CSS 学习
1. 选择器
1.1 attribute 选择器
- 具有
a[href]
- 相等
a[href="/posts"]
- 包含
a[href*="posts"]
- 开头
a[href^="https"]
- 结尾
a[href$=".org"]
- 多个
a[href^="https"][href$=".org"]
1.2 类选择器
- 直接使用
.class-name
- 限定标签
a.class-name
- 多个
a.class-a.class-b
1.3 id 选择器
- 直接使用
#id-a
1.4 & 选择器
- 没有 & 时,嵌套解释为父子
nav { a { color: red; }}
- 有 & 时,& 被解释为 parent(这句和上面等效)
nav { & a { color: red; }}
- 没有 & 时,嵌套解释为父子
a { :hover { color: white; }}
会被解释为
a *:hover { color: white; }
- 有 & 时,& 被解释为 parent
a { &:hover { color: white }}
1.5 关系选择器
- 接邻兄弟选择器
p + a { color: red; }
- 子选择器
p > a { color: red; }
- 后代选择器
p a { color: red; }
- 兄弟选择器(只选它后面的)
p ~ a { color: red; }
1.6 选择器列表
- 多个选择器共用一套样式
a,p,h1 { color: red; }
1.7 状态类伪类
- 常用于按钮按下时
:active
- 选项被选择时
:checked
- 有效/无效
:enabled
/:disabled
- 获得焦点时
:focus
- 鼠标悬浮时
:hover
- 有效/无效时(input、form 等)
:valid
/:invalid
- 必填的
:required
- 目标(URL 中的 fragmnent 指向的元素)
:target
- 链接被访问过
:visited
1.8 关系型伪类
:first-child
/:last-child
:first-of-type
/:last-of-type
:nth-child(n)
:nth-of-type(n)
:nth-last-child(n)
:nth-last-of-type(n)
:only-child
:only-of-type
1.9 功能型伪类
:has(+ p)
1.10 伪类元素
::before
/::after
::first-letter
/::first-line
::placeholder
::selection