Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Defining Rows and Columns | Getting Started with CSS Grid
CSS Grid Mastery

bookDefining Rows and Columns

Understanding grid-template-rows and grid-template-columns

To begin mastering CSS Grid layouts, you need to understand how to define the structure of your grid using the grid-template-rows and grid-template-columns properties. These properties allow you to specify the number and size of the rows and columns in your grid container.

The syntax for both properties is straightforward: provide a space-separated list of track sizes using units such as px, em, %, fr (fractional units), or keywords like auto.

Example:

  • grid-template-columns: 200px 1fr 2fr; creates three columns:
    • First column fixed at 200 pixels;
    • Second column takes up one fraction of the remaining space;
    • Third column takes up two fractions of the remaining space.
  • grid-template-rows: 100px auto; creates two rows:
    • First row fixed at 100 pixels;
    • Second row automatically adjusts to fit its content.

Best Practices:

  • Use flexible units like fr for responsive layouts;
  • Keep grid definitions clear and readable;
  • Avoid overly complex track listings unless necessary for your design.
index.html

index.html

styles.css

styles.css

copy

How Changing Grid Definitions Affects Layout

When you adjust the values of grid-template-rows and grid-template-columns, you directly control the number of tracks and their sizing, which in turn affects how grid items are displayed.

  • Increasing the number of columns causes grid items to flow into additional vertical tracks;
  • Changing a row's size expands or contracts the space available to its items;
  • Using flexible units like fr allows your layout to adapt smoothly to different screen sizes;
  • Fixed units such as px ensure precise control over track dimensions.

Understanding how these definitions interact helps you predict and manage the placement and sizing of grid items, making your layouts both robust and responsive.

question mark

Which of the following CSS declarations correctly creates a grid with three columnsβ€”first column fixed at 100px, second column flexible (using 1fr), and third column twice as flexible (using 2fr)?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 9.09

bookDefining Rows and Columns

Swipe to show menu

Understanding grid-template-rows and grid-template-columns

To begin mastering CSS Grid layouts, you need to understand how to define the structure of your grid using the grid-template-rows and grid-template-columns properties. These properties allow you to specify the number and size of the rows and columns in your grid container.

The syntax for both properties is straightforward: provide a space-separated list of track sizes using units such as px, em, %, fr (fractional units), or keywords like auto.

Example:

  • grid-template-columns: 200px 1fr 2fr; creates three columns:
    • First column fixed at 200 pixels;
    • Second column takes up one fraction of the remaining space;
    • Third column takes up two fractions of the remaining space.
  • grid-template-rows: 100px auto; creates two rows:
    • First row fixed at 100 pixels;
    • Second row automatically adjusts to fit its content.

Best Practices:

  • Use flexible units like fr for responsive layouts;
  • Keep grid definitions clear and readable;
  • Avoid overly complex track listings unless necessary for your design.
index.html

index.html

styles.css

styles.css

copy

How Changing Grid Definitions Affects Layout

When you adjust the values of grid-template-rows and grid-template-columns, you directly control the number of tracks and their sizing, which in turn affects how grid items are displayed.

  • Increasing the number of columns causes grid items to flow into additional vertical tracks;
  • Changing a row's size expands or contracts the space available to its items;
  • Using flexible units like fr allows your layout to adapt smoothly to different screen sizes;
  • Fixed units such as px ensure precise control over track dimensions.

Understanding how these definitions interact helps you predict and manage the placement and sizing of grid items, making your layouts both robust and responsive.

question mark

Which of the following CSS declarations correctly creates a grid with three columnsβ€”first column fixed at 100px, second column flexible (using 1fr), and third column twice as flexible (using 2fr)?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 2
some-alt