第 17 章 CSS 边框与背景[下]

学习要点:

1.设置背景

主讲教师:李炎恢

本章主要探讨 HTML5 中 CSS 边框和背景,通过边框和背景的样式设置,给元素增加更丰富的外观。

一.设置背景

盒模型的尺寸可以通过两种方式实现可见性,一种就是之前的边框,还有一种就是背景。 CSS 背景设置的样式表如下:

属性说明CSS 版本
background-color背景的颜色1
background-image背景的图片1/3
background-repeat背景图片的样式1/3
background-size背景图像的尺寸3
background-position背景图像的位置1
background-attachment背景图片的滚动1/3
background-clip背景图片的裁剪3
background-origin背景图片起始点3
background背景图片简写方式1

1.background-color

说明CSS 版本
颜色设置背景颜色为指定色1
transparent设置背景颜色为透明色1
  1. div { background-color: silver;
  2. }

解释:设置元素的背景颜色。属性值是颜色值。

  1. div b { background-color: transparent;
  2. }

解释:默认值为 transparent,为透明的意思。这样<div>内部的元素就会继承<div>的背景色。一般来说这个属性使用频率很低,原因是不需要改变色彩时就默认,需要改变色彩时又是颜色值。

  1. body { background-color: silver;
  2. }

解释:在<body>元素下可以设置整个页面的背景色。

2.background-image

说明CSS 版本
none取消背景图片的设置1
url通过 URL 设置背景图片1
  1. body { background-image: url(loading.gif);
  2. }

解释:url 里面是图片的路径,设置整个页面以这个图片为背景,如果图片不足以覆盖,则复制扩展。

  1. div { background-image: none;
  2. }

解释:如果多个 div 批量设置了背景,而其中某一个不需要背景,可以单独设置 none 值取消背景。

在 CSS3 中,背景图片还设置了比如线性、放射性等渐变方式。但由于支持度的问题,比如 IE9 尚未支持。我们把这些的新特性放到独立的课程中讲解。

3.background-repeat

说明CSS 版本
repeat-x水平方向平铺图像1
repeat-y垂直方向平铺图像1
repeat水平和垂直方向同时平铺图像1
no-repeat禁止平铺图像1
  1. body { background-image: url(loading.gif); background-repeat: no-repeat;
  2. }

解释:让背景图片只显示一个,不平铺。CSS3 还提供了两个值,由于支持度不佳,这里忽略。

4.background-position

说明CSS 版本
top将背景图片定位到元素顶部1
left将背景图片定位到元素左部1
right将背景图片定位到元素右部1
bottom将背景图片定位到元素底部1
center将背景图片定位到元素中部1
长度值使用长度值偏移图片的位置1
百分数使用百分数偏移图片的位置1
  1. body { background-image: url(loading.gif); background-repeat: no-repeat; background-position: top;
  2. }

解释:将背景图片置于页面上方,如果想置于左上方则值为:top left。

  1. body { background-image: url(loading.gif); background-repeat: no-repeat; background-position: 20px 20px;
  2. }

解释:使用长度值或百分数,第一值表示左边,第二个值表示上边。

5.background-size

说明CSS 版本
auto默认值,图像以本尺寸显示3
cover等比例缩放图像,使图像至少覆盖容器,但有可能超出容器3
contain等比例缩放图像,使其宽度、高度中较大者与容器横向或纵向重合3
长度值CSS 长度值,比如 px、em3
百分数比如:100%3
  1. body { background-image: url(loading.gif); background-size: cover;
  2. }

解释:使用 cover 相当于 100%,全屏铺面一张大图,这个值非常实用。在等比例放大缩小的过程中,可能会有背景超出,当然,这点无伤大雅。

  1. div { background-image: url(loading.gif); background-size: contain;
  2. }

解释:使用 contain 表示,尽可能让图片完整的显示在元素内。

  1. body { background-image: url(loading.gif); background-size: 240px 240px;
  2. }

解释:长度值的用法,分别表示长和高。

6.background-attachment

说明CSS 版本
scroll默认值,背景固定在元素上,不会随着内容一起滚动1
fixed背景固定在视窗上,内容滚动时背景不动1
  1. body { background-image: url(loading.gif); background-attachment: fixed;
  2. }

解释:fixed 属性会导致背景产生水印效果,拖动滚动条而背景不动。

7.background-origin

说明CSS 版本
border-box在元素盒子内部绘制背景3
padding-box在内边距盒子内部绘制背景3
content-box在内容盒子内部绘制背景3
  1. div { width: 400px; height: 300px; border: 10px dashed red; padding: 50px; background-image: url(img.png); background-repeat: no-repeat; background-origin: content-box;
  2. }

解释:设置背景起始位置。

8.background-clip

说明CSS 版本
border-box在元素盒子内部裁剪背景3
padding-box在内边距盒子内部裁剪背景3
content-box在内容黑子内部裁剪背景3
  1. div { width: 400px; height: 300px; border: 10px dashed red; padding: 50px; background-image: url(img.png); background-repeat: no-repeat; background-origin: border-box; background-clip: padding-box;
  2. }

解释:在内边距盒子内部裁剪背景。

9.background

  1. div { width: 400px; height: 300px; border: 10px dashed red; padding: 50px; background: silver url(img.png) no-repeat scroll left top/100% border-box content-box;
  2. }

解释:完整的简写顺序如下:

[background-color]

[background-image]

[background-repeat]

[background-attachment]

[background-position]/[background-size]

[background-origin]

[background-clip]