search
HomeWeb Front-endCSS Tutorialwhat is css box model
what is css box modelMay 14, 2019 pm 04:05 PM
box model

css box model is also called the box model. The innermost part of the box is the actual content of the element, that is, the element content. Next to the outside of the element box is the inner margin, followed by the border, and then the outermost layer is the outer margin. This Several parts together form the box model

The box model is the core basic knowledge in html css. Only by understanding this important concept can we do better typesetting and page layout. The following is a summary of the knowledge about the CSS box model. I hope it will be helpful to everyone.

what is css box model

1. Concept of css box model

CSS css box model, also known as box model (Box Model), contains element content (content), padding, border, and margin. As shown in the picture:

what is css box model


The innermost box in the picture is the actual content of the element, that is, the element box, which is immediately outside the element box. The first is the padding, followed by the border, and then the outermost layer is the outer margin, which constitutes the box model. Usually the background display area we set is the range of content, padding, and borders. The outer margin is transparent and will not block other surrounding elements.

Then, the total width of the element box = the width of the element (element), the value of the left and right margins of the padding, the value of the left and right margins of the margin, the left and right width of the border;

Element The total height of the box = the value of the top and bottom margins of the height padding of the element, the value of the top and bottom margins of the margin + the top and bottom width of the border.

2. CSS margin merging (overlay)

When two element boxes adjacent in the upper and lower directions meet vertically, the margins will be merged, and the merged margins will The height of is equal to the higher of the two merged margins, as shown in the figure:

what is css box model

what is css box model


is relatively easy to understand, so sometimes you need to consider this factor when encountering actual situations on the page. Of course, merging margins also has meaning, as shown below:

what is css box model


It should be noted that only the block boxes in the ordinary document flow Margin merging occurs only with vertical margins. Margins between inline boxes, floated boxes, or absolutely positioned boxes are not merged.

It is also often used in css reset

* {
  margin : 0;
  padding : 0;
}

3. Introduction to box-sizing attribute

The box-sizing attribute is in the user interface attribute One, the reason why I introduce it is because this attribute is related to the box model, and it may be used in css reset.

box-sizing : content-box|border-box|inherit;

(1) content-box , the default value, allows the set width and height values ​​to be applied to the content box of the element. The width of the box only contains the content.

That is, total width = margin border padding width

(2) border-box, the set width value is actually the total width of the border padding element except margin. The width of the box includes the border padding content

That is, the total width = margin width

Many CSS frameworks will simplify the calculation method of the box model.

(3) inherit , stipulates that the value of the box-sizing attribute should be inherited from the parent element

About the use of border-box:

1. The width of the box is 100%, and you want inner spacing on both sides. It is better to use it at this time.

2. Setting the border-box globally is very good. First of all, it is intuitive, and secondly, it can save you time and time again. Plus, minus, it also has a key role - to allow boxes with borders to use percentage widths normally.

4. Applications and minor problems related to the frame model encountered in actual development.

1. Margin out of bounds (the margin-top of the first child element and the margin-bottom of the last child element are out of bounds)

Take the first The margin-top of a child element is an example:

When the parent element has no border, when setting the margin-top value of the first child element, the margin-top value will be added to the parent element. There are four solutions to this phenomenon:

(1) Add a border to the parent element (side effect)

(2) Set the padding value to the parent element (side effect)

(3) The parent element adds overflow: hidden (side effect)

(4) The parent element adds prefix content to generate. (Recommended)

Take the fourth method as an example:

.parent {     
width : 500px;     
height : 500px;     
background-color : red;       
}.parent : before {     
content : " ";     
display : table;}.child {     
width : 200px;    
 height : 200px;     
 background-color : green;     
 margin-top : 50px;}
<div class="parent">    
<div class="child"></div> </div>

2. Box model between browsers.

(1) The ul tag has a padding value by default in Mozilla, but in IE only margin has a value.

(2) The difference between the standard box model and the IE model:

The standard box model is the one introduced above, while the IE model is more like box-sizing: border-box ; Its content width also includes border and padding. The solution is: add a doctype statement to the html template.

3. Use the box model to draw triangles

<!DOCTYPE html><html>
  <head>
    <style>
        .triangle {
            width : 0;
            height: 0;
            border : 100px solid transparent;
            border-top : 100px solid blue; /*这里可以设置border的top、bottom、left、right
            四个方向的三角*/
        }
    </style>
  </head>
  <body>
    <div class="triangle"></div>
  </body></html>

页面显示结果为:


what is css box model

The above is the detailed content of what is css box model. 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
Demystifying Screen Readers: Accessible Forms & Best PracticesDemystifying Screen Readers: Accessible Forms & Best PracticesMar 08, 2025 am 09:45 AM

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

Create a JavaScript Contact Form With the Smart Forms FrameworkCreate a JavaScript Contact Form With the Smart Forms FrameworkMar 07, 2025 am 11:33 AM

This tutorial demonstrates creating professional-looking JavaScript forms using the Smart Forms framework (note: no longer available). While the framework itself is unavailable, the principles and techniques remain relevant for other form builders.

Adding Box Shadows to WordPress Blocks and ElementsAdding Box Shadows to WordPress Blocks and ElementsMar 09, 2025 pm 12:53 PM

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

Working With GraphQL CachingWorking With GraphQL CachingMar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Making Your First Custom Svelte TransitionMaking Your First Custom Svelte TransitionMar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Classy and Cool Custom CSS Scrollbars: A ShowcaseClassy and Cool Custom CSS Scrollbars: A ShowcaseMar 10, 2025 am 11:37 AM

In this article we will be diving into the world of scrollbars. I know, it doesn’t sound too glamorous, but trust me, a well-designed page goes hand-in-hand

Show, Don't TellShow, Don't TellMar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

What the Heck Are npm Commands?What the Heck Are npm Commands?Mar 15, 2025 am 11:36 AM

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool