A brief discussion of the interesting box model of CSS3 box-sizing property

A brief discussion of the interesting box model of CSS3 box-sizing property

Everyone must know the composition of the box model, from the inside to the outside: content, padding, border, margin.

There are two standard box models, one is the standard model and the other is the IE model.

From the above two pictures, it is not difficult to see that in the standard model, the width and height of the box model are just the width and height of the content.

In the IE model, the width and height of the box model is the total width and height of the content + padding + border.

How to set two models in css

The CSS3 property box-sizing (default value: content-box) is used here.

/* Standard model */ 
 box-sizing:content-box;
  /*IE model*/
 box-sizing:border-box;

content-box: This is the behavior for width and height specified by CSS 2.1. Specifying the width and height of an element (min/max properties) applies to the width and height of the box. The padding and border of an element are laid out and drawn except for the specified width and height

border-box: Specify width and height (minimum/maximum properties) to determine the element's border box. That is, specifying the width and height of an element includes specifying padding and borders. The content width and height are calculated by subtracting the width of the border and padding on both sides from the specified "width" and "height" properties.

Test reference cases

The ideal effect and code are as follows:

.input { width: 146px; height: 36px; line-height: 36px; background: transparent; border: 2px solid #0D349A; color: #bdbdbd; padding-left: 10px; padding-right: 30px; font-size: 14px;
box-sizing:content-box; (default optional)} 

When applied in a project using the bootstrap framework, I found that the default box-sizing: border-box; in the bootstrap.min.css style would interfere with the width and height of the search box.

* { -webkit-box-sizing: border-box; 
-moz-box-sizing: border-box;
 box-sizing: border-box;}

This property causes the page to appear like this:

.input { width: 146px; height:
 36px;
 line-height: 36px; 
background: transparent; 
border: 2px solid #0D349A;
 color: #bdbdbd; padding-left: 10px;
 padding-right: 30px; 
font-size: 14px; box-sizing:border-box;} 

At this time, if you want to achieve the ideal effect, you must adjust the style to:

.input { width: 190px;
 height: 40px; line-height: 
40px; background: transparent; 
border: 2px solid #0D349A;
 color: #bdbdbd; padding-left: 10px; 
padding-right: 30px;
 font-size: 14px; 
box-sizing:border-box;}

PS Tips: When a container width is defined as width:100%;, if padding or border is added, it will overflow the parent container and expand outwards.

If you use this style and specify box-sizing: border-box; then the padding and border will no longer overflow, but shrink inwards. This effect feels very practical.

Summarize

The above is the editor’s introduction to the interesting box model of CSS3 box-sizing attribute. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time!

<<:  Detailed process of drawing three-dimensional arrow lines using three.js

>>:  docker run -v mounts data volumes abnormally, and the container status is always restarting

Recommend

Timeline implementation method based on ccs3

In web projects we often use the timeline control...

Vue.js implements tab switching and color change operation explanation

When implementing this function, the method I bor...

CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

The specific steps of installing mysql5.7.18 unde...

No-nonsense quick start React routing development

Install Enter the following command to install it...

Vue project implements left swipe delete function (complete code)

Achieve results The code is as follows html <t...

Using HTML web page examples to explain the meaning of the head area code

Use examples to familiarize yourself with the mean...

Shell script settings to prevent brute force ssh

The shell script sets access control, and the IP ...

How to lock a virtual console session on Linux

When you are working on a shared system, you prob...

Implementation of Docker deployment of SQL Server 2019 Always On cluster

Table of contents Docker deployment Always on clu...

How to configure path alias for react scaffolding

The react version when writing this article is 16...

MySQL 8.0.20 installation and configuration method graphic tutorial

MySQL download and installation (version 8.0.20) ...

Tomcat8 uses cronolog to split Catalina.Out logs

background If the catalina.out log file generated...

Using CSS3 and JavaScript to develop web color picker example code

The web color picker function in this example use...

The perfect solution for forgetting the password in mysql8.0.19

Recommended reading: MySQL 8.0.19 supports accoun...

Solution to the Chinese garbled characters problem in MySQL under Ubuntu

Find the problem I have been learning Django rece...