Align flex items in rows using specified spacing
P粉354948724
P粉354948724 2023-09-17 19:34:16
0
1
553

I have two rows containing

, each row hasdisplay: flex;, each row contains several sub
;project. In the example below, you can see that there are 2 items in the first row (the green box labeled "item") and their ratio is6/3. In the second row there are 4 items and their proportion is2/2/2/3.

Now I want the last item in each row (withflex: 3;) to be perfectly aligned unless I add agap: X px;.


How do I include thisgapin the calculation so that items with the same width align according to theirflexproperty? Or simplify it graphically in the following code snippet: How do I align the red border?

.flex { height: 50px; display: flex; gap: 20px; // <- 破坏我的布局的罪魁祸首 } .item { background-color: green; outline: 1px solid black; display: flex; justify-content: center; align-items: center; } .item-2 { flex: 2; } .item-3 { flex: 3; } .item-6 { flex: 6; }
item
item
item
item
item
item

P粉354948724
P粉354948724

reply all (1)
P粉154798196

One of the many possible solutions is to wrap some items in another flex div.
See the code below, which contains classflex-item:

item
item
item
item
item
item
.flex { height: 50px; display: flex; gap: 20px; // <- 破坏我的布局的罪魁祸首 } .item { background-color: green; outline: 1px solid black; display: flex; justify-content: center; align-items: center; } .item-2 { flex: 2; } .item-3 { flex: 3; } .item-6 { flex: 6; } .flex-item { display: flex; gap: 20px; justify-content: center; }
    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!