Home>Article>Web Front-end> How to implement two column layout in css

How to implement two column layout in css

青灯夜游
青灯夜游 Original
2021-07-22 15:33:42 6012browse

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.

How to implement two column layout in css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

1. What is a two-column layout?

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.

2. How to achieve fixed width on the left side and adaptive adjustment on the right side?

1. Double inline-block

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

      
1234
1234

2. Double float

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

      
123adadadddddddddddddddddddddddddddddddddddddddd

3. Floating margin

## 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

      

1234

1234

4. Floating BFC

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

      
111111111111111111111111
111111111111111111111111111111111111111111111

5.absolute margin- left

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

      

6.flex layout

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

      

3. The elements on the left and right sides are both adaptive

Strictly speaking, it does not mean that both elements are adaptive, it is just that the fixed width above is changed to be stretched by the child elements

1. Floating BFC

The principle is the same as above, except that the width of the left element is not set and is supported by the child elements

2.table layout

Principle: The parent element is display:table, and the left element is wrapped with a div. The div is set to display:table-cell, width :0.1% (guaranteed minimum width), margin-right is set internally on the left element, and display:table-cell is set on the right element.

Disadvantages: IE7 and below do not support it. When display:table is used, padding is invalid. The line-height attribute of the parent element is invalid. When display:table-cell is used, margin is invalid.

      
126545453dddddddd453453453
12121

3.flex layout

The principle and shortcomings are the same as the flex layout above

4.grid layout

## Principle: The parent element sets display: grid, grid-template-columns:auto 1fr; (This attribute defines the column width, auto The keyword indicates that the length is determined by the browser itself. fr is a relative size unit, indicating that the remaining space is equally divided) grid-gap: 20px (line spacing)

Disadvantages: too compatibility Poor, IE11 does not support it, only Google 57 and above can

      
1111111111111111111111111
(Learning video sharing:

css video tutorial

)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn