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



grid-area

CSS Grid Layout is a powerful tool for creating complex layouts on the web. It allows you to create a grid of rows and columns, and then place content within those cells. One of the key features of CSS Grid Layout is the grid-area property, which allows you to specify where content should be placed within the grid.

The grid-area property is used to assign a name to a grid item, which can then be used to place that item within the grid. The name can be any string, and is defined using the grid-template-areas property. Here's an example:

<style>
  .grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 100px 100px;
    grid-template-areas:
      "header header header"
      "main main sidebar";
  }

  .header {
    grid-area: header;
  }

  .main {
    grid-area: main;
  }

  .sidebar {
    grid-area: sidebar;
  }
</style>

<div class="grid-container">
  <div class="header">Header</div>
  <div class="main">Main Content</div>
  <div class="sidebar">Sidebar</div>
</div>

In this example, we have a grid container with three columns and two rows. The grid-template-areas property defines the layout of the grid, with the header spanning all three columns in the first row, and the main content and sidebar each spanning one column in the second row. The grid-area property is then used to assign each item to its corresponding area within the grid.

The grid-area property can also be used to span multiple rows or columns. To do this, you can specify the starting and ending row or column numbers using the / character. Here's an example:

<style>
  .grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 100px 100px;
    grid-template-areas:
      "header header header"
      "main main sidebar";
  }

  .header {
    grid-area: header;
  }

  .main {
    grid-area: main;
  }

  .sidebar {
    grid-area: sidebar;
    grid-row: 2 / 4;
  }
</style>

<div class="grid-container">
  <div class="header">Header</div>
  <div class="main">Main Content</div>
  <div class="sidebar">Sidebar</div>
</div>

In this example, we have the same grid layout as before, but the sidebar now spans two rows using the grid-row property. The value 2 / 4 specifies that the sidebar should start on the second row and end on the fourth row.

The grid-area property can also be used to span multiple columns using the grid-column property. Here's an example:

<style>
  .grid-container {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 100px 100px;
    grid-template-areas:
      "header header header"
      "main main sidebar";
  }

  .header {
    grid-area: header;
  }

  .main {
    grid-area: main;
  }

  .sidebar {
    grid-area: sidebar;
    grid-column: 2 / 4;
  }
</style>

<div class="grid-container">
  <div class="header">Header</div>
  <div class="main">Main Content</div>
  <div class="sidebar">Sidebar</div>
</div>

In this example, the sidebar now spans two columns using the grid-column property. The value 2 / 4 specifies that the sidebar should start on the second column and end on the fourth column.

The grid-area property is a powerful tool for creating complex layouts with CSS Grid Layout. By assigning names to grid items and using the grid-template-areas property to define the layout of the grid, you can easily create flexible and responsive layouts that adapt to different screen sizes and devices.

Conclusion

CSS Grid Layout is a powerful tool for creating complex layouts on the web. The grid-area property is a key feature of CSS Grid Layout, allowing you to specify where content should be placed within the grid. By assigning names to grid items and using the grid-template-areas property to define the layout of the grid, you can easily create flexible and responsive layouts that adapt to different screen sizes and devices.

References

Activity