Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 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)?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  2

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  2
some-alt