CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



grid

CSS Grid is a powerful layout system that allows developers to create complex and responsive web layouts with ease. It is a two-dimensional grid-based layout system that allows you to create complex layouts by dividing a page into rows and columns. Grid is a relatively new feature in CSS, but it has quickly become one of the most popular and widely used layout systems in web development.

How Grid Works

The CSS Grid layout system works by dividing a page into a grid of rows and columns. You can then place content into specific cells within the grid, allowing you to create complex layouts that are both flexible and responsive. Grid is designed to work with both fixed and flexible layouts, making it a versatile tool for web developers.

Grid is based on a set of CSS properties that allow you to define the size and position of grid items within the grid. These properties include:

  • grid-template-columns - Defines the number and size of columns in the grid.
  • grid-template-rows - Defines the number and size of rows in the grid.
  • grid-template-areas - Defines named grid areas that can be used to place content within the grid.
  • grid-column-start - Defines the starting column of a grid item.
  • grid-column-end - Defines the ending column of a grid item.
  • grid-row-start - Defines the starting row of a grid item.
  • grid-row-end - Defines the ending row of a grid item.
  • grid-column - A shorthand property that defines both the starting and ending columns of a grid item.
  • grid-row - A shorthand property that defines both the starting and ending rows of a grid item.
  • grid-area - A shorthand property that defines both the starting and ending rows and columns of a grid item.

Examples

Here are some examples of how to use CSS Grid:

Example 1: Basic Grid

In this example, we create a basic grid with two columns and two rows:

  
    <div class="grid">
      <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>

    .grid {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-template-rows: 1fr 1fr;
    }

    .item {
      background-color: #ccc;
      padding: 20px;
      text-align: center;
    }

    .item1 {
      grid-column: 1 / 2;
      grid-row: 1 / 2;
    }

    .item2 {
      grid-column: 2 / 3;
      grid-row: 1 / 2;
    }

    .item3 {
      grid-column: 1 / 2;
      grid-row: 2 / 3;
    }

    .item4 {
      grid-column: 2 / 3;
      grid-row: 2 / 3;
    }
  

In this example, we create a grid with two columns and two rows using the grid-template-columns and grid-template-rows properties. We then place four items within the grid using the grid-column and grid-row properties.

Example 2: Responsive Grid

In this example, we create a responsive grid that adjusts its layout based on the size of the screen:

  
    <div class="grid">
      <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>

    .grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
      grid-gap: 20px;
    }

    .item {
      background-color: #ccc;
      padding: 20px;
      text-align: center;
    }
  

In this example, we create a grid with a flexible number of columns using the repeat function and the auto-fit keyword. We also use the minmax function to set a minimum and maximum size for each column. Finally, we add some spacing between the grid items using the grid-gap property.

Conclusion

CSS Grid is a powerful layout system that allows developers to create complex and responsive web layouts with ease. It is a two-dimensional grid-based layout system that allows you to create complex layouts by dividing a page into rows and columns. Grid is a relatively new feature in CSS, but it has quickly become one of the most popular and widely used layout systems in web development.

References

Activity