5 solutions to CSS box collapse

5 solutions to CSS box collapse

First, what is box collapse?

Elements that should be inside the parent box are outside.

Second, why does the box collapse?

When the parent element is not set to a sufficient size and the child element is set to float, the child element will jump out of the parent element's boundary (out of the document flow), especially when the parent element's height is auto and there are no other non-floating visible elements in the parent element, the parent box's height will collapse directly to zero. We call this CSS height collapse.

In the following figure, the boxes of the two child elements at the bottom are set to float left and right respectively, and the long box at the top collapses.

ex:

3. Several solutions to box collapse

The simplest, most direct and crude method is to hard-code the box size, set a fixed width and height for each box until it is suitable. The advantage of this method is that it is simple and convenient, has good compatibility, and is suitable for layouts that only change a small amount of content and do not involve box layout. The disadvantage is that it is non-adaptive, and the browser window size directly affects the user experience.

Add floats to the outer parent box to separate it from the standard document flow. This method is convenient, but it is not very friendly to the page layout and is difficult to maintain.

Add the overflow property to the parent box.

  1. overflow:auto; may cause scroll bars to appear, affecting the appearance.
  2. overflow:hidden; may cause the content to be invisible.

Introduce a clearing float at the bottom of the parent box. The simplest ones are:

<br style="clear:both;"/>

Many people solve this problem, but we do not recommend it because it introduces unnecessary redundant elements.

The after pseudo-class clears the float.

The after pseudo-element of the outer box sets the clear property.

#parent:after{
                clear: both;
                content: "";
                width: 0;
                height: 0;
                display: block;
                visibility: hidden;
            }

This is a pure CSS method to solve the box collapse caused by floating. It does not introduce any redundant elements. This method is recommended to solve CSS box collapse.

Note: Although the fifth method is good, it is not compatible with lower versions of IE. The specific solution to choose can be decided based on the actual situation.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Detailed explanation of COLLATION examples in MySQL that you may have overlooked

>>:  Introduction to HTML method of opening link files using hyperlinks

Recommend

Implementation of mounting NFS shared directory in Docker container

Previously, https://www.jb51.net/article/205922.h...

Analysis of HTTP interface testing process based on postman

I accidentally discovered a great artificial inte...

js date and time formatting method example

js date time format Convert the date and time to ...

JS removeAttribute() method to delete an attribute of an element

In JavaScript, use the removeAttribute() method o...

MySQL merges multiple rows of data based on the group_concat() function

A very useful function group_concat(), the manual...

Datagrip2020 fails to download MySQL driver

If you cannot download it by clicking downloadlao...

TypeScript uses vscode to monitor the code compilation process

Install Install ts command globally npm install -...

Implementing file content deduplication and intersection and difference in Linux

1. Data Deduplication In daily work, there may be...

Pitfalls and solutions for upgrading MySQL 5.7.23 in CentOS 7

Preface Recently, I found a pitfall in upgrading ...

Media query combined with rem layout in CSS3 to adapt to mobile screens

CSS3 syntax: (1rem = 100px for a 750px design) @m...

Web front-end development experience summary

XML files should be encoded in utf-8 as much as p...