前端基础-CSS基础
概述
前端基础-CSS基础
正文
CSS选择器
CSS 选择器是 CSS 规则的第一部分。它是元素和其他部分组合起来告诉浏览器哪个 HTML 元素应当是被选为应用规则中的 CSS 属性值的方式。选择器所选择的元素,叫做“选择器的对象”。
选择器的种类:类型、类和 ID 选择器
1
2
3
4
5
6
7
8
9/* 类型 */
h1 {
}
/* 类 */
.xxx{
}
/* ID选择器 */
#xxx{
}标签属性选择器
1
2
3
4/* 根据一个元素上的某个标签的属性的存在以选择元素的不同方式 */
a[title] {}
/* 根据一个有特定值的标签属性是否存在来选择 */
a[href="https://example.com"] {}伪类与伪元素
1
2
3
4
5/* 伪类:用来样式化一个元素的特定状态. :hover伪类会在鼠标指针悬浮到一个元素上的时候选择这个元素 */
a:hover{}
/* 伪元素:选择一个元素的某个部分而不是元素自己. ::first-line是会选择一个元素(<p>)中的第一行,类似<span>包在了第一个被格式化的外面,然后选择这个<span> */
p::first-line{}运算符
将其它选择器组合起来,更辅助的选择元素. (>)选择了<xxx>元素的初代子元素1
2article >p {
}
类型、类和ID选择器
- 类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76类型选择器,也叫"标签名选择器/元素选择器"
span {
background-color: yellow;
}
strong {
color: rebeccapurple;
}
em {
color: rebeccapurple;
}
/* 全局选择器* */
* {
margin: 0;
}
/* xx元素的第一个子元素 */
xx :first-child{};
xx *:first-child{};
```
- 类选择器
```css
.highlight {
background-color: yellow;
}
/* <h1 class="highlight">Class selectors</h1>
<p>Veggies es bonus vobis, proinde vos postulo essum magis <span class="highlight">kohlrabi welsh onion</span> daikon amaranth tatsoi tomatillo
melon azuki bean garlic.</p>
<p class="highlight">Gumbo beet greens corn soko <strong>endive</strong> gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard
greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini.</p> */
/* 指向特定元素的类 */
span.highlight {
background-color: yellow;
}
h1.highlight {
background-color: pink;
}
/* 多个类被应用的时候指向一个元素 */
.notebox {
border: 4px solid #666;
padding: .5em;
}
.notebox.warning {
border-color: orange;
font-weight: bold;
}
.notebox.danger {
border-color: red;
font-weight: bold;
}
/*
<div class="notebox">
This is an informational note.
</div>
<div class="notebox warning">
This note shows a warning.
</div>
<div class="notebox danger">
This note shows danger!
</div>
<div class="danger">
This won't get styled — it also needs to have the notebox class
</div>
*/ - ID选择器
1
2
3
4
5
6
7#one {
background-color: yellow;
}
h1#heading {
color: rebeccapurple;
}
属性选择器
- 选择器的值是否存在
选择器 | 示例 | 描述 |
---|---|---|
[attr] | a[title] | 匹配一个名为title的属性的元素 |
[attr=value] | a[href=”https://example.com"] | 匹配href值为https://example.com的元素 |
[attr~=value] | p[class~=”special”] | 匹配class有一个值为special |
[attr|=value] | div[lang|=”zh”] | 匹配lang的属性的值为zh,或者开始为zh |
1 | li[class] { |
- 字符串匹配选择器
选择器 | 示例 | 描述 |
---|---|---|
[attr^=value] | li[class^=”box-“] | 匹配以box-开头的属性。 |
[attr$=value] | li[class$=”-box”] | 匹配以-box结尾的属性 |
[attr*=value] | li[class*=”box”] | 匹配包含box的属性 |
- 大小写敏感
1
2
3
4
5
6
7
8li[class^="a"] {
background-color: yellow;
}
/* i 要以大小写不敏感的方式匹配字符 */
li[class^="a" i] {
color: red;
}
伪类和伪元素
- 伪类:头为冒号的关键字
:
1 | 选择器 描述 |
- 伪元素开头为双冒号
::
1
2
3
4
5
6
7
8选择器 描述
::after 匹配出现在原有元素的实际内容之后的一个可样式化元素。
::before 匹配出现在原有元素的实际内容之前的一个可样式化元素。
::first-letter 匹配元素的第一个字母。
::first-line 匹配包含此伪元素的元素的第一行。
::grammar-error 匹配文档中包含了浏览器标记的语法错误的那部分。
::selection 匹配文档中被选择的那部分。
::spelling-error 匹配文档中包含了浏览器标记的拼写错误的那部分。
1 | /* 伪类和伪元素组合起来 */ |
关系选择器
后代选择器
后代选择器——典型用单个空格(” “)字符——组合两个选择器,比如,第二个选择器匹配的元素被选择,如果他们有一个祖先(父亲,父亲的父亲,父亲的父亲的父亲,等等)元素匹配第一个选择器。选择器利用后代组合符被称作后代选择器。1
body article p{}
相邻选择器
1
p+img{}
通用选择器
1
h1~p{}
子代选择器
子代关系选择器是个大于号(>),只会在选择器选中直接子元素的时候匹配。继承关系上更远的后代则不会匹配。例如,只选中作为的直接子元素的 元素:
1 | article > p |
层叠、优先级与继承
1 | /* 层叠 */ |