I have a fixed number of columns in a CSS Grid display, and elements to put in these columns. The number of elements to put is smaller than the number of columns.
I would like the columns to be aligned to the right, in the order where the elements were added. In the code below, 4 elements are aligned to the left, and 6 other are emty to their right:
.hello {
border-style: solid;
border-color: blue;
}
.container {
display: grid;
grid-gap: 5px;
grid-template-columns: repeat(10, 5rem);
}
<div class="container">
<div class="hello">1</div>
<div class="hello">2</div>
<div class="hello">3</div>
<div class="hello">4</div>
</div>
I would like to change this display from the one above:
1 2 3 4 empty empty empty empty empty empty
to
empty empty empty empty empty empty 1 2 3 4
I tried all kinds of combinations of [align,justify]-[content,items]
but never to the right one. Is this doable with CSS Grid?
4
Answers
Since the number is limited, you can define all of them manually
But better use flexbox:
I have used CSS code, the :nth-child() pseudo-class is used to target specific elements with the class .hello and adjust their grid positioning.
If your number of empty rows are dynamic,You should use javascript/jquery/ts for creating DIVs that will repeat
Used Flex instead of GRID.
Gap is the distance between flex elements.You can adjust it however u want.
used For aligning all Divs to the right.
min-width:20px; is added so that to show that divs wil have a minimum width.
Flex=1
can be used so that all the divs inside flex have equal width.flex: 1;
in .hello ClassHow about adding empty elements to fill the grid?