Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Align Boxes Using Justify Content
📖 Scenario: You are creating a simple webpage section that displays three colored boxes in a row. You want to control how these boxes are spaced horizontally inside their container.
🎯 Goal: Build a webpage with a container holding three boxes. Use CSS Flexbox and the justify-content property to align the boxes horizontally in different ways.
📋 What You'll Learn
Create a container with three boxes inside
Use CSS Flexbox on the container
Add a CSS variable --justify-value to control the justify-content property
Apply the justify-content property using the CSS variable
Boxes should have distinct background colors and fixed size
The layout should be responsive and accessible
💡 Why This Matters
🌍 Real World
Web developers often need to arrange elements horizontally with flexible spacing. Using justify-content with flexbox is a common way to do this in navigation bars, galleries, or toolbars.
💼 Career
Understanding flexbox and justify-content is essential for front-end developers to create responsive and well-aligned layouts that work on different screen sizes and devices.
Progress0 / 4 steps
1
Create the HTML container and boxes
Write HTML code to create a <section> element with class container. Inside it, add three <div> elements with classes box1, box2, and box3. Each box should contain text: "Box 1", "Box 2", and "Box 3" respectively.
CSS
Hint
Use a <section> with class container. Inside, add three <div> elements with the exact classes and text.
2
Add CSS Flexbox and a CSS variable for justify-content
In a <style> block or CSS file, create a CSS rule for .container. Set display: flex; and define a CSS variable called --justify-value with the initial value flex-start.
CSS
Hint
Use display: flex; on .container. Define --justify-value: flex-start; inside the same rule.
3
Apply justify-content using the CSS variable and style boxes
In the .container CSS rule, add justify-content: var(--justify-value);. Then create CSS rules for .box1, .box2, and .box3 to set their width to 6rem, height to 6rem, and background colors to #ff6666, #66ff66, and #6666ff respectively.
CSS
Hint
Use justify-content: var(--justify-value); inside .container. Set width, height, and background-color for each box as described.
4
Make the container responsive and add accessibility
Add a CSS media query for screens narrower than 30rem to change flex-direction of .container to column. Also add aria-label="Box container" attribute to the <section> element for accessibility.
CSS
Hint
Add aria-label="Box container" to the <section>. Use a media query for max-width 30rem to set flex-direction: column; on .container.
Practice
(1/5)
1. What does the CSS property justify-content control in a flex container?
easy
A. The vertical alignment of items inside the container
B. The horizontal alignment of items inside the container
C. The size of the container
D. The color of the items
Solution
Step 1: Understand the role of justify-content
This property controls how items are aligned horizontally inside a flex or grid container.
Step 2: Differentiate from vertical alignment
Vertical alignment is controlled by align-items, not justify-content.
Final Answer:
The horizontal alignment of items inside the container -> Option B
Quick Check:
Justify content = horizontal alignment [OK]
Hint: Justify content aligns items left to right [OK]
Common Mistakes:
Confusing justify-content with align-items
Thinking it changes item size
Assuming it controls colors
2. Which of the following is the correct syntax to center items horizontally using justify-content in a flex container?
easy
A. justify-content: center-items;
B. justify-content: middle;
C. justify-content: align-center;
D. justify-content: center;
Solution
Step 1: Recall valid values for justify-content
Common valid values include flex-start, center, space-between, and space-around.
Step 2: Identify the correct syntax for centering
The correct value to center items horizontally is center, so the syntax is justify-content: center;.
Final Answer:
justify-content: center; -> Option D
Quick Check:
Centering uses 'center' value [OK]
Hint: Use 'center' exactly to center items [OK]
Common Mistakes:
Using invalid values like 'middle' or 'align-center'
Missing the colon or semicolon
Confusing with align-items syntax
3. Given this CSS and HTML, what will be the horizontal spacing of the items inside the container?
A. The container must have display: flex; or display: grid; for justify-content to work
B. The property name should be justify-items instead
C. The value 'center' is invalid for justify-content
D. The container needs a fixed width
Solution
Step 1: Check the display property
justify-content only works on flex or grid containers, but here display is set to block.
Step 2: Understand the requirement for flex/grid
Without display: flex; or display: grid;, justify-content has no effect.
Final Answer:
The container must have display: flex; or display: grid; for justify-content to work -> Option A
Quick Check:
Justify-content needs flex or grid [OK]
Hint: Use flex or grid display for justify-content to work [OK]
Common Mistakes:
Using justify-content on block containers
Confusing justify-content with justify-items
Thinking value 'center' is invalid
5. You want to create a navigation bar with 4 links spaced evenly across the width, but with equal space around each link (including edges). Which justify-content value should you use in your flex container?
hard
A. space-around
B. flex-start
C. center
D. space-between
Solution
Step 1: Understand the difference between space-between and space-around
space-between puts equal space only between items, no space at edges. space-around adds equal space around each item, including edges.
Step 2: Match requirement to property value
Since the requirement is equal space around each link including edges, space-around is the correct choice.
Final Answer:
space-around -> Option A
Quick Check:
Space-around = equal space around all items [OK]
Hint: Use space-around for equal space including edges [OK]