search
HomeWeb Front-endCSS TutorialUnderstanding the css box model: Understand what the css box model is in 5 minutes?

What is the css box model? After all, you can often see the css box model when learning css. Therefore, in the following article, php Chinese website will talk to you about the concept of the css box model and your understanding of the css box model.

First of all, let’s take a look at what the css box model is?

From Baidu Encyclopedia, we can know that the attribute names commonly heard in web design are: content, padding, border, margin, CSS box Patterns all have these properties. We can understand these attributes by transferring them to the boxes (boxes) in our daily life. The boxes we see in our daily life are boxes that can hold things and also have these attributes, so it is called the box model. As shown below:

Understanding the css box model: Understand what the css box model is in 5 minutes?

The CSS box model is a thinking model used by CSS technology that is often used in web design.

So, after knowing what the css box model is, how should we understand the css box model?

Understanding of css box model

We can understand the css box model as a box in daily life.

Content is what is contained in the box. It has height (height) and width (width). It can be a picture, text or small box nesting. In reality, the content cannot be larger than the box, and the content cannot be larger than the box. The box will burst, but in CSS, the box is elastic. If the content is too large, the box will be stretched, but it will not damage the box.

Padding is filling, just like we fill it with something, such as foam or plastic film, in order to ensure that the things in the box are not damaged. The filling can be large or small, soft or hard, and it is reflected in the web page It’s the size of the padding.

Border is the outer border, because the border has size and color attributes, which is equivalent to the thickness of the box and its color or material.

Margin is the distance between our box and other boxes or other things. If there are many boxes, margin is the direct distance between the boxes, which allows for ventilation, beauty and easy removal.

There are two types of css box models, one is the W3C box model, which is the standard model, and the other is the IE box model.

How to set up the two css box models:

/* 标准模型 */
box-sizing:content-box; 
/*IE模型*/
box-sizing:border-box;

The two box models of css can be represented by the following figures:

Standard model:

Understanding the css box model: Understand what the css box model is in 5 minutes?

IE box model:

Understanding the css box model: Understand what the css box model is in 5 minutes?

We can clearly see from the above figure that the width or height of the IE box model is the width and height of the content (content), and the width or height of the W3C box model, the standard model, is the content (content). ) padding (inner margin) border (border)

Let’s take an example: the width and height of a div are 105px, the inner margin is 10px, the border is 5px, and the outer margin is 30px. The total width and total height of the div displayed under the W3C box model (including margins, borders, padding, and content) are 105 15 5 30 = 155px. The total width and total height of the div displayed under the IE box model (including margins, borders, padding, and content) are 105 30 = 135px.

<style>
  .content {background: #eee; height: auto;border: 1px solid black;}
  .div {width: 105px;height: 105px;margin: 30px;padding: 15px;border: 5px solid black;}
  .div-01 {background: green;}
  .div-02 {background: pink;box-sizing: border-box;}
</style>
<div class="content">
  <div class="div div-01">W3C盒模型</div>
  <div class="div div-02">IE盒模型</div>
</div>

The effect is as follows:

Understanding the css box model: Understand what the css box model is in 5 minutes?

#The obvious difference is that if the width of the element (width) is certain, the width of the W3C box model (width) does not include padding and borders, but is included in the IE box model.

Of course, after seeing this, the content in the css box model must not be fully grasped. Next, you can check out the css video tutorial column on the PHP Chinese website and css3 video tutorial column, the video in it talks about the css3 box model in great detail.

Related recommendations:

Introduction to CSS box model

Let’s talk about css box model_html/css_WEB-ITnose

The above is the detailed content of Understanding the css box model: Understand what the css box model is in 5 minutes?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.