


In-depth understanding of the meaning and principles of the CSS box model
Many front-end developers think that CSS is too simple and there is no need to spend so long learning it. In fact, this is not the case. After a front-end developer uses CSS well, the efficiency and user experience will be improved a lot. I plan to learn CSS in depth recently. , so that when encountering a problem, you will know how to deal with it and prescribe the right remedy quickly. Next, we mainly introduce the CSS box model.
What is the box model
Speaking of the box model, as front-end developers, I believe everyone has understood it. The explanation from the mdn official website is applied here:
In a document, each element is represented as a rectangular box. Determining the dimensions, properties --- aspects like its color, background, border --- and position of these boxes are the goals of the rendering engine.
In CSS, each of these rectangular boxes is described using the standard box model. This model describes the content of the space occupied by an element. Each box has four sides: margin, border, padding and content.
The official language is always so obscure, so let me take a screenshot of the chrome console to illustrate:
The outermost orange one is The margin area, the yellow one inside is the border area, the green one further inside is the padding area, and the innermost green one is the content area.
The role of the box model
Well, there seems to be nothing to say about the concept of the box model, just the picture above. Let's take a look at the impact of the box model on our layout. For example, if you want to set the size of an element to 200px, if you write the following code:
<style> .box { width: 200px; height: 100px; padding: 20px; } </style> <div class="box"> 盒子模型 </div>
As a result, when you review the element, you find that the size of the element has become 240px, and It’s not 200px anymore.
Why is this? Because by default, the setting of width, height, etc. is for the content area, so the set width: 200px is only the width of the content area, plus the size of the left and right padding, causing the overall size of the element to become larger. This is different from our understanding of the middle box in the real world. For example, when we talk about the area of a house, it does not only refer to the available area, but also includes the thickness of the walls, balconies, elevators and other spaces.
Box model and box-sizing
In order to match CSS with the real world, box-sizing comes into play at this time. Box-sizing is used to set the effect objects of width and height. There are three values, namely content-box, paading-box, and border-box. The default value is content-box. Some people may ask, why is there no margin-box? I don’t know the specific reasons. You can refer to the two reasons mentioned by teacher Zhang Xinxu in the book "CSS World":
margin-box itself does not have much value
And the standardization of margin conflict. Because the margin specification says "the background of the margin is always transparent", if there is a margin-box, what about the background?
As for why there is no margin-box, I will briefly mention it. Friends who are interested can check it out by themselves.
Best Practice
For layout convenience, some experts suggest that we set all elements to box-sizing: border-box
If you do not consider low-version browsers, you can use the following code:
*, *:before, *:after { box-sizing: border-box; }
Later, experts suggested the inheritance method, as follows:
html { box-sizing: border-box; } *, *::before, *::after { box-sizing: inherit; }
This method is called Best practice, I won’t go into the specific reasons. Since it is recommended by experts, we can put the code snippet of the second method into reset.css in the future.
The above is the detailed content of In-depth understanding of the meaning and principles of the CSS box model. For more information, please follow other related articles on the PHP Chinese website!

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

Deploying like an idiot comes down to a mismatch between the tools you use to deploy and the reward in complexity reduced versus complexity added.


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

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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),

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