search
HomeWeb Front-endCSS TutorialHTML and CSS box model


This time I will bring you the box model of HTML and CSS. What are the precautions when using the box model of HTML and CSS? The following is a practical case. Get up and take a look.

1. Border (Part 1)

1. What is the border?

The border is the line surrounding the width and height of the label

2. Format of border attributes

2.1 Continuous writing (setting borders on four sides at the same time)
border: width of border, style of border, color of border;

Shortcut key:

bd+ border: 1px solid #000;

Note:

1. The color attribute can be omitted in the continuous writing format, and the default is black after omitting it
2.In the continuous writing format The style cannot be omitted. If you omit it, you will not see the border.
3. In the continuous writing format, the width can be omitted, but you can still see the border after omitting it.

2.2 Continuous writing (set the borders of the four sides separately)

border-top: The width of the border, the style of the border, and the color of the border;
border-right: The width of the border, the style of the border, and the color of the border;
border-bottom: The width of the border, the style of the border, and the color of the border ;
border-left: The width of the border, the style of the border, the color of the border;

Shortcut keys:

bt+ border-top: 1px solid #000;
br+
bb+
bl+

2. Border (Part 2)

2.3 Continuous writing (set borders on four sides respectively) Set borders based on three elements

border-width: top right, bottom left;
border-style: top right, bottom left;
border-color: top right, bottom left;

Notes:

1. The values ​​of these three attributes are assigned clockwise, that is, they are assigned according to top, right, bottom, and left, instead of up, down, left, and right in daily life

2. When the values ​​​​of these three attributes are omitted The rules of

2.1 Top right, bottom left > top right, bottom > The value on the left is the same as the value on the right

2.2 Top right, bottom left > top right > The value on the left is the same as The value on the right is the same, the value on the bottom is the same as the top

2.3 The value of top, right, bottom left> top>, the value of bottom right and left is the same as the top

3. Non-concatenation (direction + element)
border-left-width: 20px;
border-left-style: double;
border-left-color: pink;

3. Padding

1.What is padding?

The distance between the border and the content is padding

2.Format

2.1Non-sequential writing
padding-top: ;
padding-right: ;
padding-bottom: ;
padding-left: ;

2.2 Continuous writing

padding: top, right, bottom, left;

3. Rules when attribute values ​​are omitted

3.1 Top, right, bottom, left> top, right, bottom> left The value is the same as the one on the right
3.2 Top right, bottom left> Top right> The value on the left is the same as the right, The value on the bottom is the same as the top
3.3 Top right, bottom left> Top> Right, bottom left The value is the same as above

Note:

1.After setting the padding for the label, the width and height occupied by the label will change
2.Set the padding for the label After that, the inner margin will also have the background color

4. Margin

1. What is the margin?

Between tags and tags The distance is the outer margin

2. Format

2.1 Non-continuous writing
margin-top: ;
margin-right: ;
margin-bottom: ;
margin-left: ;

2.2 Continuous writing
margin: top, right, bottom, left;

3. Rules for omitting the values ​​of these three attributes

3.1 Top right, bottom left > Top right, bottom > The value on the left is the same as the one on the right
3.2 Top right, bottom left > Top right > The value on the left is the same as the value on the right. Same as above
3.3 Top, right, bottom, left> top> The value of right, bottom and left is the same as above

Note:

The part of the outer margin has no background color

5. Margin merging phenomenon

1. In the vertical direction of the default layout, the margins will not overlap by default, and merging will occur. Who If the margins are relatively large, just listen to whoever says it;
2. Merging phenomenon will not occur in the horizontal direction;

HTML and CSS box model

Margin merging phenomenon (collapse)

6. CSS box model

1. What is the CSS box model?
The CSS box model is just a metaphor, all in HTML The tags are all boxes

Conclusion

1. All tags in HTML can be set
Width/height == specifies the area where content can be stored
Padding = = Filling
Border == Mobile phone box itself
Margin == The gap between the box and the box

7. The width and height of the box model

1. The width and height of the content

are the width and height set through the width/height attributes

2. The width and height of the element

Width = left border + Left padding + width + right padding + right border
Height can be proven in the same way

3. The width and height of the element space

Width = left outer margin + left border + left inner margin + width + right inner margin + right border + right outer margin
Highly similar proof

8. Box box-sizing attribute

1. There is a new box-sizing attribute in CSS3. This attribute can ensure that the width and height of the box element remain unchanged after we add padding and border to the box

2. How the box-sizing attribute ensures that after adding padding and border, the width and height of the box element remain unchanged. The principle we learned earlier is the same. After adding padding and border, we must ensure that the width and height of the box element remain unchanged. , then you must subtract the width and height of part of the content

3.box-sizing value

3.1content-box (default value)
The width and height of the element = border+ Padding + content width and height

3.2border-box (the width and height of the element will not change)
The width and height of the element = the width and height of width/height

9 . Center the box

Notes (1)

1. If the two boxes are in a nested relationship, then if you set the margin at the top of the inner box, the outer box will also Pushed down
2. If the outer box does not want to be set down together, then you can add a border attribute to the outer box or set the overflow: hidden; attribute
3. In enterprise development, generally if you need to control the distance between nested relationship boxes, you should first consider padding, and then consider margin
margin is essentially used to control sibling relationships.

Notes on the gap between (2)

1. In nested boxes, we can use margin: 0 auto; to make the inner box be in the outer box Middle horizontal centering
2.margin: 0 auto; Only valid for horizontal direction, invalid for vertical direction; Centering can only be calculated by pixels in vertical direction;

10. text-align: The difference between center; and margin:0 auto;

text-align: center;Function

Set the text/picture stored in the box to be horizontally centered

margin:0 auto;Function

Let the box center itself horizontally

11. Clear the default margins

1. Why should you clear the default margins (outer margins and padding)

In enterprise development, in order to better control the position of the box and calculate the width and height of the box, etc., so in enterprise development, the first thing before writing code is to clear the default margins

2.How to clear the default margins

Format

*{margin: 0;padding: 0;}
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{            margin:0;padding:0}

3.Attention

Wildcard selector Will search for (traverse) all the tags in the current interface, so the performance is not good

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

CSS background and sprites

Three major features of CSS you must know

CSS usage skills you don’t know

The above is the detailed content of HTML and 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
@keyframes vs CSS Transitions: What is the difference?@keyframes vs CSS Transitions: What is the difference?May 14, 2025 am 12:01 AM

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

Using Pages CMS for Static Site Content ManagementUsing Pages CMS for Static Site Content ManagementMay 13, 2025 am 09:24 AM

I know, I know: there are a ton of content management system options available, and while I've tested several, none have really been the one, y'know? Weird pricing models, difficult customization, some even end up becoming a whole &

The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

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 Article

Hot 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.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool