Table of Contents generated with DocToc

背景

background-color

  1. background-color: <color>
  2. background-color: #f00;
  3. background-color: rgba(255, 0, 0, 0.5);
  4. background-color: transparent; /* 默认值 */

background-image

  1. background-image: <bg-image>[, <bg-image>]*
  2. /* <bg-image> = <image> | none */
  3. background-image: url("../image/pic.png");
  4. background-image: url("../image/pic.png0"), url("../image/pic1.png");
  5. /* 多张背景图时,先引入的图片在上一层后引入则在下一层 */

NOTE:当background-colorbackground-image 共存时,背景颜色永远在最底层(于背景图片之下)。

background-repeat

background-repeat 需与背景图片数量一致。

  1. background-repeat: <repeat-style>[, <repeat-style]*
  2. <repeat-style> = repeat-x | repeat-y | [repeat | space | round | no-repeat]{1,2}
  3. /* X 轴 Y 轴 */
  4. background-repeat: no-repeat repeat;
  • space 平铺并在水平和垂直留有空隙,空隙的大小为图片均匀分布后完整覆盖显示区域的宽高
  • round 不留空隙平铺且覆盖显示区域,图标会被缩放以达到覆盖效果(缩放不一定等比)

background-attachment

当页面内容超过显示区域时,使用 local 使背景图片同页面内容一同滚动。

  1. background-attachment: <attachment>[, <attachment>]*
  2. <attachment> = scroll | fixed | local

background-position

  1. background-position: <position>[, <position>]*
  2. <position> = [left|center|right|top|bottom|<percentage>|<length>]|[left|center|right|top|bottom|<percentage>|<length>] [left|center|right|top|bottom|<percentage>|<length>] | [center |[left|right][<percentage>|<length>]?]&&[center |[left|right][<percentage>|<length>]?]
  3. /* 默认位置为 */
  4. background-position: 0 0;
  5. /* percentage 是容器与图片的百分比重合之处*/
  6. background-position: 20% 50%;
  7. /* 等同效果 */
  8. background-position: 50% 50%;
  9. background-position: center center;
  10. background-position: 0 0;
  11. background-position: left top;
  12. background-position: 100% 100%;
  13. background-position: right bottom;
  14. /* 四个值时方向只为参照物 */
  15. background-position: right 10px top 20px;

背景 - 图1

Sprite 的使用
  1. background-image: url(sprite.png)
  2. background-repeat: no-repeat;
  3. background-positon: 0 -100px

使用位置为负值将图片偏移使需要的图片位置上移并显示正确的图案。

linear-gradient

  1. linear-gradient()
  2. [[<angle> | to <side-or-corner],]? <color-step>[, <color-stop>]+
  3. <side-or-corner> = [left | right] || [top | bottom]
  4. <color-stop> = <color> [<percentage> | <length>]?
  5. background-image: linear-gradient(red, blue);
  6. background-image: linear-gradient(to top, red, blue);
  7. background-image: linear-gradient(to right bottom, red, blue);
  8. background-image: linear-gradient(0deg, red, blue);
  9. background-image: linear-gradient(45deg, red, blue);
  10. background-image: linear-gradient(red, green, blue);
  11. background-image: linear-gradient(red, green 20%, blue);

背景 - 图2

radial-gradient

  1. radial-gradient( [ circle || <length> ] [ at <position> ]? , | [ ellipse || [<length> | <percentage> ]{2}] [ at <position> ]? , | [ [ circle | ellipse ] || <extent-keyword> ] [ at <position> ]? , | at <position> , <color-stop> [ , <color-stop> ]+ )
  2. <extent-keyword> = closest-corner | closest-side | farthest-corner | farthest-side
  3. <color-stop> = <color> [ <percentage> | <length> ]?
  4. background-image: radial-gradient(cloest-side, red, blue);
  5. background-image: radial-gradient(circle, red, blue);
  6. background-image: radial-gradient(circle 100px, red, blue);
  7. background-image: radial-gradient(red, blue);
  8. background-image: radial-gradient(100px 50px, red, blue);
  9. background-image: radial-gradient(100px 50px at 0 0, red, blue);
  10. background-image: radial-gradient(red, green 20%, blue);

背景 - 图3

repeat-*-gradient

  1. background-image: repeating-linear-gradient(red, blue 20px, red 40px);
  2. background-image: repeating-radial-gradient(red, blue 20px, red 40px);

背景 - 图4

background-origin

案例模型

背景 - 图5

决定背景 (0,0) 坐标与 100% 坐标的区域。默认值为 padding-box

  1. <box>[, <box>]*
  2. <box> = border-box | padding-box | content-box
  3. background-image: url(red.png);
  4. background-repeat: no-repeat;
  5. background-origin: padding-box;
  6. background-origin: border-box;
  7. background-origin: content-box;

背景 - 图6

background-clip

裁剪背景,默认值为border-box

  1. <box>[, <box>]*
  2. <box> = border-box | padding-box | content-box
  3. background-image: url(red.png);
  4. background-repeat: no-repeat;
  5. background-clip: border-box;
  6. background-clip: padding-box;
  7. background-clip: content-box;

背景 - 图7

background-size

  1. <bg-size>[, <bg-size>]*
  2. <bg-size> = [<length> | <percentage> | auto] {1, 2} | cover | contain
  3. background-image: url(red.png);
  4. background-repeat: no-repeat;
  5. background-position: 50% 50%;
  6. background-size: auto;
  7. background-size: 20px 20px;
  8. /* % 参照物为容器*/
  9. background-size: 50% 50%;
  10. /* 尽可能小,但宽度与高度不小过容器(充满容器) */
  11. background-size: cover;
  12. /* 尽可能大,但宽度与高度不超过容器(最大完全显示)*/
  13. background-size: contain;

背景 - 图8

background shorthand

  1. [<bg-layer,]* <final-bg-layer>
  2. <bg-layer> = <bg-image> || <position> [/ <bg-size>]? || <repeat-style> || <attachment> || <box> || <box>
  3. /* 两个 <box> 第一个为 background-origin */
  4. /* 两个 <box> 第二个为 background-clip */
  5. /* 只出现一个 <box> 则即是 background-origin 也是 background-clip */
  6. <final-bg-layer> = <bg-layer> || <'background-color'>
  7. background: url(red.png) 0 0/20px 20px no-repeat, url(blue.png) 50% 50%/contain no-repeat content-box green;

背景 - 图9