❰ Back Home

CSS Flexbox Fundamentals

Understanding Flexbox

CSS Flexbox, or Flexible Box Layout, is a powerful one-dimensional layout model. It provides a more efficient way to lay out, align, and distribute space among items in a container, even when their size is unknown or dynamic. It's particularly useful for aligning items in a row or a column.

The Flex Container and Flex Items

To use Flexbox, you first need a flex container. You create one by setting the `display` property of an element to `flex` or `inline-flex`.


.flex-container {
  display: flex;
  /* or display: inline-flex; */
}
            

The direct children of a flex container are called flex items. Flexbox properties are then applied to the container to control the layout of its items.

Main Axis and Cross Axis

Flexbox works along two axes:

flex-direction Property

This property defines the direction of the main axis. Common values are `row` (default), `row-reverse`, `column`, and `column-reverse`.

Item 1
Item 2
Item 3
Item 4

justify-content Property

This property aligns flex items along the main axis of the flex container. It distributes extra free space if there is any.

Start
Middle
End
Center
Space B
Space B
Space B

align-items Property

This property aligns flex items along the cross axis of the flex container. It defines the default behavior for all flex items.

Top
Middle
Bottom
Center Vertically
Stretch
Stretch

Flex Item Properties

You can also control individual flex items using properties like `flex-grow`, `flex-shrink`, `flex-basis`, and `align-self`.

flex-grow

Specifies how much a flex item should grow relative to the rest of the flex items. A value of 1 means it will take up available space. If all items have `flex-grow: 1`, they will share the space equally.

Grow 1
Grow 2
Grow 3

align-self

Allows the default alignment (specified by `align-items` on the container) to be overridden for individual flex items.

Normal
Center Me
Normal

Flexbox is a fundamental tool for modern web layout. Mastering these concepts will greatly enhance your ability to create responsive and well-aligned designs.

Explore Spicy Lentil Curry Recipes