Home Web Front-end CSS Tutorial Understanding the css box model: Understand what the css box model is in 5 minutes?

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

Sep 03, 2018 pm 05:18 PM
css box model

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;
Copy after login

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>
Copy after login

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles