Search code examples
htmlcssalignmentcss-grid

How to align grid elements to the right column?


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 the other 6 are empty 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 but none of them work right. Is this possible with CSS Grid?


Solution

  • Since the number is limited, you can define all of them manually

    .hello {
      border-style: solid;
      border-color: blue;
    }
    
    .container {
      display: grid;
      gap: 5px;
      grid-template-columns: repeat(10, 3rem);
      margin: 5px;
    }
    
    .container > :nth-last-child(1) {grid-column-end: -1}
    .container > :nth-last-child(2) {grid-column-end: -2}
    .container > :nth-last-child(3) {grid-column-end: -3}
    .container > :nth-last-child(4) {grid-column-end: -4}
    .container > :nth-last-child(5) {grid-column-end: -5}
    .container > :nth-last-child(6) {grid-column-end: -6}
    .container > :nth-last-child(7) {grid-column-end: -7}
    .container > :nth-last-child(8) {grid-column-end: -8}
    .container > :nth-last-child(9) {grid-column-end: -9}
    <div class="container">
      <div class="hello">1</div>
      <div class="hello">2</div>
      <div class="hello">3</div>
      <div class="hello">4</div>
    </div>
    
    <div class="container">
      <div class="hello">1</div>
      <div class="hello">2</div>
      <div class="hello">3</div>
    </div>
    
    <div class="container">
      <div class="hello">1</div>
      <div class="hello">2</div>
      <div class="hello">3</div>
      <div class="hello">4</div>
      <div class="hello">5</div>
      <div class="hello">6</div>
    </div>

    But better use flexbox:

    .hello {
      border-style: solid;
      border-color: blue;
      width: 3rem;
    }
    
    .container {
      display: flex;
      gap: 5px;
      width: calc(10*3rem + 9*5px);
      margin: 5px;
    }
    
    .container > :first-child {margin-left:auto}
    <div class="container">
      <div class="hello">1</div>
      <div class="hello">2</div>
      <div class="hello">3</div>
      <div class="hello">4</div>
    </div>
    
    <div class="container">
      <div class="hello">1</div>
      <div class="hello">2</div>
      <div class="hello">3</div>
    </div>
    
    <div class="container">
      <div class="hello">1</div>
      <div class="hello">2</div>
      <div class="hello">3</div>
      <div class="hello">4</div>
      <div class="hello">5</div>
      <div class="hello">6</div>
    </div>