The flexible container is designed to align elements along the X and Y axes.
You are asking for alignment on the Z axis.
Flexbox is not designed to align with the z axis (overlap).
Any overlap should occur with negative margins, absolute positioning, CSS, JavaScript grid layout, or something else. The z-index property may also need a role.
Here is a basic example using CSS Grid:
.cards { display: grid; grid-template-columns: repeat(30, 10px); grid-template-rows: 90px; max-width: 300px; } .card { grid-row-start: 1; background-color: lightgreen; border: 1px solid black; } .card:nth-child(1) { grid-column: 1 / 6; } .card:nth-child(2) { grid-column: 4 / 9; } .card:nth-child(3) { grid-column: 7 / 12; } .card:nth-child(4) { grid-column: 10 / 15; } .card:nth-child(5) { grid-column: 13 / 18; } .card:nth-child(6) { grid-column: 16 / 21; } .card:nth-child(7) { grid-column: 19 / 24; } .card:nth-child(8) { grid-column: 22 / 27; } .card:nth-child(9) { grid-column: 25 / 30; }
<div class='cards'> <div class='card'>1</div> <div class='card'>2</div> <div class='card'>3</div> <div class='card'>4</div> <div class='card'>5</div> <div class='card'>6</div> <div class='card'>7</div> <div class='card'>8</div> <div class='card'>9</div> </div>
Cards are made for overlapping using linear placement. In this case, the grid-column property is used to overlap the column paths.
However, some scripts will be required if the number of cards changes dynamically, since the number of overlaps must change so that all cards match exactly in a container with a fixed width.
source share