"I am Saqib Jahangir. A passionate vlogger, software engineer, trainer and avid traveler with a deep love for exploring the hidden gems of our beautiful planet. With a strong foundation in Application Development, Application Architecture & Database Design and Product Management, I bring over a decade of hands-on experience building secure, scalable, and resilient web applications for a diverse range of industries."

Flexbox (Flexible Box Layout)

 

Flexbox is a powerful CSS layout module designed to help you arrange items in a container efficiently, even when their size is unknown or dynamic. It provides an easy and clean way to align, distribute, and order elements within a container, making responsive design much simpler.

Why Use Flexbox?

Traditional layout methods like floats and positioning can get complicated when you want to create flexible, adaptable layouts. Flexbox simplifies this by:

  • Aligning items horizontally or vertically with ease
  • Distributing space evenly between items or around them
  • Changing the order of elements visually without altering the HTML
  • Handling dynamic or unknown item sizes gracefully

How Flexbox Works

To use Flexbox, you set the container’s display property to flex or inline-flex:

.container {

  display: flex;

}

This turns the container into a flex container, and all its direct children become flex items.

Main Concepts and Properties

  • Main Axis and Cross Axis
    Flexbox layouts operate along two axes: the main axis (default is horizontal, left to right) and the cross axis (perpendicular to the main axis, vertical by default).
  • flex-direction
    Defines the direction of the main axis. Common values:
    • row (default): left to right
    • row-reverse: right to left
    • column: top to bottom
    • column-reverse: bottom to top
  • justify-content
    Aligns items along the main axis. Options include:
    • flex-start (default)
    • center
    • flex-end
    • space-between
    • space-around
    • space-evenly
  • align-items
    Aligns items along the cross axis (vertical by default). Values:
    • stretch (default)
    • flex-start
    • center
    • flex-end
    • baseline
  • flex-wrap
    Controls whether items wrap onto multiple lines or stay on one line. Values:
    • nowrap (default)
    • wrap
    • wrap-reverse
  • align-content
    When there are multiple lines (due to wrapping), this aligns the entire group of lines along the cross axis.
  • flex-grow, flex-shrink, flex-basis
    Control how individual flex items grow, shrink, or take up space inside the container.

Simple Example

.container {

  display: flex;

  justify-content: space-between;

  align-items: center;

}

This will place items in a row, evenly spaced, and vertically centered.

Popular Posts

Operators (Arithmetic, Comparison, Logical)

Functions (Built-in & User-defined)