what is css 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.
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:
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:
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:
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>
页面显示结果为:
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Backdrop-filter is used to apply visual effects to the content behind the elements. 1. Use backdrop-filter:blur(10px) and other syntax to achieve the frosted glass effect; 2. Supports multiple filter functions such as blur, brightness, contrast, etc. and can be superimposed; 3. It is often used in glass card design, and it is necessary to ensure that the elements overlap with the background; 4. Modern browsers have good support, and @supports can be used to provide downgrade solutions; 5. Avoid excessive blur values and frequent redrawing to optimize performance. This attribute only takes effect when there is content behind the elements.

First, use JavaScript to obtain the user system preferences and locally stored theme settings, and initialize the page theme; 1. The HTML structure contains a button to trigger topic switching; 2. CSS uses: root to define bright theme variables, .dark-mode class defines dark theme variables, and applies these variables through var(); 3. JavaScript detects prefers-color-scheme and reads localStorage to determine the initial theme; 4. Switch the dark-mode class on the html element when clicking the button, and saves the current state to localStorage; 5. All color changes are accompanied by 0.3 seconds transition animation to enhance the user

User agent stylesheets are the default CSS styles that browsers automatically apply to ensure that HTML elements that have not added custom styles are still basic readable. They affect the initial appearance of the page, but there are differences between browsers, which may lead to inconsistent display. Developers often solve this problem by resetting or standardizing styles. Use the Developer Tools' Compute or Style panel to view the default styles. Common coverage operations include clearing inner and outer margins, modifying link underscores, adjusting title sizes and unifying button styles. Understanding user agent styles can help improve cross-browser consistency and enable precise layout control.

The style of the link should distinguish different states through pseudo-classes. 1. Use a:link to set the unreached link style, 2. a:visited to set the accessed link, 3. a:hover to set the hover effect, 4. a:active to set the click-time style, 5. a:focus ensures keyboard accessibility, always follow the LVHA order to avoid style conflicts. You can improve usability and accessibility by adding padding, cursor:pointer and retaining or customizing focus outlines. You can also use border-bottom or animation underscore to ensure that the link has a good user experience and accessibility in all states.

Theaspect-ratioCSSpropertydefinesthewidth-to-heightratioofanelement,ensuringconsistentproportionsinresponsivedesigns.1.Itisapplieddirectlytoelementslikeimages,videos,orcontainersusingsyntaxsuchasaspect-ratio:16/9.2.Commonusecasesincludemaintainingres

The:emptypseudo-classselectselementswithnochildrenorcontent,includingspacesorcomments,soonlytrulyemptyelementslikematchit;1.Itcanhideemptycontainersbyusing:empty{display:none;}tocleanuplayouts;2.Itallowsaddingplaceholderstylingvia::beforeor::after,wh

vw and vh units achieve responsive design by associating element sizes with viewport width and height; 1vw is equal to 1% of viewport width, and 1vh is equal to 1% of viewport height; commonly used in full screen area, responsive fonts and elastic spacing; 1. Use 100vh or better 100dvh in the full screen area to avoid the influence of the mobile browser address bar; 2. Responsive fonts can be limited with 5vw and combined with clamp (1.5rem, 3vw, 3rem) to limit the minimum and maximum size; 3. Elastic spacing such as width:80vw, margin:5vhauto, padding:2vh3vw, can make the layout adaptable; pay attention to mobile device compatibility, accessibility and fixed width content conflicts, and it is recommended to give priority to using dvh first;

Define@keyframesbouncewith0%,100%attranslateY(0)and50%attranslateY(-20px)tocreateabasicbounce.2.Applytheanimationtoanelementusinganimation:bounce0.6sease-in-outinfiniteforsmooth,continuousmotion.3.Forrealism,use@keyframesrealistic-bouncewithscale(1.1
