CSS layout box model properties
This time I will bring you the box model attributes of CSS layout, what are the notes of the box model attributes of CSS layout, the following is a practical case, let’s take a look.
width/height
In CSS, you can set an explicit height for any block-level element.
If the specified height is greater than the height required to display the content, the excess height will produce a visual effect, as if there is extra padding;
If the specified height is less than the height required to display the content , depending on the overflow attribute, the scroll bar needs to be set to overflow:auto.
auto
Width, height and margin can be set to auto. For block-level elements, if the width is set to auto, it will be as wide as possible. Specifically, element width = containing block width - element horizontal margin - element horizontal margin width - element horizontal padding;
If the height is set to auto, it will be as narrow as possible. In detail, element height = height just enough to contain its inline content
[Note] If the height of the containing block is not explicitly declared, the element's percentage height will be reset to auto
[Maximum and minimum width and height]
min-width | min-height
Initial value: 0
Applies to: block-level elements and replacement elements
Percentage: relative to the width (height) of the containing block
max-width | max-heightt
Initial value: none
Applies to: block-level elements and replacement elements
Percentage: relative to the width (height) of the containing block
[Note] When the minimum width (height) is greater than the maximum width (height), the value of the minimum width and height is Accurate
Padding
Compared with other attributes of the box model (such as margins with negative values that are often used in positioning), padding appears to be quite satisfactory and has no compatibility. There are no special issues
For inline elements, left padding is applied to the beginning of the element, and right padding is applied to the end of the element. Vertical padding does not affect the line height, but it does affect itself. Size, you can see it by adding the background color
[Note] The padding cannot be a negative value
padding
Initial value: Undefined
Percentage: Relative to the width of the containing block
【50%】
Block-level elements can achieve a square effect through padding:50%, because the percentage values of horizontal and vertical padding are relative to the containing block Determined by the width, often used for mobile header images
External margin
Setting the external margin will create additional white space outside the element. Usually refers to an area where other elements cannot be placed, and the background of the parent element can be seen in this area
margin
Initial value: Undefined
Applies to: all elements
Percentage: relative to the width of the containing block
[Note] For ordinary elements, the containing block is the block-level parent element. For positioned elements, the containing block is the positioning parent. . Therefore, the margin percentage of ordinary elements is relative to the width of the block-level parent element, and the margin percentage of positioned elements is relative to the width of the positioned parent.
margin can be set to negative values. Margin and width and height support auto, and Margins have very strange overlapping properties.
Introducing several key parts of margins, including overlap, auto and invalid situations
1. Overlap
[Premise]
Margin overlaps and It’s called margin merging. There are two prerequisites for this happening.
1. It only happens on block elements (excluding float, absolute, and inline-block elements)
2. It only happens on vertical elements In the direction (regardless of writing-mode)
[Classification]
Overlap of the pitch margin
1. Adjacent sibling elements
<style> p{ line-height: 2em; margin: 2px 0; background-color : lightblue; display:inline-block; width: 100%;} </style> <p>兄弟一</p> <p>兄弟二</p>
2. The margin overlap between the parent element and the first or last child element is also called margin transfer
<style> .box{ background-color: pink; height:30px;} .inner{ margin-top: 1em; background-color: lightblue;} </style> <div class="box"> <div class="inner">子级</div> </div>
In web page layout, because of margin overlap, we often use margin Use it as little as possible as a "question style". But in fact, it plays a big role,
So, we must make good use of overlap, and we can use margin-top and margin-bottom at the same time in the list items. In this way, the page structure is more robust, and the removal or positioning of the last element will not destroy the original layout
2.auto
Only width/height and margin can be set to auto .
[Why margin:auto cannot achieve vertical centering]
水平方向可以居中是因为块级元素的宽度默认是撑满父级元素的,如果给宽度设置一个固定值,而左右margin设置为auto,则可以平分剩余空间
垂直方向不可以居中是因为块级元素的高度默认是内容高度,与父级元素的高度并没有直接的关系,而上下margin设置为auto,则被重置为0
margin: 0 auto;
【为什么图片使用margin:auto不能水平居中】
图片无法水平居中,类似于块级元素无法垂直居中。因为图片的宽度width默认是自身宽度,与父元素的宽度没有直接关系。左右margin设置为auto,会被重置为0
所以,图片要水平居中,需要设置为display:block元素
3.无效情形
1、行内元素垂直margin无效
因为行内元素垂直布局主要是通过行高line-height和垂直对齐vertical-align来影响的,垂直margin并不会影响它们,所以不会影响垂直布局。而在显示方式,margin区域不会显示元素背景,所以也不会影响自身元素的显示,所以行内元素垂直margin无效。[注意]不包括inline-block
2、某些表格类元素margin无效
<thead>``<tbody>``<tfoot>``<tr>``<col>``<colgroup>``<td>``<th> 不可设置margin。
3、BFC造成的margin看似无效
左侧元素使用浮动,右侧元素使用overflow-hidden实现两栏自适应的布局时,右侧元素的margin-left值只有足够大,才能看到效果。这是因为margin-left是相对于父元素左侧,而不是图片右侧
相信看了这些案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
如何解决layer.photos()异步修改图片地址后显示异常的问题
The above is the detailed content of CSS layout box model properties. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

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.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
