Use CSS3's flex layout to implement the three-column layout. Flex layout, also known as flexible layout, provides the maximum flexibility for box models and is the preferred solution for layout. It is now supported by all modern browsers. In this case, we mainly use the default arrangement of flex container members along the main axis, and use the shorthand form of flex properties, namely flex-grow, flex-shrink, and flex-basis, to make the blocks adaptively expand.
By using calc in CSS, the length of the middle section can be dynamically calculated to achieve adaptability. Calc can be used with inline-block inline block-level elements to implement the three-column layout. Note that when using inline block-level elements, if there are line breaks in the HTML code, these blank line breaks will also be interpreted as elements, resulting in white spaces. Therefore, do not use line breaks in this part when writing the code. In addition, calc can also be used in combination with float to achieve the same result.
BFC (Block Formatting Context) is part of the visual CSS rendering of a web page. It is the area where the layout of block boxes occurs and where floating elements interact with other elements. It is a rendering area used for laying out block-level boxes and is completely independent of the outside area. It is an environment where the feature of not overlapping with floating elements is utilized to achieve a three-column layout.
<!DOCTYPEhtml><html><head><title>BFC</title><styletype="text/css">.container{height: 200px;border: 1px solid #eee;}.container div{color: #fff;height: 100%;}.container > .left{float: left;width: 200px;background-color: #19be6b;}.container > .main{display: flex;/* One of the triggering conditions for BFC: a flex or inline-flex element is a direct child of the element. */background-color: #2979ff;}.container > .right{float: right;width: 200px;background-color: #fa3534;}</style></head><body><divclass="container"><divclass="left">left</div><divclass="right">right</div><divclass="main">main</div></div></body></html>
This method is to make the left and right modules float to the left and right respectively, and set the margin value of the middle module to make the width of the middle module adapt to the content.
Currently in CSS layout solutions, grid layout can be considered as the most powerful layout solution. It can divide the webpage into grids and then use these grids to create various layouts. Grid layout is similar to Flex layout in that it can specify the position of multiple members inside the container. The difference is that Flex layout is a line layout, which can only specify the position of members relative to the line, and can be regarded as a one-dimensional layout. Grid layout divides the container into rows and columns, generating cells, and then specifies the cell in which the member is located, which can be regarded as a two-dimensional layout.