CSS Grid has transformed how we design and layout content on the web. It offers a two-dimensional layout system, providing a more flexible and efficient way to design web pages. For those still getting a grasp of CSS Grid or looking for a quick reference guide, here’s a handy cheat sheet to help you master the basics.
To create a grid, you can define columns and rows on a container.
.container { display: grid; grid-template-columns: 1fr 2fr 1fr; /* 3 columns where middle column is twice the size of others */ grid-template-rows: auto 150px; /* 2 rows, one with automatic height and another with fixed height */ }
Create space between grid items.
.container { grid-gap: 15px; /* Gap between rows & columns */ grid-row-gap: 15px; /* Gap only between rows */ grid-column-gap: 15px; /* Gap only between columns */ }
.item { grid-column-start: 1; grid-column-end: 3; grid-row-start: 1; grid-row-end: 2; }
.item { grid-column: span 2; /* Spans across 2 columns */ grid-row: span 1; /* Spans across 1 row */ }
.container { grid-template-areas: "header header header" "sidebar content aside" "footer footer footer"; } .header { grid-area: header; } .sidebar { grid-area: sidebar; } ...
Align items (Vertically):
Justify items (Horizontally):
Align content:
Justify content:
Combine CSS Grid with media queries to create responsive layouts. For instance:
.container { grid-template-columns: repeat(2, 1fr); } @media (min-width: 768px) { .container { grid-template-columns: repeat(4, 1fr); } }
CSS Grid revolutionizes the realm of web design by introducing a two-dimensional layout system that offers unparalleled flexibility and efficiency. Whether it’s defining columns and rows, manipulating spaces between grid items, or placing and spanning them across the grid, CSS Grid provides designers and developers with a robust set of tools. The added convenience of alignment, justification, and the potential for seamless responsive designs further cements its essential role in modern web development. For both novices aiming to delve into the world of web design and seasoned developers seeking a quick refresher, this cheat sheet captures the foundational elements of the CSS Grid. As with any tool, regular practice and application will lead to mastery. Dive into your next project with these Grid properties at your fingertips!
Quick Links
Legal Stuff