第 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 |
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> silver</span>; }
解释:设置元素的背景颜色。属性值是颜色值。
<span style="color: #800000;">div b </span>{<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> transparent</span>; }
解释:默认值为 transparent,为透明的意思。这样
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> silver</span>; }
解释:在
元素下可以设置整个页面的背景色。
2.background-image
值 |
说明 |
CSS 版本 |
none |
取消背景图片的设置 |
1 |
url |
通过 URL 设置背景图片 |
1 |
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(loading.gif)</span>; }
解释:url 里面是图片的路径,设置整个页面以这个图片为背景,如果图片不足以覆盖,则复制扩展。
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> none</span>; }
解释:如果多个 div 批量设置了背景,而其中某一个不需要背景,可以单独设置 none 值取消背景。
在 CSS3 中,背景图片还设置了比如线性、放射性等渐变方式。但由于支持度的问题,比如 IE9 尚未支持。我们把这些的新特性放到独立的课程中讲解。
3.background-repeat
值 |
说明 |
CSS 版本 |
repeat-x |
水平方向平铺图像 |
1 |
repeat-y |
垂直方向平铺图像 |
1 |
repeat |
水平和垂直方向同时平铺图像 |
1 |
no-repeat |
禁止平铺图像 |
1 |
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(loading.gif)</span>;<span style="color: #ff0000;"> background-repeat</span>:<span style="color: #0000ff;"> no-repeat</span>; }
解释:让背景图片只显示一个,不平铺。CSS3 还提供了两个值,由于支持度不佳,这里忽略。
4.background-position
值 |
说明 |
CSS 版本 |
top |
将背景图片定位到元素顶部 |
1 |
left |
将背景图片定位到元素左部 |
1 |
right |
将背景图片定位到元素右部 |
1 |
bottom |
将背景图片定位到元素底部 |
1 |
center |
将背景图片定位到元素中部 |
1 |
长度值 |
使用长度值偏移图片的位置 |
1 |
百分数 |
使用百分数偏移图片的位置 |
1 |
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(loading.gif)</span>;<span style="color: #ff0000;"> background-repeat</span>:<span style="color: #0000ff;"> no-repeat</span>;<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> top</span>; }
解释:将背景图片置于页面上方,如果想置于左上方则值为:top left。
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(loading.gif)</span>;<span style="color: #ff0000;"> background-repeat</span>:<span style="color: #0000ff;"> no-repeat</span>;<span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> 20px 20px</span>; }
解释:使用长度值或百分数,第一值表示左边,第二个值表示上边。
5.background-size
值 |
说明 |
CSS 版本 |
auto |
默认值,图像以本尺寸显示 |
3 |
cover |
等比例缩放图像,使图像至少覆盖容器,但有可能超出容器 |
3 |
contain |
等比例缩放图像,使其宽度、高度中较大者与容器横向或纵向重合 |
3 |
长度值 |
CSS 长度值,比如 px、em |
3 |
百分数 |
比如:100% |
3 |
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(loading.gif)</span>;<span style="color: #ff0000;"> background-size</span>:<span style="color: #0000ff;"> cover</span>; }
解释:使用 cover 相当于 100%,全屏铺面一张大图,这个值非常实用。在等比例放大缩小的过程中,可能会有背景超出,当然,这点无伤大雅。
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(loading.gif)</span>;<span style="color: #ff0000;"> background-size</span>:<span style="color: #0000ff;"> contain</span>; }
解释:使用 contain 表示,尽可能让图片完整的显示在元素内。
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(loading.gif)</span>;<span style="color: #ff0000;"> background-size</span>:<span style="color: #0000ff;"> 240px 240px</span>; }
解释:长度值的用法,分别表示长和高。
6.background-attachment
值 |
说明 |
CSS 版本 |
scroll |
默认值,背景固定在元素上,不会随着内容一起滚动 |
1 |
fixed |
背景固定在视窗上,内容滚动时背景不动 |
1 |
<span style="color: #800000;">body </span>{<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(loading.gif)</span>;<span style="color: #ff0000;"> background-attachment</span>:<span style="color: #0000ff;"> fixed</span>; }
解释:fixed 属性会导致背景产生水印效果,拖动滚动条而背景不动。
7.background-origin
值 |
说明 |
CSS 版本 |
border-box |
在元素盒子内部绘制背景 |
3 |
padding-box |
在内边距盒子内部绘制背景 |
3 |
content-box |
在内容盒子内部绘制背景 |
3 |
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 400px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 10px dashed red</span>;<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(img.png)</span>;<span style="color: #ff0000;"> background-repeat</span>:<span style="color: #0000ff;"> no-repeat</span>;<span style="color: #ff0000;"> background-origin</span>:<span style="color: #0000ff;"> content-box</span>; }
解释:设置背景起始位置。
8.background-clip
值 |
说明 |
CSS 版本 |
border-box |
在元素盒子内部裁剪背景 |
3 |
padding-box |
在内边距盒子内部裁剪背景 |
3 |
content-box |
在内容黑子内部裁剪背景 |
3 |
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 400px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 10px dashed red</span>;<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;"> background-image</span>:<span style="color: #0000ff;"> url(img.png)</span>;<span style="color: #ff0000;"> background-repeat</span>:<span style="color: #0000ff;"> no-repeat</span>;<span style="color: #ff0000;"> background-origin</span>:<span style="color: #0000ff;"> border-box</span>;<span style="color: #ff0000;"> background-clip</span>:<span style="color: #0000ff;"> padding-box</span>; }
解释:在内边距盒子内部裁剪背景。
9.background
<span style="color: #800000;">div </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 400px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 300px</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 10px dashed red</span>;<span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;"> background</span>:<span style="color: #0000ff;"> silver url(img.png) no-repeat scroll left top/100% border-box content-box</span>; }
解释:完整的简写顺序如下:
[background-color]
[background-image]
[background-repeat]
[background-attachment]
[background-position]/[background-size]
[background-origin]
[background-clip]

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

AI Hentai Generator
Generate AI Hentai for free.

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 official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

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

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

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
