Comprehensive Example of CSS Flexbox Layout
Objective
To demonstrate a simple but comprehensive example of a layout using CSS Flexbox, illustrating the versatility and power of this layout model.
Scenario
Let's create a typical webpage layout with a header, a navigation bar, a main content area, a sidebar, and a footer.
This layout will adapt to different screen sizes using Flexbox.
<!DOCTYPE html>
<html>
<head>
<title>Flexbox Layout Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>Header</header>
<nav>Navigation Bar</nav>
<div class="main-container">
<main>Main Content</main>
<aside>Sidebar</aside>
</div>
<footer>Footer</footer>
</body>
</html>
Here, we have defined a basic HTML structure with a `<header>`, `<nav>`, `<main>`, `<aside>`, and `<footer>`.
CSS with Flexbox
Put this code into file named: styles.css
```css
body {
margin: 0;
font-family: Arial, sans-serif;
}
header, nav, main, aside, footer {
padding: 20px;
border: 1px solid #ddd;
}
header, footer {
background-color: #f3f3f3;
}
.main-container {
display: flex;
flex-wrap: wrap;
}
main {
flex: 1; /* Flex grow */
min-width: 200px; /* Minimum width before wrapping */
}
aside {
flex-basis: 200px; /* Base width of the sidebar */
}
nav {
background-color: #e3e3e3;
}
```
- The `.main-container` class is given `display: flex;` making it a flex container.
- `flex-wrap: wrap;` allows flex items to wrap onto multiple lines, from top to bottom.
- For `<main>`, `flex: 1;` allows it to grow and fill up the available space.
- `<aside>` is given a `flex-basis` of `200px`, setting its initial size.
- `min-width` in `<main>` ensures content doesn't get too squished.
Explanation of Flexbox Properties
1. **Flex Container (`display: flex;`):** The `.main-container` becomes a flex container, allowing direct children (`<main>` and `<aside>`) to lay out flexibly.
2. **Flex Item (`flex` property):** The `flex` property on `<main>` tells it to grow and take up any available space, ensuring the main content area is flexible and responsive.
3. **Flex Basis (`flex-basis`):** The `flex-basis` on `<aside>` sets a base size for the sidebar, dictating how it flexes relative to other elements.
4. **Flex Wrap (`flex-wrap`):** `flex-wrap: wrap;` allows elements to wrap onto multiple lines instead of squeezing into a single row, enhancing responsiveness.
Result
With this setup:
- The header and footer span the full width of the page.
- The navigation bar is also full-width, sitting below the header.
- The main content and sidebar are side by side. If the screen size reduces, the sidebar will wrap below the main content due to `flex-wrap`.
- The sidebar maintains a consistent width, and the main content area adjusts flexibly.
Conclusion
This example demonstrates the power of CSS Flexbox in creating a flexible, responsive web page layout.
Flexbox's simplicity and efficiency in handling both horizontal and vertical alignment make it an excellent choice for modern web design.
Experiment with these properties in your own projects to get a better grasp of their capabilities.