Project: Colored Flexbox Boxes
This project will create a webpage with a flex container holding three boxes, each with a different color (red, blue, green). The boxes will use flex: 1 to distribute space equally, and I’ll include variations to demonstrate other Flexbox properties like flex-grow, flex-shrink, and flex-basis. The layout will be responsive, and I’ll add hover effects to make it interactive.
HTML (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Flexbox Colored Boxes</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="container">
<div class="box red">Red Box</div>
<div class="box blue">Blue Box</div>
<div class="box green">Green Box</div>
</div>
</body>
</html>
CSS (styles.css)
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
}
.container {
display: flex;
width: 80%;
max-width: 1200px;
min-height: 200px;
background-color: #fff;
border: 2px solid #333;
padding: 10px;
gap: 10px; /* Space between flex items */
}
.box {
flex: 1; /* Shorthand for flex-grow: 1, flex-shrink: 1, flex-basis: 0% */
color: white;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2rem;
transition: flex 0.3s ease;
}
.red {
background-color: #ff0000;
}
.blue {
background-color: #0000ff;
}
.green {
background-color: #00ff00;
}
/* Hover effect to demonstrate flex changes */
.box:hover {
flex: 2; /* On hover, this box grows twice as much as others */
}
/* Responsive design for smaller screens */
@media (max-width: 600px) {
.container {
flex-direction: column; /* Stack boxes vertically */
}
.box {
flex: 1 1 auto; /* Allow boxes to take full width */
min-height: 100px;
}
}
Explanation of Flexbox and flex: 1
What is Flexbox?
Flexbox is a CSS layout module designed to arrange items in a container, either in a row or column, with flexible sizing and alignment. It’s ideal for creating responsive layouts where items can grow, shrink, or maintain specific sizes based on available space.
Key Flexbox Properties Used
display: flex (on
.container):- Turns the container into a flex container, making its direct children (the boxes) flex items.
- By default, flex items are laid out in a row (based on
flex-direction: row).
flex: 1 (on
.box):flexis a shorthand for three properties:- flex-grow: 1: Allows the item to grow to fill available space.
- flex-shrink: 1: Allows the item to shrink if there’s not enough space.
- flex-basis: 0%: Sets the initial size of the item to 0, so it relies on content or available space.
- With
flex: 1, all boxes share the container’s width equally (because they all grow at the same rate).
gap: 10px:
- Adds a 10px gap between flex items, making the layout visually cleaner.
flex-direction: column (in media query):
- On smaller screens (below 600px), the boxes stack vertically instead of horizontally.
Hover Effect:
- When you hover over a box, its
flexvalue changes toflex: 2, making it grow twice as much as the others, demonstrating howflex-growaffects layout dynamically.
- When you hover over a box, its
Other Properties:
- justify-content: center and align-items: center (on
.containerand.box): Center the content horizontally and vertically. - transition: flex 0.3s ease: Smoothly animates changes in the
flexproperty on hover. - min-height: 100vh (on
body): Ensures the body takes up the full viewport height, centering the container vertically.
- justify-content: center and align-items: center (on
How the Colored Boxes Demonstrate Flexbox
- Red, Blue, Green Boxes: Each box has
flex: 1, so they divide the container’s width equally (approximately 33.33% each, minus gaps). - Hover Interaction: Hovering over a box increases its
flex-growto 2, causing it to take up more space while the others shrink proportionally. - Responsive Behavior: On screens smaller than 600px, the boxes stack vertically, each taking the full width, showing how
flex-directionchanges the layout.
How to Run the Project
Create Files:
- Save the HTML code in a file named
index.html. - Save the CSS code in a file named
styles.cssin the same directory.
- Save the HTML code in a file named
Open in Browser:
- Open
index.htmlin a web browser (e.g., Chrome, Firefox). - You’ll see three colored boxes (red, blue, green) side by side in a white container with a border.
- Open
Interact:
- Hover over any box to see it expand (demonstrating
flex-grow). - Resize the browser window below 600px to see the boxes stack vertically.
- Hover over any box to see it expand (demonstrating
Understanding flex: 1 Through the Project
- Equal Sizing: With
flex: 1, all boxes start with equal width because they grow equally to fill the container. - Dynamic Resizing: The hover effect shows how changing
flex-grow(viaflex: 2) makes one box take more space. - Shrink and Grow: If you add more content to one box (e.g., more text), it may try to grow or shrink based on
flex-shrinkand available space. - Responsive Layout: The media query shows how Flexbox adapts to different screen sizes by changing
flex-direction.
Variations to Experiment With
To deepen your understanding, try modifying the CSS:
- Change flex-grow:
- Set
.red { flex: 2; }to make the red box twice as wide as the others initially.
- Set
- Set flex-basis:
- Try
.box { flex: 1 1 100px; }to give each box a starting width of 100px before growing/shrinking.
- Try
- Disable flex-shrink:
- Use
.box { flex: 1 0 0; }to prevent boxes from shrinking below their content size.
- Use
- Change flex-direction:
- Set
.container { flex-direction: column; }to stack boxes vertically by default.
- Set
- Add more boxes:
- Add another
<div class="box" style="background-color: purple;">Purple Box</div>to see howflex: 1divides space among more items.
- Add another
Visualizing Flexbox Behavior
Since you didn’t explicitly ask for a chart, I won’t generate one, but I can describe how you might visualize Flexbox behavior:
- Imagine a bar chart where each bar represents a box’s width. With
flex: 1, all bars would be equal. On hover, the hovered box’s bar would double in width, while others shrink proportionally.
Additional Notes
- Browser Compatibility: Flexbox is supported in all modern browsers. Use
box-sizing: border-boxto ensure padding and borders don’t break the layout.
This project should give you a hands-on understanding of flex: 1 and Flexbox.