Introduction
Modern CSS has evolved tremendously, giving developers powerful tools to create sophisticated layouts without relying on external frameworks. In this comprehensive guide, we'll explore CSS Grid and Flexbox - two revolutionary layout systems that have transformed how we build responsive websites.
Whether you're building a simple blog or a complex web application, mastering these techniques will make your development workflow faster and your layouts more maintainable.
1. CSS Grid - The Two-Dimensional Layout System
CSS Grid is perfect for creating complex, two-dimensional layouts. It allows you to control both rows and columns simultaneously, making it ideal for page layouts, image galleries, and dashboard interfaces.
Basic Grid Setup
/* Create a responsive grid layout */
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
padding: 20px;
}
auto-fit
with minmax()
for truly responsive grids that adapt to any screen size without media queries!
2. Flexbox - The One-Dimensional Layout Master
Flexbox excels at distributing space along a single axis (row or column). It's perfect for navigation bars, card layouts, and centering content both vertically and horizontally.
Flexible Navigation Bar
/* Perfect centered layout with Flexbox */
.flex-container {
display: flex;
justify-content: space-between;
align-items: center;
gap: 15px;
flex-wrap: wrap;
}
3. Combining Grid and Flexbox
The real power comes from combining both systems. Use Grid for your overall page layout and Flexbox for components within those grid areas.
/* Modern dashboard layout */
.dashboard {
display: grid;
grid-template-areas:
"header header header"
"sidebar content content"
"footer footer footer";
grid-template-columns: 250px 1fr 1fr;
gap: 20px;
min-height: 100vh;
}
4. Modern CSS Features to Level Up
CSS Custom Properties (Variables)
:root {
--primary-color: #667eea;
--secondary-color: #764ba2;
--spacing: 20px;
--border-radius: 12px;
}
Container Queries (Now Supported!)
/* Responsive based on container, not viewport */
.container {
container-type: inline-size;
}
@container (min-width: 500px) {
.card {
display: flex;
gap: 20px;
}
}
5. Real-World Example: Pricing Table with Grid
Let's put everything into practice with a professional pricing table. This example demonstrates Grid layout, custom properties, gradients, and responsive design - all working together.
The Code
/* Responsive pricing grid */
.pricing-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 30px;
margin: 40px 0;
}
Basic
- ✓ 5 Projects
- ✓ 10GB Storage
- ✓ Email Support
- ✓ Basic Analytics
Professional
- ✓ Unlimited Projects
- ✓ 100GB Storage
- ✓ Priority Support
- ✓ Advanced Analytics
- ✓ Custom Domain
Enterprise
- ✓ Everything in Pro
- ✓ Unlimited Storage
- ✓ 24/7 Phone Support
- ✓ Dedicated Manager
- ✓ Custom Integration
Key Takeaways
✅ Use CSS Grid when: You need control over both rows and columns, creating page layouts, or building complex structured interfaces.
✅ Use Flexbox when: You're aligning items in a single direction, creating navigation menus, or centering content.
✅ Combine both: Grid for macro-layout, Flexbox for micro-layout within components.
✅ Modern CSS: Leverage custom properties, container queries, and other modern features for maintainable, scalable code.