Home >Web Front-end >CSS Tutorial >Comprehensively review the relevant knowledge points of the CSS box model
The CSS box model is a key and difficult point in the basics of CSS, so it is often used by interviewers to examine candidates’ mastery of front-end basics. This article will comprehensively sort out the knowledge points of the CSS box model.
Let’s look at an example first: What is the total width of the div element below?
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CSS 盒模型(https://github.com/webharry/fe-interview)</title> <style> div { background-color: lightgrey; width: 200px; border: 10px solid yellow; padding: 10px; margin: 20px; } </style> </head> <body> <div>这里是盒子内的实际内容。有 10px 内间距,20px 外间距、10px 黄色边框。</div> </body> </html>
To answer this question, we first have to understand the CSS box model.
Each HTML element consists of a rectangular frame (box), called the box model. The CSS box model defines the dimensions and margins of an HTML element.
To form a box in CSS requires:
width
and height
. padding
related properties. border
related properties. margin
related properties. As shown in the figure:
The difference between the two box models is how they calculate the width and height of an element, and how they handle the element's padding, borders, and margins.
##Important: When you specify the width (width) and height (height) of a CSS element ) properties, you are just setting the width and height of the content area (content).
web front-end)
The above is the detailed content of Comprehensively review the relevant knowledge points of the CSS box model. For more information, please follow other related articles on the PHP Chinese website!