Modern CSS3 Tutorial – Flexbox, Grid & Responsive Design
Master modern CSS3: Flexbox, CSS Grid, media queries, and responsive design. Style your HTML pages and make them mobile-friendly with real code examples.
Introduction: Why CSS3 Matters
HTML5 gives structure; CSS3 gives design. Modern CSS3 lets you create layouts that adapt beautifully to phones, tablets, and desktops. In this tutorial, you will learn Flexbox, Grid, and responsive design with practical, copy-pasteable examples.
1. Setting Up Your CSS
Create a file named style.css and link it in your HTML:
<link rel="stylesheet" href="style.css">
2. CSS Basics Refresher
body {
font-family: Arial, sans-serif;
margin: 0;
background-color: #f9f9f9;
color: #333;
}
h1 {
color: #1a73e8;
}
3. Flexbox: One-Dimensional Layouts
Flexbox is perfect for aligning items in a row or column.
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
background: #1a73e8;
color: white;
}
Centering Items Vertically and Horizontally
.hero {
display: flex;
justify-content: center;
align-items: center;
min-height: 60vh;
text-align: center;
}
Flex Wrap
.card-container {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 250px;
background: white;
padding: 1rem;
border-radius: 8px;
}
4. CSS Grid: Two-Dimensional Layouts
Grid is ideal for page layouts with rows and columns.
.grid-layout {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-rows: auto 1fr auto;
gap: 1rem;
min-height: 100vh;
}
header, footer {
grid-column: 1 / -1;
background: #1a73e8;
color: white;
padding: 1rem;
}
aside {
background: #e8f0fe;
padding: 1rem;
}
main {
padding: 1rem;
}
Responsive Card Grid
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
padding: 2rem;
}
The auto-fit and minmax() combination creates a fully responsive grid with no media queries.
5. Responsive Design with Media Queries
Media queries adapt styles to screen size.
/* Default: mobile-first styles */
.container {
padding: 1rem;
}
/* Tablets and up */
@media (min-width: 600px) {
.container {
padding: 2rem;
}
}
/* Desktops and up */
@media (min-width: 1024px) {
.grid-layout {
grid-template-columns: 250px 1fr;
}
}
Mobile-First Principles
- Start with styles for small screens.
- Use
min-widthmedia queries to add complexity. - Use relative units like
remand%. - Always include the viewport meta tag.
6. Building a Mobile-Friendly Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Responsive Page</title>
</head>
<body>
<div class="grid-layout">
<header>My Site</header>
<aside>Sidebar</aside>
<main>Main Content</main>
<footer>© 2026 Eduvalle</footer>
</div>
</body>
</html>
7. Best Practices
- Use a reset or normalize CSS for consistency.
- Keep specificity low to avoid conflicts.
- Use CSS variables for colors and spacing.
- Test on real devices, not just browser resizing.
Conclusion
Flexbox handles one-dimensional layouts; Grid handles two-dimensional ones. Together with media queries, they form the foundation of modern responsive web design. Practice by rebuilding this layout and experimenting with different breakpoints.
Ready to Launch Your Website Online?
Once you finish building your project locally, you need fast and reliable hosting to put it online. Students and developers can get an automatic 20% discount (code: AR10-5) on their first order at Hostnin NVMe Web Hosting (plans start from ৳149/mo with free SSL and bKash/Nagad support), or view full details in our Eduvalle Student Shop.
Whether you are launching an online store, agency, or portfolio without coding from scratch, explore finished, mobile-ready websites with free full setup and 100% code ownership at Nowvelle.
Eduvalle-এর একাডেমিক টিম: HSC ও ভর্তি পরীক্ষার প্রস্তুতির জন্য যাচাইকৃত নোট ও কুইজ।