Study notes-flexible layout to achieve two rows and four columns equally divided layout

During the development process, you are sometimes asked to place multiple pictures in 2X2 or 3X3 formats. It is required that the gap between the top and bottom and the left and right of any two adjacent pictures is the same.


一.padding-top/margin-top,padding-bottom/margin-bottom

1. Code:

<div class="page-container">
    <div class="layout-wrapper">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item margin"></div>
        <div class="item margin"></div>
        <div class="item margin"></div>
        <div class="item margin"></div>
    </div>
</div>
* { padding: 0; margin: 0; }
.page-container {
    width: 100%;
    height: 100vw;
    background-color: #F5F5F5;
}
.layout-wrapper {
    width: 80%;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}
.item {
    flex-shrink: 0;
    width: 24.1%;
    height: 200px;
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 4px 4px 4px rgba(0,0,0,0.05);
}
.item.margin { margin-top: 1.2%; }

2. Effect picture:

3. Key points:

When the values ​​of padding-top, padding-bottom.margin-top, margin-bottom are set as percentages, the calculation result is based on the width of the parent element.

The above knowledge is mainly used here.

 2. Grid layout

1. Code:

<div class="page-container">
    <div class="layout-wrapper">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
</div>
* { padding: 0; margin: 0; }
.page-container {
    width: 100%;
    height: 100vw;
    background-color: #F5F5F5;
}
.layout-wrapper {
    width: 80%;
    margin: 0 auto;
    display: grid;
    grid-template-columns: repeat(4, 25%);
    grid-template-rows: 200px 200px;
    grid-gap: 12px;
}
.item {
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 4px 4px 4px rgba(0,0,0,0.05);
}

 2. Effect picture:

3. Key points:

Grid layout can easily implement many complex page layouts. Very practical


In web page editing, this effect is more common. Of course, other methods can also be used to achieve the same effect.

Guess you like

Origin blog.csdn.net/qq_41339126/article/details/114092437