Hello! Welcome to your first lesson in the "Responsive Design and Modern CSS" module.
In the previous module, we covered the fundamentals of CSS, culminating in how browsers resolve style conflicts using specificity and the cascade. You now have the tools to style individual elements. Now, we'll shift our focus from styling elements to arranging them on the page.
Today's learning outcome is to create flexible layouts using CSS Flexbox, including flex-direction, justify-content, and align-items. Flexbox is a powerful, one-dimensional layout model that makes it incredibly easy to align and distribute items within a container. As a UI/UX designer, you'll find this immediately familiar, as it's the same underlying principle behind Figma's "Auto Layout" feature. Mastering Flexbox is a fundamental step toward translating your static designs into dynamic, responsive web pages.
1. The Core Idea: Flex Containers and Flex Items
Flexbox works by defining a flex container and the flex items inside it.
- Flex Container: The parent element. You turn any element into a flex container by setting its
displayproperty toflex. - Flex Items: The direct children of the flex container. They are automatically arranged according to the flexbox rules.
Your experience with Figma's Auto Layout will be a great asset here. When you apply Auto Layout to a frame in Figma, that frame becomes the "flex container," and the objects inside it become the "flex items."
To get a quick visual overview and see this connection in action, let's watch a short introductory video.
CSS for UI Designers: Understanding Flexbox and Applying it to Figma's Auto Layout
This video from the moonlearning channel provides a fantastic introduction to Flexbox and directly compares its properties to the settings you use in Figma's Auto Layout.
Watch from the beginning until 02:47. Pay close attention to: The concept of a flex container and flex items. How display: flex transforms the layout. The introduction to container properties like flex-direction, justify-content, and align-items.
2. The Two Axes of Flexbox
As the video mentioned, once you create a flex container, its items are laid out along an axis. The most crucial concept to grasp in Flexbox is that it operates on two axes:
- Main Axis: The primary direction in which flex items are laid out. By default, this is horizontal (a row).
- Cross Axis: The axis perpendicular to the main axis. By default, this is vertical.
Understanding these axes is key because all alignment properties in Flexbox refer to one or the other.
To solidify this foundational concept, please read the following sections from the MDN Web Docs, the official resource for web developers.
Basic concepts of flexbox - CSS - MDN Web Docs
This article, 'Basic concepts of flexbox' from MDN, provides a clear, technical definition of the Flexbox model and its axes.
Read the sections 'The two axes of flexbox' (including 'The main axis' and 'The cross axis') and 'The flex container'. This will reinforce the terminology and the core model of Flexbox.
3. Controlling Direction and Alignment
Now that we understand the container/item model and the two axes, let's explore the main properties you'll use to control your layouts. These properties are all set on the flex container.
flex-direction: Setting the Main Axis
This property defines the main axis, determining whether your items are arranged in a row or a column.
row(default): Items are laid out horizontally, left-to-right.column: Items are laid out vertically, top-to-bottom.row-reverse: Items are laid out horizontally, right-to-left.column-reverse: Items are laid out vertically, bottom-to-top.
When you change flex-direction to column, the main axis becomes vertical, and the cross axis becomes horizontal. The alignment properties we're about to discuss will adjust accordingly.
justify-content: Alignment on the Main Axis
This property defines how flex items are distributed along the main axis. It controls what happens with any extra space.
flex-start(default): Items are packed at the start of the main axis.flex-end: Items are packed at the end of the main axis.center: Items are centered along the main axis.space-between: Items are evenly distributed; the first item is at the start, the last item is at the end.space-around: Items are evenly distributed with equal space around them.space-evenly: Items are evenly distributed with equal space between them.
In Figma, this corresponds to the "Distribution" setting in the Auto Layout panel (e.g., "Pack" vs. "Space between").
align-items: Alignment on the Cross Axis
This property defines how flex items are aligned along the cross axis.
stretch(default): Items stretch to fill the container's height (or width, if in a column).flex-start: Items are aligned to the start of the cross axis.flex-end: Items are aligned to the end of the cross axis.center: Items are centered along the cross axis.
This corresponds to the alignment settings in Figma's Auto Layout (e.g., align top, center, or bottom).
Let's watch a detailed walkthrough that demonstrates these properties in code.
The 'Flexbox Crash Course' by Traversy Media is a classic for a reason. We'll watch a few key segments that clearly explain and demonstrate these core alignment properties.
Please watch the following segments: Main and Cross Axes (03:34 - 05:01): A great reinforcement of this core concept. justify-content (09:48 - 11:05): See how to align items on the main axis. align-items (11:05 - 12:05): See how to align items on the cross axis. flex-direction (12:57 - 15:04): Watch how changing the direction swaps the axes and affects alignment. Centering an Element (15:04 - 16:27): A very common and powerful use case for Flexbox.
4. A Deeper Look: content vs. items
You might have noticed the property names: justify-content and align-items. Why not justify-items or align-content? This naming is intentional and reveals a fundamental aspect of Flexbox.
- Along the main axis, items can't overlap. They are treated as a single group of "content" that can be distributed. You can't tell one item to go to the start and another to go to the end along this axis without affecting its siblings. Hence,
justify-content. - Along the cross axis, each item is independent. You can align one item to the top, another to the center, and a third to the bottom without them interfering with each other. They are treated as individual "items". Hence,
align-items.
This interactive guide provides a brilliant explanation and visualization of this concept.
An Interactive Guide to Flexbox in CSS
This 'Interactive Guide to Flexbox' by Josh W. Comeau is one of the best learning resources for CSS on the web. We'll focus on the section that clarifies the logic behind the property names.
Read the section titled 'Alignment' and pay special attention to the sub-section 'Content vs. items'. The metaphor of a kebab skewer versus cocktail wieners is a perfect way to build an intuition for this.
5. Practical Exercise: Building a Component Header
Let's apply what you've learned. Imagine you've designed a simple profile card. The header of the card contains a title on the left and a "more options" icon on the right, both vertically centered.
Here is the HTML:
<div class="card-header">
<h3 class="card-title">My Profile</h3>
<span class="options-icon">...</span> <!-- Imagine an icon here -->
</div>
Your task is to write the CSS for the .card-header class to achieve this layout using Flexbox.
Instructions:
- Make
.card-headera flex container. - Use a property to push the title and icon to opposite ends of the container.
- Use another property to ensure they are vertically centered.
Click here to see the solution
.card-header {
display: flex;
justify-content: space-between; /* Pushes items to opposite ends of the main (horizontal) axis */
align-items: center; /* Centers items on the cross (vertical) axis */
padding: 16px; /* Example padding */
border-bottom: 1px solid #eee; /* Example border */
}
This simple, three-line solution is a perfect example of the power and elegance of Flexbox for building common UI components.
Conclusion
Excellent work! You've just learned one of the most essential tools in modern front-end development. By understanding how to control layout in one dimension, you can build a huge variety of components, from navigation bars and card headers to rows of buttons.
Key Takeaways:
- Flexbox is a one-dimensional layout model consisting of a flex container (
display: flex) and its flex items. - Layout is based on a main axis and a cross axis.
flex-directionsets the direction of the main axis (roworcolumn).justify-contentaligns items along the main axis.align-itemsaligns items along the cross axis.- This model is conceptually identical to Auto Layout in design tools like Figma, making it an intuitive transition from design to code.
In our next lesson, we'll explore Flexbox's two-dimensional counterpart: CSS Grid. While Flexbox is perfect for arranging items in a line, Grid is designed for creating complex, two-dimensional layouts, like the overall structure of a web page.
Can't find a good explanation? Sign up and we'll make it for you
Sign up