Home Web Front-end HTML Tutorial A brief explanation of box-sizing in css (standard box model and weird box model)_html/css_WEB-ITnose

A brief explanation of box-sizing in css (standard box model and weird box model)_html/css_WEB-ITnose

Jun 24, 2016 am 11:44 AM

When I was writing a program to do layout today, I encountered a problem about the box model in CSS. I asked Baidu to explain this attribute. I have a big brain, but the text is too confusing to understand. What should I do? So I wrote a program test myself. Well, it was good. Once I saw the effect, I suddenly understood. Here is a brief explanation, which may be superficial. But I hope it can help some front-end friends.

First let’s talk about the value of box-sizing. There are three optional values ​​​​for box-sizing, inheirt, content-box (standard box model) and border-box (ie6 and other non-standard browsers) Weird box model);

First let’s talk about the first value content-box, which is the default value of box-sizing. It means: the width of the space actually occupied by the element = the width (width) we set, the border and the inner spacing. Suppose we set the width of a div to 100px. If we set the border and spacing for this div, the border and spacing will expand outward. causing the actual width of the actual div to be greater than 100px. The above code and pictures are clear at a glance.

First define a div with a width and height of 100px, set box-sizing to content-box, and set the background to red. To highlight it we put it on a blue background;

<div id="bx1" style="width:300px;height:300px;background:blue;">        <div id="bx2" style="width:100px;height:100px;background:red;margin:0px auto;box-sizing:content-box;"></div></div>
Copy after login

It is displayed as follows:

For ease of comparison we add The only difference between a new div and the previous div is the addition of a 10px yellow border;

<div id="bx1" style="width:300px;height:300px;background:blue;">        <div id="bx2" style="width:100px;height:100px;background:red;margin:0px auto;box-sizing:content-box;"></div>        <div id="bx3" style="width:100px;height:100px;background:red;margin:10px auto; box-sizing:content-box;border:10px solid yellow"></div></div>
Copy after login

The result is as shown: the border is expanded outside the div.

We are adding a 10-pixel inner margin to the newly added div. As a result, the inner margin also expands outward. Why is it said that the inner margin expands outward? You can pay attention to the position of "hello".

#bx3{padding:10px;}
Copy after login
Copy after login

 

Okay, now that we’ve finished talking about one attribute, let’s start with another attribute, border-box. Modify the box-sizing of the original div to border-box.

The border-box expands inward. If you set box-sizing to border-box, the width of the space actually occupied by the element is equal to the width we set. If you set the border and spacing, the border and spacing will expand inward. Let’s go through the original example again. (Please forgive me for being obsessive-compulsive and I still pursue perfection)

<div id="bx1" style="width:300px;height:300px;background:blue;">        <div id="bx2" style="width:100px;height:100px;background:red;margin:0px auto;color:white;box-sizing:border-box;">hello</div></div>
Copy after login

Initial image without borders and spacing:

Now Add a new div for comparison. The only difference from the original div is the addition of a 10px wide yellow border;

<div id="bx1" style="width:300px;height:300px;background:blue;">        <div id="bx2" style="width:100px;height:100px;background:red;margin:0px auto;color:white;box-sizing:border-box;">hello</div>        <div id="bx3" style="width:100px;height:100px;background:red;margin:0px auto;color:white;box-sizing:border-box;border:10px solid yellow">hello</div></div>
Copy after login

The result is as shown: the border expands inward

Then we add 10px padding to the div;

#bx3{padding:10px;}
Copy after login
Copy after login

The result is as shown: the padding expands inward, pay attention to " hello" position.

I think everyone should be clear about these two attributes.

Now to summarize, box-sizing has three values: context-box, border-box and inherit. Content-box is the default value of box-sizing, which means the actual space width occupied by the element = what we set Width (width) is the inner spacing of the border. After we set the width of the div, we then add the border and spacing. The border and spacing expand outward. The width of the space actually occupied by the border-box element is equal to the width we set. If the border and spacing are set, the border and spacing will expand inward. As for the inherit attribute, everyone should know that it inherits whatever attribute it has.

It was originally a very small thing, but after so long, I finally asked the world to turn out perfectionists and obsessive-compulsive disorder patients, we are innocent

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)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

See all articles