这节也是关于盒模型的扩展,而我个人喜欢把盒模型想象成图画。元素的尺寸调整就是画布大小的调整,边框的设置就是画框的镶嵌。但是,作为一个最终要将画作展现到美术馆(网页)的艺术家来说,仅仅是这样还是不够的,就像各种画作里面可以进行背景的描绘一样,我们也可以为自己的画设置背景。
下表总结了背景相关的一些属性:
属性 | 值 | 说明 | 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; }

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment