Method: 1. Set "dislpay:inline-block" on both box elements; 2. Set both box elements to float; 3. The left fixed-width element is floated, and the right element is set to margin-left. And the value is greater than the width of the fixed-width element; 4. Floating BFC; 5. Absolute positioning margin-left, etc.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
There are two types of two-column layouts. One is fixed width on the left and adaptive on the right. One is that both columns are adaptive (that is, the left width is determined by the child elements, and the right side fills in the remaining space). CSS interview questions are common test questions and are also skills that a front-end development engineer must master. The implementation methods will be introduced below.
Principle: Set dislpay:inline-block on both elements. To eliminate the influence of HTML spaces, the font-size of the parent element needs to be set to 0, and the width of the adaptive element on the right is calculated using the calc function. If the heights of the two elements are different, you can set vertical-align:top adjustment for the elements.
Disadvantages: Since the font-size of the parent element is set to 0, the text in the child element will not be displayed
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> *{ padding: 0; margin: 0; } .box{ height: 600px; width: 100%; font-size:0; } .left{ display: inline-block; width: 100px; height: 200px; background-color: red; vertical-align: top; } .right{ display: inline-block; width: calc(100% - 100px); height: 400px; background-color: blue; vertical-align: top; } </style> </head> <body> <div class="box"> <div class="left"> <span>1234</span> </div> <div class="right"> <span>1234</span> </div> </div> </body> </html>
Principle: Two elements are set to float, and the width of the adaptive element on the right is calculated using the calc function
Disadvantages: Parent Elements need to be cleared from floating
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> *{ padding: 0; margin: 0; } .box{ height: 600px; width: 100%; } .left{ float: left; width: 100px; height: 200px; background-color: red; } .right{ float: left; width: calc(100% - 100px); height: 400px; background-color: blue; } </style> </head> <body> <div class="box"> <div class="left"> <span> 123adadadddddddddddddddddddddddddddddddddddddddd </span> </div> <div class="right"></div> </div> </body> </html>
## Principle: Fixed-width elements on the left are floating, and elements on the right are floating The adaptive element can set the margin-left value to be greater than the width of the fixed-width element
Disadvantages: The parent element needs to clear the float
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> *{ padding: 0; margin: 0; } .box{ height: 600px; width: 100%; } .left{ float: left; width: 100px; height: 200px; background-color: red; } .right{ margin-left: 100px; height: 400px; background-color: blue; } </style> </head> <body> <div class="box"> <div class="left"> <p>1234</p> </div> <div class="right"> <p>1234</p> </div> </div> </body> </html>
Principle: The parent element sets overflow:hidden, the left fixed-width element floats, and the right adaptive element sets overflow:auto to create BFC
Disadvantages: If the content of the left element exceeds the set width, it will overlap the right element
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> *{ padding: 0; margin: 0; } .box{ height: 600px; width: 100%; overflow: hidden; } .left{ float: left; width: 100px; height: 200px; background-color: red; } .right{ overflow: auto; height: 400px; background-color: blue; } </style> </head> <body> <div class="box"> <div class="left">111111111111111111111111</div> <div class="right">111111111111111111111111111111111111111111111</div> </div> <div class="right"></div> </body> </html>
Principle: The parent element is positioned relatively, the left element is positioned absolutely, and the right adaptive element sets the margin-left value to be greater than the width of the fixed-width element
Disadvantages: The parent element is set to relative positioning
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> *{ padding: 0; margin: 0; } .box{ height: 600px; width: 100%; position: relative; } .left{ position: absolute; width: 100px; height: 200px; background-color: red; } .right{ margin-left: 100px; height: 400px; background-color: blue; } </style> </head> <body> <div class="box"> <div class="left"></div> <div class="right"></div> </div> </body> </html>
Principle: The parent element sets display: flex, and the adaptive element sets flex: 1
Disadvantages: There are compatibility issues, and it is not supported below IE10
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> *{ padding: 0; margin: 0; } .box{ height: 600px; width: 100%; display: flex; } .left{ width: 100px; height: 200px; background-color: red; } .right{ flex: 1; height: 400px; background-color: blue; } </style> </head> <body> <div class="box"> <div class="left"></div> <div class="right"></div> </div> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style> .parent{ display: table; width: 100%; } .box{ display: table-cell; width: 0.1%; } .left{ margin-right: 20px; background-color: red; height: 200px; } .right{ display: table-cell; background-color: blue; height: 300px; } </style> </head> <body> <div class="parent"> <div class="box"> <div class="left">126545453dddddddd453453453</div> </div> <div class="right">12121</div> </div> </body> </html>
The principle and shortcomings are the same as the flex layout above
Disadvantages: too compatibility Poor, IE11 does not support it, only Google 57 and above can
The above is the detailed content of How to implement two column layout in css. For more information, please follow other related articles on the PHP Chinese website!<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.parent{
display:grid;
grid-template-columns:auto 1fr;
grid-gap:20px
}
.left{
background-color: red;
height: 200px;
}
.right{
height:300px;
background-color: blue;
}
</style>
</head>
<body>
<div class="parent">
<div class="left">1111111111111111111111111</div>
<div class="right"></div>
</div>
</body>
</html>