9.css背景_html/css_WEB-ITnose
这节也是关于盒模型的扩展,而我个人喜欢把盒模型想象成图画。元素的尺寸调整就是画布大小的调整,边框的设置就是画框的镶嵌。但是,作为一个最终要将画作展现到美术馆(网页)的艺术家来说,仅仅是这样还是不够的,就像各种画作里面可以进行背景的描绘一样,我们也可以为自己的画设置背景。
下表总结了背景相关的一些属性:
属性 | 值 | 说明 | CSS 版本 |
background-color | 颜色 | 背景的颜色 | 1 |
background-image | none 或 url | 背景的图片 | 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.颜色,2.图片及相关设置。下面来逐一说明。
1.背景颜色
值 | 说明 | CSS 版本 |
颜色 | 设置背景颜色为指定色 | 1 |
transparent | 设置背景颜色为透明色 | 1 |
div { background-color: silver; }
解释:关于颜色的值请参考css颜色。而元素的背景颜色为透明色,是浏览器默认的,所以很少有必要专门去设置。另外,针对
元素设置颜色会设置整个页面的颜色。
2.背景图片
2.1 图片的引入
使用background-image属性可以为背景引入图片
值 | 说明 | CSS 版本 |
none | 取消背景图片的设置 | 1 |
url | 通过 URL 设置背景图片 | 1 |
body { background-image: url(loading.gif); }
解释:none值是为了解决子元素的继承问题的,而url里面放的是图片的资源地址。
2.2 图片的平铺方式
由background-repeat属性决定
值 | 说明 | CSS 版本 |
repeat-x | 水平方向平铺图像 | 1 |
repeat-y | 垂直方向平铺图像 | 1 |
repeat | 水平和垂直方向同时平铺图像 | 1 |
no-repeat | 禁止平铺图像 | 1 |
body { background-image: url(loading.gif); background-repeat: no-repeat; }
解释:所谓平铺,就是当图片小于元素的大小时,会试图复制自己而填满整个元素
2.3 图片位置的调整
使用background-position属性来调整图片在元素里的位置,一般要先禁用图片的平铺行为。
值 | 说明 | CSS 版本 |
top | 将背景图片定位到元素顶部 | 1 |
left | 将背景图片定位到元素左部 | 1 |
right | 将背景图片定位到元素右部 | 1 |
bottom | 将背景图片定位到元素底部 | 1 |
center | 将背景图片定位到元素中部 | 1 |
长度值 | 使用长度值偏移图片的位置 | 1 |
百分数 | 使用百分数偏移图片的位置 | 1 |
/*将背景图片置于页面上方,如果想置于左上方则值为:top left*/body { background-image: url(loading.gif); background-repeat: no-repeat; background-position: top; } /*使用长度值或百分数,第一值表示左边,第二个值表示上边*/body { background-image: url(loading.gif); background-repeat: no-repeat; background-position: 20px 20px; }
2.4 图片的大小
由background-size属性控制。
值 | 说明 | CSS 版本 |
auto | 默认值,图像以原尺寸显示 | 3 |
cover | 等比例缩放图像,使图像至少覆盖容器,但有可能超出容器 | 3 |
contain | 等比例缩放图像,使其宽度、高度中较大者与容器横向或纵向重合 | 3 |
长度值 | CSS 长度值,比如 px、em | 3 |
百分数 | 比如:100% | 3 |
具体说明看表格就好,这里就不举例了。
2.5 图片是否滚动
由background-attachment属性控制。
值 | 说明 | CSS 版本 |
scroll | 默认值,背景固定在元素上,不会随着内容一起滚动 | 1 |
fixed | 背景固定在视窗上,内容滚动时背景不动 | 1 |
body { background-image: url(loading.gif); background-attachment: fixed; }
解释:fixed 值会导致背景产生水印效果,拖动滚动条而背景不动。
2.6 图片在元素的那个区域显示
由background-origin控制,和图片的位置调整不同,位置调整默认是在元素内部显示,而这里的居于除了元素的内部以为,还包括内边距和边框。
值 | 说明 | CSS 版本 |
border-box | 在元素的边框绘制背景 | 3 |
padding-box | 在元素的内边距绘制背景 | 3 |
content-box | 在元素的内容部分绘制背景 | 3 |
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.7 图片的裁剪
由background-clip控制。在图片尺寸小于元素时,会有平铺的行为。而大于时,我们就要决定是否对多余的部分进行裁剪了。
值 | 说明 | CSS 版本 |
border-box | 在元素盒子内部裁剪背景 | 3 |
padding-box | 在内边距盒子内部裁剪背景 | 3 |
content-box | 在内容内部裁剪背景 | 3 |
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.8 简写形式
和很多设置一样,背景也有相应的简写形式,其顺序如下:
[background-color]
[background-image]
[background-repeat]
[background-attachment]
[background-position] / [ background-size]
[background-origin]
[background-clip];
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; }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.
