CSS Grid Layout is a powerful tool for creating complex, responsive layouts across different screen sizes. It enables you to align and organize content into rows and columns, making it ideal for building flexible layouts with minimal code. This guide covers all the essentials, from the core concepts of CSS Grid to practical examples of properties and values.
📌 Table of Contents
📖 Section | Description |
---|---|
1. What is CSS Grid? | An overview of CSS Grid and its functionality. |
2. Key Grid Terminologies | Core concepts for understanding CSS Grid. |
• Grid Container | Parent element that holds the grid. |
• Grid Item | Direct child elements of the grid container. |
• Grid Line | Lines that divide rows and columns. |
• Grid Track | Space between two grid lines, creating rows or columns. |
3. Properties for Grid Containers | Essential properties for setting up a grid. |
• display | Defines the container as a grid. |
• grid-template-columns | Defines column layout. |
• grid-template-rows | Defines row layout. |
• gap | Adds space between grid items. |
4. Properties for Grid Items | Controls individual grid items’ positioning. |
• grid-column | Sets item span and placement in columns. |
• grid-row | Sets item span and placement in rows. |
• justify-self, align-self | Aligns items within their grid cells. |
5. Useful Functions | Powerful CSS Grid functions for flexibility. |
• repeat() | Automates repetitive grid layouts. |
• minmax() | Sets minimum and maximum track sizes. |
• fit-content() | Limits track size based on content. |
6. Conclusion | Wrap-up of CSS Grid's benefits and tips. |
What is CSS Grid?
CSS Grid is a two-dimensional layout system in CSS that allows developers to define rows and columns in which to place elements. This layout model gives you a structured approach to organizing content, making it easier to achieve complex layouts with simpler code.
Key Grid Terminologies
Before we get into coding, let’s go over a few important concepts:
-
Grid Container: The parent element with
display: grid;
, turning it into a grid container. - Grid Item: Any direct child of the grid container.
- Grid Line: Lines that divide rows and columns.
- Grid Track: The space between two adjacent grid lines, which can be a row or column.
Properties for Grid Containers
To work effectively with CSS Grid, you need to understand the properties you can apply to the grid container:
display: grid;
This property turns any block-level container into a grid container, enabling all its child elements to become grid items.
.container {
display: grid;
}
grid-template-columns
Defines the number and width of columns in your grid. You can use different units like px
, %
, fr
(fractional units), or auto
.
.container {
display: grid;
grid-template-columns: 100px 1fr 2fr;
}
grid-template-rows
Similar to columns, this property defines the height of rows.
.container {
display: grid;
grid-template-rows: 100px auto 1fr;
}
gap
, column-gap
, row-gap
These properties specify the space between grid items.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
}
Properties for Grid Items
Now, let’s look at properties specifically for grid items, which allow you to control the positioning and alignment within the grid.
grid-column
This property defines where an item starts and ends within the column tracks.
.item {
grid-column: 1 / 3;
}
grid-row
Similar to grid-column
, but for rows. It specifies the row-start and row-end positions for an item.
.item {
grid-row: 2 / span 2;
}
justify-self
and align-self
These properties control how a grid item aligns within its grid cell, either horizontally or vertically.
.item {
justify-self: center;
align-self: end;
}
Useful Functions
CSS Grid includes powerful functions for dynamic layouts:
repeat()
Automate repetitive tracks. repeat()
takes two arguments: the number of times to repeat and the track size.
.container {
grid-template-columns: repeat(3, 1fr);
}
minmax()
Define minimum and maximum size limits for grid tracks.
.container {
grid-template-columns: minmax(150px, 1fr);
}
fit-content()
Limits the track size based on the content.
.container {
grid-template-columns: fit-content(200px);
}
Conclusion
CSS Grid provides a modern, versatile layout solution that can transform your approach to web design. By understanding the fundamental concepts, properties, and functions of CSS Grid, you can create complex, responsive layouts with less code. This guide covered everything from basic properties to advanced functions, giving you the knowledge to get started on your CSS Grid journey.
That's all for today.
And also, share your favourite web dev resources to help the beginners here!
Connect with me:@ LinkedIn and checkout my Portfolio.
Explore my YouTube Channel! If you find it useful.
Please give my GitHub Projects a star ⭐️
Thanks for 32131! 🤗
Top comments (1)
Subscribe to my YouTube Channel if you find it helpful! Subscribing is free, and it will motivate me to stay active here. 😊