Home Web Front-end HTML Tutorial 第 17 章 CSS 边框与背景[下] - 水之原

第 17 章 CSS 边框与背景[下] - 水之原

May 20, 2016 pm 04:50 PM

学习要点:

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>;
}
Copy after login

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

 

<span style="color: #800000;">div b </span>{<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> transparent</span>;
}
Copy after login

解释:默认值为 transparent,为透明的意思。这样

内部的元素就会继承
的背景色。一般来说这个属性使用频率很低,原因是不需要改变色彩时就默认,需要改变色彩时又是颜色值。

 

<span style="color: #800000;">body </span>{<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> silver</span>;
}
Copy after login

解释:在

元素下可以设置整个页面的背景色。

 

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>;
}
Copy after login

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

 

<span style="color: #800000;">div </span>{<span style="color: #ff0000;">
    background-image</span>:<span style="color: #0000ff;"> none</span>;
}
Copy after login

解释:如果多个 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>;
}
Copy after login

解释:让背景图片只显示一个,不平铺。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>;
}
Copy after login

解释:将背景图片置于页面上方,如果想置于左上方则值为: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>;
}
Copy after login

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

 

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>;
}
Copy after login

解释:使用 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>;
}
Copy after login

解释:使用 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>;
}
Copy after login

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

 

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>;
}
Copy after login

解释: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>;
}
Copy after login

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

 

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>;
}
Copy after login

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

 

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>;
}
Copy after login

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

[background-color]
[background-image]
[background-repeat]
[background-attachment]
[background-position]/[background-size]
[background-origin]
[background-clip]

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

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,

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

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

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

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

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

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

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

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

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

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

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; 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

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

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

See all articles