Objective: Understand how to use CSS Flexbox & Grid to build modern, responsive page layouts.
✅ Introduction to CSS Layouts
✅ Flexbox Basics (Aligning & Spacing Elements)
✅ CSS Grid Basics (Creating Grid-Based Layouts)
✅ Responsive Design with Media Queries
✅ Combining Flexbox & Grid for Best Results
🎯 CSS provides multiple ways to arrange elements on a page.
📌 Common Layout Techniques:
✅ Floats & Inline-Block (Old methods – rarely used now)
✅ Flexbox (Best for one-dimensional layouts like menus & cards)
✅ CSS Grid (Best for complex, two-dimensional layouts like full web pages)
🔹 Example: Old Method Using Floats (❌ Outdated)
.container {
width: 100%;
}
.left {
float: left;
width: 50%;
}
.right {
float: right;
width: 50%;
}🎯 Better Alternative: Use Flexbox or Grid!
🎯 Flexbox makes it easy to align, distribute, and space elements inside a container.
🔹 Basic Flexbox Example:
.container {
display: flex;
}🔹 HTML Structure:
<div class="container">
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="box">Box 3</div>
</div>🔹 CSS: Basic Flexbox Layout
.container {
display: flex;
justify-content: space-between;
}
.box {
width: 100px;
height: 100px;
background-color: lightblue;
text-align: center;
line-height: 100px;
}🎯 Bonus: Try using justify-content: center;
Applies Flexbox behavior to a container.
.container {
display: flex;
}justify-content: flex-start; /* Default */
justify-content: center; /* Center elements */
justify-content: space-between; /* Even spacing */
justify-content: space-around; /* Spacing with margins */align-items: flex-start; /* Align to the top */
align-items: center; /* Center elements */
align-items: flex-end; /* Align to the bottom */flex-wrap: wrap; /* Allow wrapping to new lines */🎯 Bonus: Try setting flex-direction: column;
🎯 CSS Grid is great for building full-page layouts!
🔹 Basic Grid Example:
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}🔹 HTML Structure:
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>🔹 CSS: Basic Grid Layout
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.item {
background-color: lightcoral;
padding: 20px;
text-align: center;
}🎯 Bonus: Try using grid-template-rows: repeat(2, 100px);
Defines the number of columns and rows.
grid-template-columns: 200px 200px; /* Two equal columns */
grid-template-columns: 1fr 2fr 1fr; /* Flexible columns */
grid-template-rows: 100px 200px; /* Two rows with different heights */gap: 20px; /* Adds spacing between items */grid-column: span 2; /* Makes an item take two columns */
grid-row: span 2; /* Makes an item take two rows */🎯 Bonus: Try setting place-items: center;
🎯 Make layouts responsive by using media queries.
🔹 Example: Changing Layout for Mobile Screens
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}🎯 Bonus: Try setting different grid-template-columns for mobile.
📌 Task: Build a responsive two-column layout using Flexbox or Grid.
🔹 HTML Structure:
<div class="container">
<div class="left">Left Column</div>
<div class="right">Right Column</div>
</div>🔹 CSS (Flexbox Version):
.container {
display: flex;
gap: 20px;
}
.left, .right {
flex: 1;
padding: 20px;
background-color: lightgray;
}
/* Responsive Design */
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}🎯 Bonus: Try the Grid version of the same layout!
✅ Practice with Flexbox & Grid layouts.
✅ Build a simple page layout using Flexbox.
✅ Try making a full website layout using Grid.
📢 Next Lesson: Lesson 4: Responsive Web Design & Mobile Optimization! 📱
📌 CSS Flexbox Guide (CSS-Tricks)
🔗 https://css-tricks.com/snippets/css/a-guide-to-flexbox/
📌 CSS Grid Guide (MDN Docs)
🔗 https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout
📌 CSS Responsive Design (W3Schools)
🔗 https://www.w3schools.com/css/css_rwd_intro.asp
✅ We learned how to use Flexbox & Grid for layouts.
✅ We covered alignments, spacing, and responsive design.
✅ We prepared for making fully responsive websites in the next lesson.