Lab 0: Warm up and getting Started:
Lab Instruction Workflow: Responsive Web Page Layout with Flexbox
Objective:
Learn to create a flexible and responsive web page layout using HTML and CSS Flexbox.
Part 1: Setup
Task 1: Create a new HTML file named flexbox-layout.html.
Task 2: Create a new CSS file named flexbox-style.css.
Part 2: HTML Structure
flexbox-layout.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox Layout</title>
<link rel="stylesheet" href="flexbox-style.css">
</head>
<body>
<header class="main-header">Header</header>
<nav class="main-nav">Navigation</nav>
<section class="main-content">Content</section>
<aside class="sidebar">Sidebar</aside>
<footer class="main-footer">Footer</footer>
</body>
</html>
Task 3: Link your CSS file within the HTML head section.
Task 4: Create the basic structure of a web page with header, navigation, content, sidebar, and footer.
Part 3: CSS Styling with Flexbox
flexbox-style.css:
body {
margin: 0;
font-family: Arial, sans-serif;
}
.main-header, .main-nav, .main-footer {
text-align: center;
padding: 1rem;
background-color: #333;
color: white;
}
.main-content, .sidebar {
padding: 1rem;
}
/* Flexbox container */
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.main-content {
flex: 1;
}
/* Responsive design */
@media (min-width: 768px) {
body {
flex-direction: row;
}
.main-nav, .main-content, .sidebar {
flex: 1;
}
.sidebar {
order: -1; /* Move sidebar to the left side */
}
}
Task 5: Add CSS to style each section of the page.
Use Flexbox to create a column layout for mobile and switch to a row layout for wider screens.
##############
To complete Task 5, we will add additional CSS to style each section of the page and use Flexbox to create a column layout for mobile devices and a row layout for wider screens.
We will utilize Flexbox properties to make the layout responsive, with a column structure on small screens and a row structure on larger screens (above 768px width).
Here’s the updated CSS that you will add to the flexbox-style.css file:
CSS: flexbox-style.css
/* Base styling for body and layout structure */
body {
margin: 0;
font-family: Arial, sans-serif;
display: flex;
flex-direction: column; /* Stack sections vertically by default */
min-height: 100vh; /* Full height of the viewport */
}
/* Header, Navigation, Footer styles */
.main-header, .main-nav, .main-footer {
text-align: center;
padding: 1rem;
background-color: #333;
color: white;
}
/* Main content and sidebar */
.main-content, .sidebar {
padding: 1rem;
}
/* Flexbox: allow main content to grow and take up space */
.main-content {
flex: 1;
background-color: #f4f4f4; /* Light background for content */
}
.sidebar {
background-color: #eaeaea; /* Slightly different color for sidebar */
padding: 1rem;
}
/* Footer styling */
.main-footer {
background-color: #444; /* Darker footer background */
color: white;
padding: 1rem;
text-align: center;
}
/* Responsive design for wider screens */
@media (min-width: 768px) {
/* Switch to a row layout when screen width is 768px or more */
body {
flex-direction: row;
}
/* Header stays at the top (spans entire width) */
.main-header {
width: 100%;
}
/* Navigation, content, and sidebar aligned side by side */
.main-nav, .main-content, .sidebar {
flex: 1;
padding: 1rem;
}
/* Sidebar to the left on wider screens */
.sidebar {
order: -1; /* Move sidebar to the left of content */
flex-basis: 20%; /* Sidebar takes up 20% of space */
}
/* Content area takes more space */
.main-content {
flex-basis: 60%; /* Content takes up 60% of space */
}
/* Navigation bar */
.main-nav {
flex-basis: 20%; /* Navigation takes up 20% of space */
background-color: #555; /* Darker background for nav */
color: white;
}
}
Explanation of the CSS:
Basic Styling for Layout Elements: We apply padding and background colors to the header, navigation, sidebar, and footer to differentiate them visually. The header and footer span the full width of the viewport and have center-aligned text for a clean layout. flex-direction: column; ensures that sections stack vertically on smaller screens (mobile-first approach). min-height: 100vh; ensures that the body takes up the full height of the viewport, making sure the footer is placed correctly at the bottom. Content and Sidebar Layout: The .main-content section uses flex: 1; to make it expand and take up available space, ensuring it fills up most of the height on small screens. The .sidebar is initially placed below the content on mobile (column layout). Media Queries for Responsive Design: At 768px screen width and above, the layout switches to a row layout using flex-direction: row; on the <body> element. The flex-basis property is used to specify how much space each section should take in the row layout: .sidebar takes 20% of the space on the left. .main-content takes up 60% of the space in the center. .main-nav takes 20% of the space on the right (or wherever it is placed in the flow). The order: -1; on the .sidebar moves the sidebar to the left in the row layout, since by default Flexbox orders items as they appear in the HTML. Navigation and Footer Styling: The navigation and footer both have darker backgrounds (#333 for the header and #555 for the navigation). The .main-footer remains at the bottom with consistent padding, and it spans the entire width of the page. Final HTML: flexbox-layout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox Layout</title>
<link rel="stylesheet" href="flexbox-style.css">
</head>
<body>
<header class="main-header">Header</header>
<nav class="main-nav">Navigation</nav>
<section class="main-content">Content</section>
<aside class="sidebar">Sidebar</aside>