One place for hosting & domains

      CSS Grid Layout: The Fr Unit


      While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or
      edited it to ensure you have an error-free learning experience. It’s on our list, and we’re working on it!
      You can help us out by using the “report an issue” button at the bottom of the tutorial.

      Introduction

      With CSS Grid Layout, we get a new flexible unit: the Fr unit. Fr is a fractional unit and 1fr is for 1 part of the available space. The following are a few examples of the fr unit at work. The grid items in these examples are placed onto the grid with grid areas.

      .container {
        display: grid;
      
        grid-template-columns: 1fr 1fr 1fr 1fr;
        grid-template-rows: 100px 200px 100px;
      
        grid-template-areas:
              "head head2 . side"
              "main main2 . side"
              "footer footer footer footer";
      }
      

      The 4 columns each take up the same amount of space.

      Head

      Head 2

      Main

      Main 2

      Side

      Footer

      Examples using fr

      Here’s the same example from above with different fr values. Notice the change in the layout:

      .container {
        /* ... */
      
        grid-template-columns: 1fr 1fr 40px 20%;
        grid-template-rows: 100px 200px 100px;
      
        /* ... */
      }
      

      Head

      Head 2

      Main

      Main 2

      Side

      Footer


      In the following last example, the sidebar item covers 2fr, so it’ll be the same width as the items that span the 1st and 2nd columns:

      .container {
        /* ... */
      
        grid-template-columns: 1fr 1fr 40px 2fr;
        grid-template-rows: 100px 200px 100px;
      
        /* ... */
      }
      

      Head

      Head 2

      Main

      Main 2

      Side

      Footer

      Mixed Units

      As you saw in the previous examples, you can mix fr values with fixed and percentage values. The fr values will be divided between the space that’s left after what’s taken by the other values.

      For example, if you have a grid with 4 columns as in the following snippet, the 1st column will be 300px, the second 80px (10% of 800px), the 3rd and 4th columns will be 210px (each occupying half of the remaining space):

      main {
        width: 800px;
        display: grid;
        grid-template-columns: 300px 10% 1fr 1fr;
        /* 300px 80px 210px 210px */
      
        grid-template-rows: auto;
      }
      



      Source link

      CSS Grid: Justification and Alignment


      While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or
      edited it to ensure you have an error-free learning experience. It’s on our list, and we’re working on it!
      You can help us out by using the “report an issue” button at the bottom of the tutorial.

      With CSS grid layout, the grid itself within its container as well as grid items can be positioned with the following 6 properties: justify-items, align-items, justify-content, align-content, justify-self and align-self. These properties are part of the CSS box alignment module and they define a standard way to position elements with either flexbox or CSS grid.

      Most of the alignment properties are applied on the grid container, but some are for grid items, for when you want to specify values that apply only for specific grid items. Let’s breakdown these properties and their effect one by one.

      For the examples to follow, we’re using the following markup and CSS rules for our grid:

      <div class="container">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
      </div>
      
      .container {
        display: grid;
        border: 2px dashed rgba(114, 186, 94, 0.35);
        height: 250px;
        grid-template-columns: 1fr 1fr 1fr;
        grid-template-rows: auto;
        background: rgba(114, 186, 94, 0.05);
      }
      .item {
        width: 50%;
        height: 50%;
        background: rgba(255, 213, 70, 0.4);
        border: 2px dashed rgba(236, 198, 48, 0.5);
      }
      

      And here’s how our base grid looks like:

      Properties on the Grid Container

      justify-items

      justify-items is used to justify the grid items along the row axis. The possible values are start, end, center and stretch. Here are examples with center and end:

      .container {
        /* ... */
        justify-items: center;
      }
      
      .container {
        /* ... */
        justify-items: end;
      }
      

      align-items

      Similar to justify-items, but aligns the grid items along the column axis:

      .container {
        /* ... */
        align-items: center;
      }
      
      .container {
        /* ... */
        align-items: end;
      }
      

      For the following justify-content and align-content examples, our base styles differ a bit in order to be able to demonstrate a grid that’s smaller than its container:

      .container {
        /* ... */
        grid-template-columns: 100px 100px 100px;
        grid-template-rows: 75px;
        height: 300px;
      }
      .item {
        width: 60px;
        height: 60px;
        /* ... */
      }
      

      justify-content

      When the entire grid is smaller than the space for the grid container, use justify-content to justify the grid along the row axis. You can use the following values: start, end, center, stretch, space-around, space-between or space evenly:

      .container {
        /* ... */
        justify-content: end;
      }
      
      .container {
        /* ... */
        justify-content: space-evenly;
      }
      

      align-content

      Similar to justify-content, but align-content aligns the grid along the column axis:

      .container {
        /* ... */
        align-content: end;
      }
      
      .container {
        /* ... */
        align-content: space-evenly;
      }
      

      Properties on the Grid Items

      Both justify-self and align-self are analogous to the equivalent properties available on the container (justify-items and align-items), but they apply on specific items that should be positioned differently than the rest of the grid items.

      justify-self (row axis)

      <div class="container">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item" style="justify-self: end;"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
      </div>
      

      align-self (column axis)

      <div class="container">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item" style="align-self: end;"></div>
        <div class="item"></div>
        <div class="item"></div>
      </div>
      



      Source link