CSS Grid Layout, commonly known as CSS Grid, is a powerful two-dimensional layout system introduced in CSS3. It allows developers to design complex, responsive, and structured web layouts with ease. Unlike traditional layout methods such as floats, tables, or even Flexbox (which is one-dimensional), CSS Grid enables precise control over both rows and columns at the same time.
CSS Grid Basics form the foundation for creating professional layouts such as dashboards, galleries, landing pages, admin panels, and full website structures. It is widely used in modern web development and is supported by all major browsers.
This learning-focused guide explains CSS Grid from scratch, covering all essential grid concepts, properties, and practical examples. The content is structured for students, beginners, and developers who want a strong conceptual understanding along with real-world usage.
Before CSS Grid, developers relied on multiple hacks and workarounds to build layouts. CSS Grid solves many layout problems by offering:
CSS Grid is ideal for page-level layouts, while Flexbox is better suited for component-level alignment. Understanding CSS Grid basics allows developers to combine both systems efficiently.
A grid container is the parent element that has the grid layout applied. All direct children of the grid container automatically become grid items.
.container {
display: grid;
}
Grid items are the direct children of a grid container. They are placed into the grid cells according to grid rules.
Grid lines are the horizontal and vertical lines that divide the grid into rows and columns. They are numbered starting from 1.
A grid cell is the smallest unit of a grid formed by the intersection of a row and a column.
Grid tracks refer to rows and columns themselves. Each row or column is considered a track.
Grid areas are rectangular sections made up of one or more grid cells.
To create a grid layout, the first step is to define a grid container using display: grid.
.container {
display: grid;
grid-template-columns: 200px 200px 200px;
grid-template-rows: 100px 100px;
}
The above example creates a grid with three columns and two rows.
The grid-template-columns property defines the number and size of columns in a grid.
.container {
display: grid;
grid-template-columns: 150px 150px 150px;
}
.container {
display: grid;
grid-template-columns: 30% 40% 30%;
}
The fr unit represents a fraction of available space and is one of the most powerful features of CSS Grid.
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
The grid-template-rows property defines the height of rows in the grid.
.container {
display: grid;
grid-template-rows: 100px 200px;
}
The repeat function reduces repetition and improves readability in grid definitions.
.container {
display: grid;
grid-template-columns: repeat(4, 1fr);
}
The gap property controls the spacing between rows and columns.
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
Grid items can be placed manually using grid-column and grid-row properties.
.item1 {
grid-column: 1 / 3;
grid-row: 1 / 2;
}
.item2 {
grid-area: 2 / 1 / 3 / 4;
}
By default, CSS Grid automatically places items in the next available cell.
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
Aligns items horizontally within their grid cells.
.container {
justify-items: center;
}
Aligns items vertically within their grid cells.
.container {
align-items: center;
}
.container {
place-items: center;
}
.container {
justify-content: center;
}
.container {
align-content: space-between;
}
Grid template areas allow developers to design layouts visually using named areas.
.container {
display: grid;
grid-template-columns: 1fr 3fr;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
}
CSS Grid makes responsive design simple using media queries and flexible units.
.container {
display: grid;
grid-template-columns: 1fr;
}
@media (min-width: 768px) {
.container {
grid-template-columns: repeat(3, 1fr);
}
}
CSS Grid Basics provide the foundation for creating modern, responsive, and maintainable layouts. By mastering grid containers, grid items, rows, columns, gaps, and alignment properties, developers can build complex layouts with minimal effort.
This detailed guide is designed as a complete learning resource for students and developers who want to understand CSS Grid Layout from the ground up.
Content, padding, border, and margin make up the box model.
Relative moves from original position; absolute positions relative to nearest positioned ancestor.
id is unique; class can be reused.
Minify files, reduce specificity, and remove unused styles.
Overrides all other declarations, regardless of specificity.
Use margin: auto or flexbox/grid techniques.
Allow responsive design by applying styles based on screen size or device.
Define relationships between selectors: descendant ( ), child (>), adjacent (+), sibling (~).
Tools like SASS or LESS add features like variables and nesting to CSS.
Targets part of an element, like ::before or ::after.
Use @import "filename.css"; at the top of the file.
Controls stacking order of overlapping elements.
Forces a property to inherit value from parent.
Static β not affected by top, bottom, left, or right.
Use universal selector * or define styles in body/root.
em is relative to parent; rem is relative to root element.
Inline, internal (embedded), and external CSS.
A layout model for arranging elements in rows or columns with flexible sizing.
Targets elements in a specific state, like :hover or :nth-child().
Use fluid layouts, media queries, and relative units.
CSS styles HTML elements to control layout, color, fonts, and responsiveness.
Reusable custom property values, declared with --var-name.
Determines which rule applies when multiple rules target the same element.
Performs calculations to dynamically set CSS property values.
Copyrights © 2024 letsupdateskills All rights reserved