CSS for Web Development

0% completed

Previous
Next
Gap, template areas, and auto-placement

CSS Grid provides powerful tools to manage the layout's structure beyond just defining columns and rows. Three useful properties are:

  • Gap: Adds consistent spacing between rows and columns without the need for additional margins.
  • Template Areas: Lets you define named areas in your grid, making it easier to design and rearrange layouts.
  • Auto-Placement: Automatically places grid items into appropriate cells when you don’t specify an explicit position.

These features simplify building complex grid layouts with less code and increased readability.

Syntax

Follow the code block below to use these properties in your grid container:

/* Set gap between rows and columns */ grid-gap: 10px; /* or use row-gap and column-gap separately */ /* Define grid template areas */ grid-template-areas: "header header header" "sidebar content content" "footer footer footer"; grid-area: header; /* Defining template area name for any particular element. After that, you can use this as a value for the `grid-template-areas` property. */ /* Auto-placement: grid will automatically assign positions for grid items that do not have an explicit placement */

Explanation:

  • grid-gap (or gap):
    • Specifies the spacing between rows and columns.
  • grid-template-areas:
    • Allows you to name regions of the grid by defining areas.
    • Items placed using area names can be rearranged easily.
  • grid-area: It defines the template-area.
  • Auto-Placement:
    • When you don’t assign a grid item to a specific cell, CSS Grid places it automatically based on available space.

Example 1: Using Gap to Create Space

In this example, we create a grid container with defined rows and columns and add a gap between the grid items for clear separation.

HTML

. . . .

Explanation:

  • Grid Layout:
    • Three columns of equal width and two rows are defined.
  • Gap:
    • A 15px gap is added between both rows and columns.
  • Outcome:
    • The grid items are evenly spaced, resulting in a clean and organized layout.

Example 2: Template Areas with Auto-Placement

In this example, we create a grid layout using template areas. We name different sections (header, sidebar, content, and footer) and position items automatically based on these areas.

HTML

. . . .

Explanation:

  • Grid Template Areas:
    • The grid-template-areas property names the layout regions, making it easy to see how the grid is organized.
  • Item Placement:
    • Each grid item has the grid-area property set to a corresponding name (header, sidebar, content, footer).
    • This makes the grid items automatically flow into the defined areas.
  • Auto-Placement:
    • Any additional grid items without a specified grid-area would be auto-placed into available cells.
  • Outcome:
    • A structured layout is formed with a header across the top, a sidebar on the left of the content area, and a footer below. Gaps create space between each area.

This lesson shows how to use gap, template areas, and auto-placement to create flexible and visually organized grid layouts. By combining these properties, you can build intricate designs with minimal code while maintaining a clear structure.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next