Solution to the left alignment of the last line of space between in flex layout


First look at the code and effect

        .main {
            outline: 1px solid;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
        .main>div {
            width: 100px;
            height: 100px;
            margin-bottom: 10px;
            background-color: lightgreen;
        <div class="main">

You can see that the last div is not in the middle, but at the end

Because we set the justify content to space between, which means the two sides are pasted

At this time, we can set a pseudo element for the outermost div with the same width as the inner Div

You only need two lines of CSS

.main:after {
    content: "";
    width: 100px;

It’s time to see the effect

In fact, the principle is that the last pseudo element pushed him over

Even if there are 9, it doesn’t affect, because his height is 0. See the figure below

So far, this article about how to solve the problem of left alignment of the last line of flex layout space between is introduced. For more information about left alignment of flex layout space between, please search previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!