YouTube Courses - Learn Smarter

YouTube-Courses.site transforms YouTube videos and playlists into structured courses, making learning more efficient and organized.

CSS Flexbox: A Comprehensive Guide for Beginners



Introduction to Flexbox

Welcome to this comprehensive guide on CSS Flexbox, designed for complete beginners. This chapter will delve into the fundamental concepts of Flexbox, a powerful layout tool in CSS. Whether you are entirely new to Flexbox or have some basic familiarity and wish to deepen your understanding, this guide is for you. If you are already an expert in Flexbox, this material might be too introductory.

This chapter is a continuation of a broader CSS learning journey. It is assumed that you have a foundational understanding of both HTML and CSS. While we will focus exclusively on Flexbox in this chapter, it’s worth noting that CSS Grid is another powerful layout tool that we will explore in a separate resource. A common question is whether to learn Flexbox or Grid. The recommendation is to learn both. Flexbox excels at one-dimensional layouts and can be effectively used alongside Grid for more complex, two-dimensional designs. Many modern web projects utilize both Flexbox and Grid in their stylesheets.

To maximize your learning, it is highly recommended that you code along with the examples and explanations provided. Hands-on practice is the most effective way to solidify your understanding of Flexbox. By the end of this chapter, you will possess a strong foundation in creating web layouts using CSS Flexbox.

What is Flexbox?

The CSS Flexible Box Module, commonly known as Flexbox, is a one-dimensional layout model in CSS.

One-dimensional layout model: A layout model that deals with either rows or columns, but not both simultaneously in the same context. Flexbox is primarily concerned with arranging items in a single direction (either horizontally or vertically).

Flexbox provides an efficient way to:

  • Design flexible layouts: Create layouts that adapt to different screen sizes and content dimensions.
  • Align items: Control the alignment of items within a container, both horizontally and vertically.
  • Distribute space: Manage how space is distributed among items within a container.

In simpler terms, Flexbox simplifies the process of arranging elements on a web page, making it easier to create responsive and well-structured designs.

Why Use Flexbox? The Evolution of CSS Layouts

Before the advent of Flexbox, CSS offered several layout modes, each with its limitations:

  • Block Layout: Used primarily for structuring sections of a web page.
  • Inline Layout: Designed for text and inline elements.
  • Table Layout: Intended for displaying tabular data in two dimensions.
  • Positioned Layout: Allowed for explicit positioning of elements on a page.

While these layout modes served their purpose, they often lacked the flexibility needed for modern web design. Developers frequently resorted to complex workarounds and “hacks” to achieve desired layouts. Common challenges included:

  • Vertical alignment of content: Centering content vertically was notoriously difficult.
  • Creating equal height columns: Achieving columns of equal height often required intricate CSS techniques.
  • Managing floats: Working with floats for layout could be cumbersome and lead to layout issues.

Flexbox was introduced to address these limitations and provide a more intuitive and powerful layout system. Flexbox offers significant advantages:

  • Directional Flexibility: Arrange items in rows (left-to-right, right-to-left) or columns (top-to-bottom, bottom-to-top).
  • Control over Spacing: Precisely manage the space between, around, and within items.
  • Alignment Capabilities: Easily align items along both the horizontal and vertical axes.
  • Order Manipulation: Change the visual order of items without altering the HTML source order.

In modern front-end development, understanding and utilizing Flexbox has become an essential skill. For anyone comfortable with HTML and CSS, learning Flexbox is the natural next step to mastering web layout techniques.

Flexbox Terminology: Building Blocks for Understanding

Before diving into Flexbox properties, it’s crucial to understand the core terminology. Grasping these terms will significantly simplify the learning process. The fundamental entities in Flexbox are:

  • Flex Container: The parent element that hosts the flex items. To enable Flexbox layout, you must declare an element as a flex container.
  • Flex Items: The direct children of the flex container. These are the elements that Flexbox will arrange and manipulate.

Consider the following HTML structure:

<div class="container"> <!-- Flex Container -->
  <div>Item 1</div>     <!-- Flex Item -->
  <div>Item 2</div>     <!-- Flex Item -->
  <div>Item 3</div>     <!-- Flex Item -->
  </div>

In this example, the div with the class “container” is the flex container, and the three child div elements are flex items.

Flexbox operates along two axes:

  • Main Axis: The primary axis along which flex items are arranged. By default, the main axis runs horizontally from left to right.

    • Main Start: The beginning point of the main axis.
    • Main End: The ending point of the main axis.
    • Main Size: The length of the main axis, from main start to main end. Flex items flow from main start to main end and occupy the main size.
  • Cross Axis: The axis perpendicular to the main axis. By default, the cross axis runs vertically from top to bottom.

    • Cross Start: The beginning point of the cross axis.
    • Cross End: The ending point of the cross axis.
    • Cross Size: The length of the cross axis, from cross start to cross end.

The direction of the main and cross axes can be modified using Flexbox properties, allowing for flexible and versatile layouts. Understanding these terms is key to comprehending how Flexbox properties control the layout of elements. Learning Flexbox essentially involves becoming familiar with the properties associated with the flex container and its flex items.

Flex Container Properties: Controlling the Flexbox Environment

Flex container properties are applied to the parent element and control the behavior of all its flex items. There are ten primary flex container properties:

  1. display: This property is fundamental and mandatory to activate Flexbox layout on an element, turning it into a flex container.
  2. flex-direction: Defines the direction in which flex items are placed within the container, establishing the main axis.
  3. flex-wrap: Controls whether flex items should wrap onto multiple lines or stay in a single line when the container is too narrow.
  4. flex-flow: A shorthand property combining flex-direction and flex-wrap for concise syntax.
  5. column-gap: Sets the spacing between columns of flex items.
  6. row-gap: Sets the spacing between rows of flex items.
  7. gap: A shorthand property for setting both row-gap and column-gap simultaneously.
  8. justify-content: Defines the alignment of flex items along the main axis.
  9. align-items: Defines the default alignment of flex items along the cross axis.
  10. align-content: Aligns rows of flex items along the cross axis, particularly when there are multiple lines of items due to wrapping.

Let’s explore each of these properties in detail.

1. display: Activating Flexbox

The display property is the cornerstone of Flexbox. To make an element a flex container and enable Flexbox layout for its children, you must set the display property to one of two values:

  • flex: Creates a block-level flex container. This means the container itself behaves like a block element, taking up the full width available and starting on a new line.
  • inline-flex: Creates an inline-level flex container. The container behaves like an inline element, only taking up as much width as necessary to contain its children.

Default Flex Item Behavior:

When you set display: flex or display: inline-flex on a container, the immediate children (flex items) exhibit the following default behaviors:

  • Row Direction: Items are displayed in a row (horizontally).
  • Start Alignment: Items start from the beginning of the main axis (leftmost point in the default row direction).
  • No Stretching in Main Axis: Items do not stretch to fill the entire main axis width but can shrink if necessary to fit within the container.
  • Stretching in Cross Axis: Items do stretch to fill the height of the cross axis (from top to bottom in the default column direction), unless their content dictates a larger height.
  • Block-level Container (flex): The container itself behaves like a block-level element, taking up 100% width.
  • Inline-level Container (inline-flex): The container only takes up the width necessary to accommodate its children.

Example:

.container {
  display: flex; /* or display: inline-flex; */
  border: 5px solid black; /* For visualization */
}

.flex-item {
  color: white;
  font-size: 1.5rem;
  padding: 1rem;
  text-align: center;
}

.item1 { background-color: #f44336; }
.item2 { background-color: #9c27b0; }
.item3 { background-color: #3f51b5; }
/* ... and so on for item4 to item9 with different background colors */

Setting display: flex on .container will arrange the child items in a row within the container. Setting display: inline-flex will cause the container to shrink-wrap around the items.

2. flex-direction: Defining the Main Axis

The flex-direction property establishes the direction of the main axis, which in turn determines the direction in which flex items are laid out within the flex container. It accepts four possible values:

  • row (default): The main axis is horizontal, from left to right. Flex items are placed in a row.
  • row-reverse: The main axis is horizontal, from right to left. Flex items are placed in a row, but in reverse order.
  • column: The main axis is vertical, from top to bottom. Flex items are stacked in a column.
  • column-reverse: The main axis is vertical, from bottom to top. Flex items are stacked in a column, but in reverse order.

Examples:

  • flex-direction: row; (Default) Items are arranged horizontally from left to right.
  • flex-direction: row-reverse; Items are arranged horizontally from right to left.
  • flex-direction: column; Items are arranged vertically from top to bottom.
  • flex-direction: column-reverse; Items are arranged vertically from bottom to top.

Code Example:

.container {
  display: flex;
  /* border and other styles */
}

.container {
  flex-direction: row; /* Default - no change from default behavior */
}

.container {
  flex-direction: row-reverse; /* Items will be in reverse horizontal order */
}

.container {
  flex-direction: column; /* Items will be stacked vertically */
}

.container {
  flex-direction: column-reverse; /* Items will be in reverse vertical order */
}

3. flex-wrap: Handling Overflowing Items

By default, flex items try to fit onto a single line. If there isn’t enough space, they will shrink to a certain extent to fit within the container. However, if they cannot shrink enough and still overflow, the flex-wrap property allows you to control this behavior. It accepts three values:

  • nowrap (default): Flex items will remain in a single line. If they overflow, they will simply extend beyond the container’s boundaries or potentially cause overflow.
  • wrap: Flex items will wrap onto multiple lines if necessary. When items exceed the container’s width (in row direction) or height (in column direction), they will wrap to a new line.
  • wrap-reverse: Similar to wrap, but the lines are stacked in reverse order. Instead of wrapping to a new line below, items wrap to a new line above.

Examples:

  • flex-wrap: nowrap; (Default) Items stay in a single line, may overflow.
  • flex-wrap: wrap; Items wrap to new lines when needed.
  • flex-wrap: wrap-reverse; Items wrap to new lines in reverse order.

Code Example:

.container {
  display: flex;
  /* flex-direction: row; (default) */
  flex-wrap: nowrap; /* Default - no wrapping */
}

.container {
  flex-wrap: wrap; /* Items will wrap to a new row when needed */
}

.container {
  flex-wrap: wrap-reverse; /* Items will wrap to a new row above */
}

/* Vertical wrapping requires setting flex-direction: column and container height */
.container {
  flex-direction: column;
  height: 400px;
  flex-wrap: wrap; /* Items will wrap to a new column to the right */
}

.container {
  flex-direction: column;
  height: 400px;
  flex-wrap: wrap-reverse; /* Items will wrap to a new column to the left */
}

4. flex-flow: The Shorthand for Direction and Wrapping

flex-flow is a shorthand property that combines flex-direction and flex-wrap into a single declaration. It accepts two values, separated by a space: the first value is for flex-direction, and the second is for flex-wrap.

Default Value: flex-flow: row nowrap;

Examples:

  • flex-flow: row nowrap; (Default) Items in a row, no wrapping.
  • flex-flow: column wrap; Items in a column, wrapping enabled.
  • flex-flow: row-reverse wrap-reverse; Items in reversed row order, reverse wrapping.

Code Example:

.container {
  display: flex;
  flex-flow: row nowrap; /* Default flex-direction and flex-wrap */
}

.container {
  flex-flow: row-reverse wrap; /* Row direction reversed, wrapping enabled */
}

5, 6 & 7. column-gap, row-gap, and gap: Spacing Between Items

These properties control the spacing (gutters) between flex items.

  • column-gap: Sets the gap size between columns of flex items. This is effective when items are arranged in rows or when wrapping creates multiple columns.
  • row-gap: Sets the gap size between rows of flex items. This is effective when wrapping creates multiple rows or when items are arranged in columns.
  • gap: A shorthand property for setting both row-gap and column-gap in a single declaration. If one value is provided, it applies to both row and column gaps. If two values are provided, the first value sets row-gap, and the second value sets column-gap.

These properties accept non-negative length values (pixels, rem, etc.) or percentages.

Examples:

  • column-gap: 20px; Sets a 20-pixel gap between columns.
  • row-gap: 30px; Sets a 30-pixel gap between rows.
  • gap: 40px; Sets a 40-pixel gap for both rows and columns.
  • gap: 40px 20px; Sets a 40-pixel row gap and a 20-pixel column gap.

Code Example:

.container {
  display: flex;
  flex-wrap: wrap; /* Enable wrapping to see row gaps */
  column-gap: 20px; /* Gap between columns */
}

.container {
  display: flex;
  flex-direction: column; /* Arrange items in a column to see row gaps */
  row-gap: 30px; /* Gap between rows */
}

.container {
  display: flex;
  flex-wrap: wrap; /* Enable wrapping */
  gap: 40px 20px; /* 40px row gap, 20px column gap */
}

8. justify-content: Aligning Items Along the Main Axis

The justify-content property defines how flex items are aligned along the main axis of the flex container. It also determines how extra space is distributed when all flex items on a line are less than the size of the container along the main axis. It accepts the following values:

  • flex-start (default): Items are packed towards the start of the main axis.
  • flex-end: Items are packed towards the end of the main axis.
  • center: Items are centered along the main axis.
  • space-between: Items are evenly distributed along the main axis. The first item is at the start, the last item is at the end, and remaining space is distributed between the items.
  • space-around: Items are evenly distributed along the main axis with equal space around each item, including before the first and after the last item. The space between items is twice the space at the start and end edges.
  • space-evenly: Items are evenly distributed along the main axis with equal space around each item, including the space before the first and after the last item. All spaces are equal.

Examples:

  • justify-content: flex-start; (Default) Items aligned to the start of the main axis.
  • justify-content: flex-end; Items aligned to the end of the main axis.
  • justify-content: center; Items centered on the main axis.
  • justify-content: space-between; Space distributed between items.
  • justify-content: space-around; Space distributed around items.
  • justify-content: space-evenly; Space evenly distributed.

Code Example:

.container {
  display: flex;
  justify-content: flex-start; /* Default - items at the start */
}

.container {
  justify-content: flex-end; /* Items at the end */
}

.container {
  justify-content: center; /* Items centered */
}

.container {
  justify-content: space-between; /* Space between items */
}

.container {
  justify-content: space-around; /* Space around items */
}

.container {
  justify-content: space-evenly; /* Space evenly distributed */
}

/* Vertical alignment with justify-content when flex-direction: column */
.container {
  display: flex;
  flex-direction: column; /* Main axis is now vertical */
  height: 800px; /* Need height to see vertical alignment */
  justify-content: flex-start; /* Items at the top */
}

.container {
  justify-content: flex-end; /* Items at the bottom */
}

.container {
  justify-content: center; /* Items vertically centered */
}
/* space-between, space-around, space-evenly work similarly in column direction */

9. align-items: Aligning Items Along the Cross Axis

The align-items property defines the default alignment for flex items along the cross axis of the flex container. It is similar to justify-content, but it operates in the perpendicular direction. It sets the default for all flex items within the container. To visualize the effect, a height needs to be set on the container. It accepts the following values:

  • stretch (default): Flex items are stretched to fill the entire height of the container along the cross axis.
  • 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.
  • baseline: Items are aligned so that their baselines align. This is particularly useful when dealing with text of different sizes within flex items.

Baseline: In typography, the baseline is the line upon which most letters “sit” and below which descenders (like the tail of ‘y’ or ‘g’) extend. In Flexbox with align-items: baseline, items are aligned based on the baseline of their text content.

Examples:

  • align-items: stretch; (Default) Items stretch to fill cross-axis height.
  • align-items: flex-start; Items aligned to the start of the cross axis.
  • align-items: flex-end; Items aligned to the end of the cross axis.
  • align-items: center; Items centered on the cross axis.
  • align-items: baseline; Items aligned by their text baselines.

Code Example:

.container {
  display: flex;
  height: 800px; /* Need height to visualize cross-axis alignment */
  align-items: stretch; /* Default - items stretch to fill height */
}

.container {
  align-items: flex-start; /* Items aligned to the top */
}

.container {
  align-items: flex-end; /* Items aligned to the bottom */
}

.container {
  align-items: center; /* Items vertically centered */
}

.container {
  align-items: baseline; /* Items aligned by text baseline */
}

To demonstrate baseline, you might need to adjust the padding or font sizes of individual flex items to see the alignment clearly.

10. align-content: Aligning Lines of Content Along the Cross Axis

The align-content property aligns lines of flex items along the cross axis. It is similar to justify-content but operates on the cross axis and applies to rows of items (when flex-direction is row and flex-wrap is wrap) or columns of items (when flex-direction is column and flex-wrap is wrap). This property only has an effect when there are multiple lines of flex items in the container due to wrapping, and there is extra space in the cross axis. It accepts the following values, which are similar to justify-content:

  • stretch (default): Lines of flex items are stretched to fill the available space in the cross axis.
  • flex-start: Lines are packed to the start of the cross axis.
  • flex-end: Lines are packed to the end of the cross axis.
  • center: Lines are centered in the cross axis.
  • space-between: Lines are evenly distributed in the cross axis. The first line is at the start, the last line is at the end, and space is distributed between the lines.
  • space-around: Lines are evenly distributed with equal space around each line. The space between lines is twice the space at the start and end edges.
  • space-evenly: Lines are evenly distributed with equal space around each line, including the space before the first and after the last line. All spaces are equal.

Examples:

  • align-content: stretch; (Default) Lines stretch to fill cross-axis height.
  • align-content: flex-start; Lines aligned to the start of the cross axis.
  • align-content: flex-end; Lines aligned to the end of the cross axis.
  • align-content: center; Lines centered on the cross axis.
  • align-content: space-between; Space distributed between lines.
  • align-content: space-around; Space distributed around lines.
  • align-content: space-evenly; Space evenly distributed among lines.

Code Example (requires wrapping and container height):

.container {
  display: flex;
  flex-wrap: wrap; /* Enable wrapping to create multiple lines */
  height: 800px; /* Need height to visualize cross-axis alignment of lines */
  align-content: stretch; /* Default - lines stretch to fill height */
}

.container {
  align-content: flex-start; /* Lines at the top */
}

.container {
  align-content: flex-end; /* Lines at the bottom */
}

.container {
  align-content: center; /* Lines vertically centered */
}

.container {
  align-content: space-between; /* Space between lines */
}

.container {
  align-content: space-around; /* Space around lines */
}

.container {
  align-content: space-evenly; /* Space evenly distributed among lines */
}

Flex Item Properties: Fine-Tuning Individual Items

Flex item properties are applied to the children of a flex container and allow for individual control over each item’s behavior within the flex layout. There are five main flex item properties:

  1. order: Controls the order in which flex items appear within the container, overriding their source order in the HTML.
  2. flex-grow: Defines the ability of a flex item to grow and take up available space within the container along the main axis.
  3. flex-shrink: Defines the ability of a flex item to shrink if necessary to fit within the container along the main axis.
  4. flex-basis: Specifies the initial size of a flex item before any available space is distributed by flex-grow or flex-shrink.
  5. flex: A shorthand property for setting flex-grow, flex-shrink, and flex-basis in a single declaration.
  6. align-self: Allows you to override the align-items property for individual flex items, controlling their alignment along the cross axis.

Let’s examine each of these properties.

1. order: Changing Item Order

The order property allows you to reorder flex items visually without changing their order in the HTML source code. By default, all flex items have an order value of 0. Items are then laid out in ascending order of their order values. If multiple items have the same order value, they are laid out in their source code order.

The order property accepts integer values (positive, negative, or zero).

Examples:

  • order: 1; This item will be placed after items with order: 0 (default).
  • order: -1; This item will be placed before items with order: 0.
  • Items with higher order values appear later in the layout.

Code Example:

.item3 {
  order: 1; /* Item 3 will be moved to the end (after default order items) */
}

.item7 {
  order: 2; /* Item 7 will be moved even further to the end */
}

.item9 {
  order: 1; /* Item 9 will also have order 1, will appear after item 3 in source order */
}

2. flex-grow: Expanding Items to Fill Space

The flex-grow property specifies how much a flex item should grow relative to other flex items in the container when there is extra space along the main axis. It determines the flex grow factor.

  • Default Value: flex-grow: 0; By default, flex items do not grow to take up extra space.
  • Non-negative numeric value: A value of 1 or greater indicates the item should grow. The growth is proportional to the value set and relative to other items’ flex-grow values.

If all items have flex-grow: 1, they will all grow equally to fill the available space. If one item has flex-grow: 2 and others have flex-grow: 1, the item with flex-grow: 2 will take up twice as much of the available space as the others.

Examples:

  • flex-grow: 0; (Default) Item does not grow.
  • flex-grow: 1; Item grows to take up available space, sharing equally with other flex-grow: 1 items.
  • flex-grow: 2; Item grows twice as much as items with flex-grow: 1.

Code Example:

.flex-item {
  flex-grow: 0; /* Default - items do not grow */
}

.item5 {
  flex-grow: 1; /* Item 5 will grow to take up available space */
}

.item6 {
  flex-grow: 3; /* Item 6 will grow 3 times as much as item 5 if item 5 has flex-grow: 1 */
}

/* To make all items grow evenly: */
.flex-item {
  flex-grow: 1; /* All items will grow equally */
}

3. flex-shrink: Compressing Items When Space is Limited

The flex-shrink property specifies how much a flex item should shrink relative to other flex items in the container when there is not enough space along the main axis. It determines the flex shrink factor.

  • Default Value: flex-shrink: 1; By default, flex items are allowed to shrink if necessary to prevent overflow.
  • Non-negative numeric value: A value of 0 prevents the item from shrinking. A value greater than 1 indicates the item should shrink more aggressively compared to items with a value of 1.

If all items have flex-shrink: 1, they will all shrink equally when needed. If one item has flex-shrink: 0, it will not shrink, while other items with flex-shrink: 1 will shrink to accommodate it. If one item has flex-shrink: 4 and others have flex-shrink: 1, the item with flex-shrink: 4 will shrink four times as much as the others.

Examples:

  • flex-shrink: 1; (Default) Item can shrink.
  • flex-shrink: 0; Item will not shrink.
  • flex-shrink: 4; Item shrinks more aggressively than items with flex-shrink: 1.

Code Example:

.flex-item {
  flex-shrink: 1; /* Default - items can shrink */
}

.flex-item {
  flex-shrink: 0; /* Items will not shrink */
}

.item3 {
  flex-shrink: 4; /* Item 3 will shrink 4 times as much as other items */
}

4. flex-basis: Setting the Initial Size

The flex-basis property specifies the initial main size of a flex item before free space is distributed according to flex-grow and flex-shrink. It can be thought of as setting the “base” width (for row direction) or height (for column direction) of the item.

  • Values: Accepts length values (pixels, rem, percentages), or the keyword auto.
  • auto (default): The initial size is determined by the item’s content.

When flex-basis is set to a value other than auto, it overrides the width (in row direction) or height (in column direction) property for that flex item.

Examples:

  • flex-basis: auto; (Default) Initial size based on content.
  • flex-basis: 200px; Sets initial width to 200 pixels (in row direction).
  • flex-basis: 50%; Sets initial width to 50% of the container’s width.

Code Example:

.flex-item {
  flex-basis: auto; /* Default - initial size based on content */
}

.item1 {
  flex-basis: 200px; /* Initial width of item 1 will be 200px */
}

flex-grow and flex-shrink properties then operate on top of this initial flex-basis size.

5. flex: The Shorthand for Grow, Shrink, and Basis

The flex property is a shorthand for setting flex-grow, flex-shrink, and flex-basis in a single property. It is the recommended way to define the flexibility of flex items as it is more concise.

Syntax: flex: <flex-grow> <flex-shrink>? <flex-basis>?;

  • <flex-grow>: Required. Specifies the flex-grow value.
  • <flex-shrink>: Optional. Specifies the flex-shrink value. If omitted, the default is 1.
  • <flex-basis>: Optional. Specifies the flex-basis value. If omitted, the default is auto.

Common Values:

  • flex: initial; Sets flex-grow: 0, flex-shrink: 1, flex-basis: auto; (Resets to initial values).
  • flex: auto; Sets flex-grow: 1, flex-shrink: 1, flex-basis: auto; (Item grows and shrinks, initial size is auto).
  • flex: none; Sets flex-grow: 0, flex-shrink: 0, flex-basis: auto; (Item does not grow or shrink, initial size is auto).
  • flex: <number>; (e.g., flex: 1;) Sets flex-grow: <number>, flex-shrink: 1, flex-basis: 0%; (Commonly used for equal distribution of space).
  • flex: <grow> <basis>; (e.g., flex: 1 200px;) Sets flex-grow, flex-shrink: 1 (default), and flex-basis.
  • flex: <grow> <shrink> <basis>; (e.g., flex: 1 2 300px;) Sets all three values explicitly.

Examples:

  • flex: 0 1 auto; (Equivalent to flex: initial;)
  • flex: 1 1 auto; (Equivalent to flex: auto;)
  • flex: 0 0 auto; (Equivalent to flex: none;)
  • flex: 1; (Commonly used for equal space distribution - flex-grow: 1, flex-shrink: 1, flex-basis: 0%)
  • flex: 2 200px; (flex-grow: 2, flex-shrink: 1, flex-basis: 200px)

Code Example:

.flex-item {
  flex: 0 1 auto; /* Equivalent to flex: initial; */
}

.flex-item {
  flex: 1; /* Common shorthand for equal distribution */
}

.item2 {
  flex: 2 200px; /* Custom grow, shrink, and basis for item 2 */
}

6. align-self: Overriding Item Alignment

The align-self property allows you to override the align-items property set on the flex container for individual flex items. This allows for fine-grained control over the cross-axis alignment of specific items.

  • Values: Accepts the same values as align-items: auto, flex-start, flex-end, center, baseline, and stretch.
  • auto (default): The item inherits the align-items value from its parent flex container.

When align-self is set to a value other than auto, it overrides the parent’s align-items setting for that specific flex item.

Examples:

  • align-self: auto; (Default) Inherits from parent’s align-items.
  • align-self: flex-start; Aligns item to the start of the cross axis, regardless of parent’s align-items.
  • align-self: flex-end; Aligns item to the end of the cross axis.
  • align-self: center; Centers item on the cross axis.
  • align-self: stretch; Stretches item to fill the cross-axis height, overriding parent’s align-items.

Code Example:

.container {
  display: flex;
  height: 400px; /* Need height to visualize cross-axis alignment */
  align-items: stretch; /* Default align-items for container */
}

.item1 {
  align-self: flex-start; /* Override container's align-items for item 1 */
}

.item2 {
  align-self: flex-end; /* Override for item 2 */
}

.item3 {
  align-self: center; /* Override for item 3 */
}

.item4 {
  align-self: stretch; /* Override for item 4 */
}

/* Items 5-9 will inherit align-items: stretch from the container */

Conclusion

This chapter has provided a comprehensive introduction to CSS Flexbox, covering its core concepts, terminology, and properties for both flex containers and flex items. By understanding and practicing with these properties, you can effectively create flexible, responsive, and well-structured web layouts. Flexbox simplifies many layout challenges previously encountered in CSS, making it an indispensable tool for modern web development.

Next Steps: Exploring CSS Grid

While Flexbox is excellent for one-dimensional layouts, CSS Grid is another powerful layout tool that excels in two-dimensional layouts. To further expand your layout capabilities, the next step is to explore CSS Grid. Understanding both Flexbox and Grid will equip you with a robust toolkit for tackling a wide range of web layout challenges.