- Animation
- Background image
- Border Color
- Border radius
- Border width
- Borders
- Box model
- Box shadow
- Class attribute
- Clip path
- Color
- Comment
- Display property
- First-child selector
- Flexbox
- Font family
- Font size
- Font style
- Font weight
- Gradient
- Grid layout
- Height
- Linking a style sheet
- Margin
- Media query
- N-th-child selector
- Overflow property
- Padding
- Pixels
- Position property
- Position property
- Pseudo-classes
- Pseudo-elements
- Rounding an image
- Selectors
- Specificity
- Text align
- Transition property
- Units
- Variable
- Width
- Z-index
CSS
CSS Grid Layout: Syntax, Usage, and Examples
CSS Grid Layout provides a two-dimensional system for arranging elements in rows and columns. Unlike Flexbox, which works primarily in one dimension, Grid gives you precise control over both axes, making it ideal for complex layouts.
How to Use CSS Grid Layout
To create a grid, apply display: grid
to the container. Then, define rows and columns using grid-template-rows
and grid-template-columns
.
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: 100px 200px;
}
This creates a three-column grid where the second column is twice as wide as the others, along with two rows of fixed heights.
When to Use CSS Grid Layout
Use Grid when you need precise control over both rows and columns. Here are three common scenarios:
1. Creating Complex Page Layouts
Web pages often contain multiple sections, such as headers, sidebars, and footers. Grid simplifies structuring these elements.
.container {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 1fr 3fr;
grid-template-rows: auto;
}
Assigning grid-template-areas
clarifies the layout and organizes content efficiently.
2. Aligning Items Precisely
Grid makes alignment straightforward without requiring additional CSS tricks. By setting align-items
and justify-items
, you can control item positioning inside the grid container.
.container {
display: grid;
place-items: center;
}
This centers all grid items inside their respective grid cells.
3. Responsive Grid Layouts
Grid enables elements to adjust dynamically based on screen size. Using repeat()
and minmax()
, you can create a fully responsive layout.
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
This ensures items scale dynamically according to available space.
Examples of CSS Grid Layout in Action
Creating a Basic Grid
You can define a simple three-column grid with equal spacing using this setup:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
This configuration creates three equal-width columns with a 10px gap between them.
Overlapping Elements
You can make elements span across multiple grid cells by specifying their position explicitly.
.container {
display: grid;
}
.item {
grid-column: 1 / span 2;
grid-row: 1 / span 2;
}
This makes the item expand across two columns and two rows.
Centering Content with Grid
Grid allows you to center an item both horizontally and vertically effortlessly.
.container {
display: grid;
place-items: center;
height: 100vh;
}
This ensures the item remains centered regardless of screen size.
Learn More About CSS Grid Layout
CSS Grid Layout Advantages
CSS Grid offers several benefits over other layout techniques:
- Precise control over both horizontal and vertical dimensions.
- Less reliance on media queries due to flexible sizing options.
- Simplifies alignment without requiring additional positioning properties.
- Works well with responsive designs using
auto-fit
andminmax()
.
CSS Grid Layout vs. Table Grid Layout
While tables use <table>
elements for structure, Grid achieves the same layout while being more flexible. Unlike tables, Grid lets you reorder, align, and resize elements dynamically.
CSS Grid Layout Examples for Responsive Design
A responsive gallery where items adapt dynamically:
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
}
This configuration ensures the layout remains flexible across different screen sizes.
CSS Grid Layout streamlines web design by offering powerful tools for structuring layouts efficiently. Whether you're designing a responsive site, a complex interface, or a simple grid-based component, Grid provides unmatched flexibility and control.
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.