不管你要用HTML显示什么内容,统统都会被放到一个长方形的盒子里。但浏览器呈现给我们的是一个二维世界,二维世界里的盒子跟我们这个世界的盒子不一样。二维世界里的盒子虽然只有宽和高两个维度,但从能装东西这个角度来说,那确实是盒子。
你在浏览器界面上看到的所有HTML元素都是装在盒子里的,不过你可能看不到盒子,要仔细观察才能发现一些蛛丝马迹。而且这个盒子还不止一层,足足有三层,大概是这个样子的:
图一:盒子模型示意图 (源自 Introduction to the CSS box model )
你要显示的内容在最里面,然后外面有一个 padding 层,接下来是 border 层,最外一层叫 margin 。这三层里有可能可以直接看到的只有 border ,其它两层虽然你可以感觉到它们的存在,但从视觉上来说,它们是完全透明的。
在继续往下看之前,先考虑两个问题:
- 为什么要搞个盒子?
- 这个盒子关我啥事儿?
第一个问题说来话长,如果确实像知道,还是去找制定CSS标准的那帮人,或者做浏览器的聊聊吧。说实话我也不知道为啥。
第二个问题,先假设你要买一套400*100的房子(像素,不是米!),规格说明是这样的:
div { width: 400px; height: 100px; border: 6px solid #949599; margin: 20px; padding: 20px; }
你一看 width 和 height 没问题,就一蹦一跳地去交钱了。结果算账的时候,你发现开发商要跟你收492*192的钱,你是不是想问候他老母?但还没等你开口,他就拿出设计图:
图二、盒子模型设计图(源自 Opening the Box Model )
然后甩给你两个公式:
- width = margin-right + border-right + padding-right + width + padding-left + border-left + margin-left
- height = margin-top + border-top + padding-top + height + padding-bottom + border-bottom + margin-bottom
并且还没等你看明白,就拿出计算器一通按,然后笑眯眯地看着你,请你自己看492和192是怎么来的。
- Width: 492 = 20 + 6 + 20 + 400 + 20 + 6 + 20
- Height: 192 = 20 + 6 + 20 + 100 + 20 + 6 + 20
简单来说,宽度要把所有横向的数都加起来,不能漏掉 margin 、 border 和 padding 的 left 和 right ;高度要把竖向的所有数都加起来,不能漏掉 margin 、 border 和 padding 的 top 和 bottom 。
所以说,你本来只想放一个400px*100px的图片,但浏览器却要给你492px*192px,如果你毫无心理准备,肯定会有点小激动吧。因此在设计页面时,一定要掌握盒子模型,要知道还有公摊面积这回事儿。不过这里只是简单介绍了一下,要深入了解,可以到两张图片的出处去看一下。
另外,chrome浏览器的开发者工具可以查看所有页面元素的盒子。 这里还有个小程序 ,可以调节几个关键的属性值,你可以直观地感受下。

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

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.