Detailed explanation of the problems and solutions encountered in flex multi-column layout

Detailed explanation of the problems and solutions encountered in flex multi-column layout

Flex layout is undoubtedly simple and easy to use. It makes our layout simpler and faster. However, when using flex for multi-column layout, I believe many people will encounter the following situation:

This happens because we use justify-content: space-between; the reason for using this setting is that we don't need to specifically calculate the spacing between elements, flex will calculate it for us, but because of this, when we have insufficient elements in the next row, the above situation will occur. Then the problem is clear, it is caused by insufficient elements in the last row, then we can think of adding an empty element to the last row to take up space, which can perfectly solve the above problem:

Here we use pseudo-elements to achieve placeholders. Note that pseudo-elements only need to set width, never height. The details are as follows:

.container::after{
      content: '';
      width:320px;
    }

The effect is as follows:

However, we will find that in display development, we will not only encounter three-column layouts, but also four-column and five-column layouts. Obviously, the above approach will not work at this time. So how to solve it? The solution is also very simple. The principle is the same. Use empty elements to occupy space. This time we will not use pseudo-elements. We use real DOM elements to operate:

Write a row of empty elements in advance: as follows

<div class="container">
    <div class="box">1</div>
    <div class="box">2</div>
    <div class="box">2</div>
    <div class="box">2</div>
    <div class="box">2</div>
    <div class="box">2</div>
    <div class="box">2</div>
    <div class="box">2</div>
    <div class="box">2</div>
    <div class="box">2</div>
    <!-- Dividing line (below is the empty element I wrote in advance) -->
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>

Corresponding CSS settings:

div:empty{
      height: 0;
      width:160px;
      border:none;
    }

The effect is as follows:

At this time, we will find that no matter how many columns there are, we can perfectly solve our initial problem

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Detailed explanation of MYSQL large-scale write problem optimization

>>:  Goodbye Docker: How to Transform to Containerd in 5 Minutes

Recommend

MySQL installation and configuration tutorial for Mac

This article shares the MySQL installation tutori...

Summary of MySql index, lock, and transaction knowledge points

This article summarizes the knowledge points of M...

Some things to note about varchar type in Mysql

Storage rules for varchar In versions below 4.0, ...

How to use Vue's idea to encapsulate a Storage

Table of contents background Function Purpose Ide...

How to limit the value range of object keys in TypeScript

When we use TypeScript, we want to use the type s...

Nginx cache configuration example

When developing and debugging a web application, ...

MySQL 8.0 upgrade experience

Table of contents Preface 1. First completely uni...

js to achieve a simple carousel effect

This article shares the specific code of js to ac...

How to expand the disk space of Linux server

Table of contents Preface step Preface Today I fo...

How to add a column to a large MySQL table

The question is referenced from: https://www.zhih...

JavaScript two pictures to understand the prototype chain

Table of contents 1. Prototype Relationship 2. Pr...

Mini Program Custom TabBar Component Encapsulation

This article example shares the specific code for...

Detailed explanation of the functions of each port of Tomcat

From the tomcat configuration file, we can see th...