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

Design Theory: A Method to Understand People's Hearts

<br />Once, Foyin and Mr. Dongpo were chatti...

Docker memory monitoring and stress testing methods

The Docker container that has been running shows ...

How to use CSS counters to beautify ordered lists of numbers

In web design, it is very important to use an org...

Solve the problem of IDEA configuring tomcat startup error

The following two errors were encountered when co...

Detailed explanation of how to configure static IP in Centos8

After installing centos 8, the following error wi...

Two implementation solutions for vuex data persistence

Table of contents Business requirements: Solution...

Vue storage contains a solution for Boolean values

Vue stores storage with Boolean values I encounte...

Solution to the error problem of Vscode remotely connecting to Ubuntu

1. Background of the incident: Because of work ne...

About ROS2 installation and docker environment usage

Table of contents Why use Docker? Docker installa...

Detailed method of using goaccess to analyze nginx logs

Recently I want to use goaccess to analyze nginx ...

6 Ways to Elegantly Handle Objects in JavaScript

Table of contents Preface 1. Object.freeze() 2. O...

Some suggestions on Vue code readability

Table of contents 1. Make good use of components ...

Echarts implements switching different X-axes in one graph (example code)

Rendering If you want to achieve the effect shown...

Mysql implementation of full-text search and keyword scoring method example

1. Introduction Today a colleague asked me how to...