What are the ways to implement equal height layout in css

王林
Release: 2020-03-24 10:48:59
forward
2439 people have browsed it

What are the ways to implement equal height layout in css

What is equal height layout?

refers to a layout in which child elements in the same parent container have equal heights.

From the perspective of the implementation method of equal height layout, it is divided into two categories:

1. Pseudo-equal height

The height difference of sub-elements still exists, but it just visually gives people the impression that Equal height.

2. True equal height

The heights of sub-elements are equal.

Pseudo-equal height implementation:

Through negative margin and Padding

True equal-height implementation:

1, table

2, absoult

3, flex

4, grid

5, js

(recommended tutorial: CSS Getting Started Tutorial )

Pseudo-equal height - negative margin and padding

Mainly use negative margin to achieve, as follows:

 <div class="layout parent">
        <div class="left"><p>left</p></div>
        <div class="center">
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
        </div>
        <div class="right"><p>right</p></div>
        <div style="clear: both;">11111111111</div>
    </div>
Copy after login
.parent{
    position: relative;
    overflow:hidden;
    color: #efefef;
}
.center,
.left,
.right {
    box-sizing: border-box;
    float: left;
}
.center {
    background-color: #2ECC71;
    width: 60%;
}

.left {
    width: 20%;
    background-color: #1ABC9C;
}
.right {
    width: 20%;
    background-color: #3498DB;
}
.left,
.right,
.center  {
    margin-bottom: -99999px;
    padding-bottom: 99999px;
}
Copy after login

Real equal height-table layout

  <div class="layout parent">
        <div class="left"><p>left</p></div>
        <div class="center">
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
        </div>
        <div class="right"><p>right</p></div>
        <div style="clear: both;">11111111111</div>
    </div>
Copy after login
    .parent{
        position: relative;
        display: table;
        color: #efefef;
    }
    .center,
    .left,
    .right {
        box-sizing: border-box;
        display: table-cell
    }
    .center {
        background-color: #2ECC71;
        width: 60%;
    }

    .left {
        width: 20%;
        background-color: #1ABC9C;
    }
    .right {
        width: 20%;
        background-color: #3498DB;
    }
Copy after login

True Contour-absolute

    <div class="layout parent">
        <div class="left"><p>left</p> </div>
        <div class="center">
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
        </div>
        <div class="right"><p>right</p></div>
    </div>
Copy after login
Copy after login
   .parent{
        position: absolute;
        color: #efefef;
        width:100%;
        height: 200px;
    }

    .left,
    .right,
    .center {
        position: absolute;
        box-sizing: border-box;
        top:0;
        bottom:0;
    }
    .center {
        background-color: #2ECC71;
        left: 200px;
        right: 300px;
    }

    .left {
        width: 200px;
        background-color: #1ABC9C;
    }
    .right {
        right:0;
        width: 300px;
        background-color: #3498DB;
    }
Copy after login

Real Contour-flex

.parent{
    display: flex;
    color: #efefef;
    width:100%;
    height: 200px;
}

.left,
.right,
.center {
    box-sizing: border-box;
    flex: 1;
}
.center {
    background-color: #2ECC71;
}
.left {
    background-color: #1ABC9C;
}
.right {
    background-color: #3498DB;
}
Copy after login
<div class="layout parent">
    <div class="left"><p>left</p> </div>
    <div class="center">
        <p>我是中间部分的内容</p>
        <p>我是中间部分的内容</p>
        <p>我是中间部分的内容</p>
        <p>我是中间部分的内容</p>
    </div>
    <div class="right"><p>right</p></div>
</div>
Copy after login
Copy after login

Real Contour-grid

    .parent{
        display: grid;
        color: #efefef;
        width:100%;
        height: 200px;
        grid-template-columns: 1fr 1fr 1fr;
    }

    .left,
    .right,
    .center {
        box-sizing: border-box;
    }
    .center {
        background-color: #2ECC71;
    }
    .left {
        background-color: #1ABC9C;
    }
    .right {
        background-color: #3498DB;
    }
Copy after login
<div class="layout parent">
    <div class="left"><p>left</p> </div>
    <div class="center">
        <p>我是中间部分的内容</p>
        <p>我是中间部分的内容</p>
        <p>我是中间部分的内容</p>
        <p>我是中间部分的内容</p>
    </div>
    <div class="right"><p>right</p></div>
</div>
Copy after login
Copy after login

Real Contour-js

Get the highest column among all elements, and then compare and modify it

    <div class="layout parent">
        <div class="left"><p>left</p> </div>
        <div class="center">
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
            <p>我是中间部分的内容</p>
        </div>
        <div class="right"><p>right</p></div>
    </div>
Copy after login
Copy after login
    .parent{
        overflow: auto;
        color: #efefef;
    }
    .left,
    .right,
    .center {
        float: left;
    }
    .center {
        width: 60%;
        background-color: #2ECC71;
    }
    .left {
        width: 20%;
        background-color: #1ABC9C;
    }
    .right {
        width: 20%;
        background-color: #3498DB;
    }
Copy after login
     // 获取最高元素的高度
    var nodeList = document.querySelectorAll(".parent > div");
    var arr = [].slice.call(nodeList,0);
    var maxHeight = arr.map(function(item){
        return item.offsetHeight
    }).sort(function(a, b){
        return a - b;
    }).pop();
    arr.map(function(item){
        if(item.offsetHeight < maxHeight) {
            item.style.height = maxHeight + "px";
        }
    });
Copy after login

As shown:

What are the ways to implement equal height layout in css

## Recommended related video tutorials :

css video tutorial

The above is the detailed content of What are the ways to implement equal height layout in css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!