Skip to content

Commit 7a82771

Browse files
committed
Add notes.
1 parent 177e7b1 commit 7a82771

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ notes/*/*.md
3535
!notes/*/506.md
3636
!notes/*/508.md
3737
!notes/*/509.md
38+
!notes/*/603.md

doc/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ issue: 17
6060
**第六章** | **用户体验**|| [购买后阅读 <sup>↗</sup>](http://www.ituring.com.cn/tupubarticle/9398)|
6161
601| [29] 选用合适的鼠标光标|-|〃|-
6262
602| [30] 扩大可点击区域|-|〃|-
63-
603| [31] 自定义复选框|[试读](https://github.com/cssmagic/CSS-Secrets/issues/14)|〃|-
63+
603| [31] 自定义复选框|[试读](https://github.com/cssmagic/CSS-Secrets/issues/14)|〃| [免费阅读](https://github.com/cssmagic/CSS-Secrets/issues/72)
6464
604| [32] 通过阴影来弱化背景|-|〃|-
6565
605| [33] 通过模糊来弱化背景|-|〃|-
6666
606| [34] 滚动提示|-|〃|-

notes/chapter-6/603.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: "[注解] [603] 自定义复选框"
3+
issue: 72
4+
---
5+
6+
## 花絮与注解
7+
8+
#### 第 150 页 ‧ 脚注 ②
9+
10+
> ② 据 CSS 2.1 规范所述:“替换元素的特征在于,其内容超出了 CSS 格式化模型的范畴,比如图片、嵌入的文档或小应用程序等。” 原则上我们无法为替换元素添加生成性内容,尽管某些浏览器可能会允许这样做。
11+
12+
WebKit 浏览器允许我们为复选框添加伪元素。虽然 CSS 规范很可能永远也不会支持这种行为,但浏览器擅自增加这种行为也没有破坏规范,因此我估计 WebKit 的这个特性并不会很快发生变化。
13+
14+
因此,从务实的角度出发,我认为这个特性是可以利用的。一旦放开这个禁忌,我们可以实现 “更好的” 自定义复选框(待续)。
15+
16+
#### 第 151 页 ‧ 侧栏警告
17+
18+
> 在使用宽松的选择符时一定要小心。对于那些后面没有 `label` 的复选框来说(比如它是被嵌套进一个 `label` 的),使用 `input[type="checkbox"]` 选择符**也会把它们隐藏起来**,从而损害可用性。
19+
20+
在实际项目中,通常不会直接写这么宽松的选择符,因为 `input` 标签一般不会光秃秃地什么 class 都不加。
21+
22+
或者从更实际的角度来看,在实践中,我们写好的自定义复选框样式通常会抽象出来,以便在网站的各处复用。参照市面上绝大多数样式库的接口风格,我们最终选用的抽象方案往往就是先约定结构,然后把可复用的样式抽象为 class 暴露出来。这样一来,原文警告的风险就不存在了,因为我们的工作方式并不是通过选择符来把样式匹配到复选框身上,而是以添加 class 的方式精确地为需要美化的复选框(及其配套的 `label` )加样式。
23+
24+
稍微解释一下整个过程吧。比如我们为自定义复选框约定了如下结构:
25+
26+
```html
27+
<span class="my-pretty-checkbox">
28+
<input type="checkbox" id="foobar">
29+
<label for="foobar">...</label>
30+
</span>
31+
```
32+
33+
同时配套的样式是这样写的:
34+
35+
```css
36+
.my-pretty-checkbox {
37+
display: inline-block;
38+
}
39+
.my-pretty-checkbox input[type="checkbox"] {
40+
/* ... */
41+
}
42+
.my-pretty-checkbox label {
43+
/* ... */
44+
}
45+
```
46+
47+
那么接下来,如果我们需要在网站的某处放置一个美化过的复选框,直接把上面约定好的结构作为复选框来用就好了。我们可以把这段结构视作一个 “组件”,如果你不是这个组件的维护者,甚至都不需要关心它内部是如何实现的,只管拿来用就好了。

0 commit comments

Comments
 (0)