box-sizing property

1. Function

  Define how to calculate the total width and total height of an element, whether the main settings need to add padding ( padding) and borders, etc.

2. Attribute value

  • content-box

​ By default, the width of any border and inner border will be added to the width of the last drawn element

  • border-box

    The set border and padding values ​​are included in width

3. Examples

     <body>
        <div class="container"></div>
     </body>
        * {
    
    
            margin: 0;
            padding: 0;
        }

        body {
    
    
            border: 1px solid rgb(192, 192, 235);
            margin: 10px;
        }

        .container {
    
    
            width: 100px;
            height: 100px;
            background: pink;
            margin: 10px;
            padding: 10px;
            border: 1px solid black;
        }
  • content-box
      * {
    
    
            box-sizing: content-box;
        }

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-XwuhHy4x-1650077940119)(image-20220416105318659.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-MXUa2xC6-1650077940121)(image-20220416105457842.png)]

At this time, the width of the box content area is the set width, and the actual width is:width+padding*2+border*2+margin*2

  • border-box
      * {
    
    
            box-sizing: border-box;
        }
       /* 此时的盒子比上面的盒子要小 */

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-yKhqp7mZ-1650077940121)(image-20220416104952115.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-Ci0ifSdy-1650077940122)(image-20220416105126344.png)]

As can be seen from the above figure content + padding + border + margin =width, the actual width of the box is the set width

Supongo que te gusta

Origin blog.csdn.net/m0_52900946/article/details/124209779
Recomendado
Clasificación