background
颜色与背景
一行写完背景的所有属性。没写到的会被重置。
| 怎么写 | background: none; |
|---|---|
| 常用的值 | none, #fff url(bg.png) center/cover no-repeat |
| 继承 | 不往下传,到这个元素为止 |
| 一次设定这些 | background-color, background-image, background-position, background-size, background-repeat |
| 分类 | 颜色与背景 |
怎么看这一页
- 会继承的属性,在父元素上写一次,所有子元素都跟着用。和文字有关的属性大多如此。
- 简写属性会把你没写到的值悄悄退回默认值。只写 background: red,先前设的背景图也一并没了。
- 同一个元素上规则撞车时,选择器越具体的赢。只有具体度一样时,才轮到写在后面的赢。
- 做动效尽量用 transform 和 opacity,会顺滑得多。改 width 或 top 得重新算一遍布局。
常见问题
Q. CSS 的 background 属性是做什么的?
一行写完背景的所有属性。没写到的会被重置。
Q. background 怎么写?
写成 background: none; 这样。常用的值有 none、#fff url(bg.png) center/cover no-repeat 等。
Q. background 会传给子元素吗?
不会。需要的元素得各写各的。
Q. background 一次能设定哪些属性?
它会一并设定 background-color、background-image、background-position、background-size、background-repeat。没写到的值会退回默认值。