How do I align a css grid track?



  • A CSS grid track is the space between the space between two adjacent grid lines, essentially, the space in-between the grid items.

    b96c0d27-066f-432a-98ce-c8f450159cb6-image.png

    Also see: How do I align css grid items? we saw how to align grid items.

    The demo code:

    HTML

    ...
    <div class="container">
          <div class="item item1">item 1</div>
          <div class="item item2">item 2</div>
          <div class="item item3">item 3</div>
          <div class="item item4">item 4</div>
          <div class="item item5">item 5</div>
          <div class="item item6">item 6</div>
          <div class="item item5">item 7</div>
          <div class="item item6">item 8</div>
    </div>
    ...
    

    CSS

    ...
    .container {
            width: 800px;
            height: 700px;
            background-color: rgb(95, 118, 133);
            margin: 40px auto;
            display: grid;
            grid-template-columns: repeat(2, 120px);
            grid-template-rows: repeat(4, 120px);
            grid-gap: 30px;
          }
    
          .item {
            padding: 10px;
            font-size: 30px;
            color: #ffffff;
          }
    
          .item1 {
            background-color: plum;
          }
    
          .item2 {
            background-color: mediumvioletred;
          }
    
          .item3 {
            background-color: chocolate;
          }
    
          .item4 {
            background-color: darkgreen;
          }
    
          .item5 {
            background-color: firebrick;
          }
    
          .item6 {
            background-color: olivedrab;
          }
    
          .item7 {
            background-color: navy;
          }
    
          .item8 {
            background-color: yellow;
          }
    ...
    

    The result is:

    b96c0d27-066f-432a-98ce-c8f450159cb6-image.png

    The CSS properties that can be used to align CSS grid track are the same as aligning items.

    Adding justify-content: end; to the CSS container.

    The result is:

    0fcc6bec-b978-4ca8-bb59-ca6dfae46417-image.png

    justify-content: space-between;

    The result is:

    45b081b9-98e0-45ee-90e7-112c3e4eb9e9-image.png

    There are other options for CSS justify-content property.

    To align vertically we use align-content

    Adding align-content: end to the CSS container.

    The result is:

    eaaed283-5ddc-4921-ab22-63a874d0d8e5-image.png

    align-content: space-around

    The result is:

    fead8a30-4dde-4836-b251-6e77c3845f20-image.png

    align-content: space-evenly

    The result is:

    2245cb31-c711-4ee2-9442-7be4db608af3-image.png

    There are other options for CSS align-content property


Log in to reply
 

© Lightnetics 2024