Modern web development requires solid understanding of CSS layout techniques. In this comprehensive guide, we'll explore CSS Grid and Flexbox - the two most powerful layout systems available today.
Understanding the Fundamentals
When to Use Flexbox vs Grid
Use Flexbox for:
- One-dimensional layouts (rows OR columns)
- Component-level layouts
- Distributing space among items
- Aligning items within containers
Use Grid for:
- Two-dimensional layouts (rows AND columns)
- Page-level layouts
- Complex grid systems
- Precise item placement
Flexbox Deep Dive
Basic Flexbox Setup
.flex-container {
display: flex;
flex-direction: row; /* default */
justify-content: space-between;
align-items: center;
gap: 1rem;
}
.flex-item {
flex: 1; /* flex-grow: 1, flex-shrink: 1, flex-basis: 0% */
}
Practical Flexbox Examples
1. Navigation Bar
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
}
.nav-links {
display: flex;
gap: 2rem;
list-style: none;
}
2. Card Layout
.card {
display: flex;
flex-direction: column;
height: 100%;
}
.card-content {
flex: 1; /* Take up remaining space */
}
.card-footer {
margin-top: auto; /* Push to bottom */
}
3. Centering Content
.center-container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
CSS Grid Mastery
Grid Container Setup
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-template-rows: auto 1fr auto;
gap: 2rem;
min-height: 100vh;
}
Advanced Grid Techniques
1. Named Grid Lines
.layout {
display: grid;
grid-template-columns:
[sidebar-start] 250px
[sidebar-end main-start] 1fr
[main-end];
grid-template-rows:
[header-start] 60px
[header-end content-start] 1fr
[content-end footer-start] 40px
[footer-end];
}
.header {
grid-column: sidebar-start / main-end;
grid-row: header-start / header-end;
}
2. Grid Template Areas
.page-layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main aside"
"footer footer footer";
grid-template-columns: 200px 1fr 200px;
grid-template-rows: 60px 1fr 40px;
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }
3. Responsive Grid
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
}
/* Mobile-first approach */
@media (max-width: 768px) {
.responsive-grid {
grid-template-columns: 1fr;
}
}
Combining Grid and Flexbox
The real power comes from using both together:
/* Grid for page layout */
.page {
display: grid;
grid-template-areas:
"header"
"main"
"footer";
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
/* Flexbox for component layout */
.header {
grid-area: header;
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
}
.card-grid {
grid-area: main;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 2rem;
padding: 2rem;
}
.card {
display: flex;
flex-direction: column;
border-radius: 8px;
overflow: hidden;
}
Modern CSS Features
Container Queries
.card-container {
container-type: inline-size;
}
@container (max-width: 400px) {
.card {
flex-direction: column;
}
}
@container (min-width: 401px) {
.card {
flex-direction: row;
}
}
Logical Properties
.content {
padding-inline: 2rem; /* left and right in LTR */
padding-block: 1rem; /* top and bottom */
margin-inline-start: auto; /* margin-left in LTR */
margin-inline-end: auto; /* margin-right in LTR */
}
Performance Tips
- Avoid nested grids when possible - use subgrid when supported
- Use
will-changesparingly for animations - Prefer
transformover changing layout properties - Test on various devices and screen sizes
/* Efficient animation */
.card {
transition: transform 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
}
Browser Support and Fallbacks
/* Fallback for older browsers */
.grid-container {
display: flex;
flex-wrap: wrap;
}
/* Progressive enhancement */
@supports (display: grid) {
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
}
}
Conclusion
Mastering CSS Grid and Flexbox opens up endless possibilities for creating beautiful, responsive layouts. Key takeaways:
- Use the right tool for the job: Grid for 2D layouts, Flexbox for 1D
- Mobile-first approach: Start with mobile layouts and enhance
- Progressive enhancement: Use feature queries for better browser support
- Practice regularly: Build projects to reinforce your understanding
Start implementing these techniques in your projects, and you'll soon discover how powerful modern CSS can be!
Resources
- CSS Grid Guide - MDN
- Flexbox Guide - CSS Tricks
- Grid by Example
- Flexbox Froggy - Learn through games!