Grid-template-rows and grid-template-columns
Grid template columns and rows are CSS properties used to define the size and layout of grid elements. By using these properties, you can easily create a grid structure with specified column and row sizes.
Lets Go!

Grid-template-rows and grid-template-columns
Lesson 23
Learn how to define row and column sizes in a grid layout.
Get Started 🍁Introduction to CSS Grid Template Columns and Rows
Welcome to our course on CSS Grid Template Columns and Rows! In this course, we will delve into the world of CSS properties, specifically focusing on the grid template columns and grid template rows. These properties play a crucial role in defining the layout of grid elements on a webpage, allowing you to customize the size and positioning of columns and rows within a grid.
Have you ever wondered how to structure your webpage layout with precision and flexibility? How do you ensure that the elements on your webpage align perfectly and display in a visually appealing manner? Well, this course will provide you with the answers you seek.
Throughout our lessons, we will cover the basics of grid template columns and rows, understanding how to define line names, track sizing functions, and create a responsive grid layout. By the end of this course, you will have a solid understanding of how to leverage these CSS properties to design stunning and functional web layouts.
So, are you ready to unlock the power of CSS Grid Template Columns and Rows? Join us on this exciting journey as we dive deep into the world of web design and layout optimization. Let's get started!
Main Concepts of CSS Grid Properties:**
-
Grid Template Columns:
- The
grid-template-columns
CSS property is used to define the line names and track sizing functions of the grid columns. - This property allows you to set the size of each grid column within a grid element.
- By defining values such as fractions or pixels, you can control the width of each column in the grid.
- For example, setting
grid-template-columns: 250px 250px;
will create two columns, each with a width of 250 pixels.
- The
-
Grid Template Rows:
- The
grid-template-rows
CSS property is used to define the line names and track sizing functions of the grid rows. - Similar to
grid-template-columns
, this property allows you to set the height of each grid row within a grid element. - By specifying values such as pixels or fractions, you can control the height of each row in the grid.
- For instance, setting
grid-template-rows: 150px 300px 200px;
will create three rows with heights of 150 pixels, 300 pixels, and 200 pixels respectively.
- The
-
Application in HTML Structure:
- To apply these grid properties, you need to define a grid container and specify the sizes for columns and rows.
- By adding internal elements (divs) within the grid container and assigning CSS styles, you can visually see the impact of grid template columns and rows.
- The specified values for grid template columns and rows determine the layout of the grid elements, allowing for customized designs.
- Using these properties, you can easily manage the layout and spacing of elements within a grid, making it a versatile tool for creating responsive designs.
By understanding and implementing grid-template-columns
and grid-template-rows
properties in CSS, you can effectively structure and design grids in HTML for a visually appealing and organized layout.
Practical Applications of CSS Grid Template Columns and Rows
In this section, we will guide you through the practical applications of using CSS Grid Template Columns and Rows to define the size of grid elements in a web layout.
Step 1: Setting Up the HTML Structure
- Create a container with a class name
grid
. - Inside the
grid
container, adddiv
elements to represent grid items. For this example, add twodiv
elements with class nameselement1
andelement2
.
Step 2: Styling Grid Items
.grid div { background-color: #2D0B44; color: white; border: 1px solid aqua; border-radius: 15px; }
Step 3: Creating Grid Layout with CSS Grid
- Apply
display: grid;
to thegrid
container. - Add
align-items: center;
andjustify-content: center;
to center the grid items. - Use
gap: 1rem;
to add spacing between grid items. - Set
min-height: 20vh;
for the grid items.
.grid { display: grid; align-items: center; justify-content: center; gap: 1rem; min-height: 20vh; }
Step 4: Defining Grid Template Columns
- Define the size of grid columns using
grid-template-columns
. - Use values like
1fr
or300px
to specify the width of each column.
.grid { grid-template-columns: 250px 250px; /* Defines two columns of 250px each */ }
Step 5: Adding Grid Elements
- Add more
div
elements (e.g., elements 3, 4, 5, 6) to see the grid layout changes. - The columns will adjust to accommodate the new elements based on the defined column sizes.
Step 6: Exploring Grid Template Rows
- Use
grid-template-rows
to set the height of grid rows. - Specify the height for each row using values like
100px
or1fr
.
.grid { grid-template-rows: 150px 300px 200px; /* Defines three rows with varying heights */ }
Step 7: Experimenting with Grid Layout
- Adjust the values in
grid-template-columns
andgrid-template-rows
to see how the layout adapts. - Add more grid elements to observe changes in row and column sizing dynamically.
By following these steps and experimenting with different values, you can gain a better understanding of how CSS grid properties like grid-template-columns
and grid-template-rows
work in defining the layout of web elements. Feel free to tweak the values and structures to create custom grid layouts for your projects.
Test your Knowledge
What does 1fr 2fr mean in grid-template-columns?
What does 1fr 2fr mean in grid-template-columns?
Advanced Insights into CSS Grid Properties
In today's video, we delved into the CSS properties grid-template-columns
and grid-template-rows
and how they can be used together in a single grid element to define the size of grid columns and rows. Let's explore some advanced insights into these properties to enhance your understanding.
Tips for Using Grid Template Columns and Rows:
-
Combining Multiple Values: You can define multiple values for both
grid-template-columns
andgrid-template-rows
to create a complex grid layout. Experiment with different fractions, pixel values, or percentages to achieve the desired grid structure. -
Creating Responsive Grids: Utilize media queries in CSS to adjust the grid template columns and rows based on different screen sizes. This flexibility allows your grid layout to adapt to various devices seamlessly.
-
Grid Line Names: Apart from track sizing functions, you can also define line names for grid columns and rows using the
grid-template-areas
property. This feature enhances the readability and organization of your grid layout.
Expert Advice:
When using CSS grid properties, it's essential to understand the interplay between grid-template-columns and grid-template-rows. By combining these properties effectively, you can create intricate and visually appealing grid layouts that cater to your design requirements.
Curiosity Question:
How can you implement nested grids within a larger CSS grid layout to achieve a more structured design? Explore the possibilities of nesting grids to enhance the visual hierarchy and organization of your web content.
Additional Resources for CSS Grid Properties
-
CSS Grid Layout by MDN Web Docs: A comprehensive guide on CSS Grid Layout with detailed explanations and examples.
-
CSS Grid Template Areas by CSS-Tricks: An in-depth tutorial on using grid template areas to define grid layout in CSS.
-
Grid by Example: A website dedicated to providing practical examples and tutorials for using CSS Grid for layout design.
Explore these resources to deepen your understanding of CSS grid properties and enhance your web development skills. Happy learning!
Practice
Task: Create a 3x2 grid using grid-template-rows and grid-template-columns.