Align Your Elements Perfectly: Flexbox for Box Alignment


  • Cross axis
    This is perpendicular to the main axis. So, if the main axis is horizontal, the cross axis is vertical, and vice versa.

  • Main axis
    This is the primary direction in which flex items are laid out. By default, it's horizontal when the flex-direction is set to row (the most common setting) and vertical when flex-direction is set to column.

  1. Justify-content
    This property controls the alignment of flex items along the main axis of the container. Common values include:

    • flex-start: Aligns items to the beginning of the container.
    • center: Centers items along the main axis.
    • flex-end: Aligns items to the end of the container.
    • space-between: Creates equal space between items.
    • space-around: Creates equal space around and between items.
  2. Align-items
    This property controls the alignment of flex items along the cross axis of the container. Common values include:

    • flex-start: Aligns items to the top of the container (for flex-direction: row) or left (for flex-direction: column).
    • center: Centers items along the cross axis.
    • flex-end: Aligns items to the bottom (for flex-direction: row) or right (for flex-direction: column).
    • stretch (default for some elements): Stretches items to fill the container's height.

These properties work together to achieve various alignments. For instance, to center items both horizontally and vertically within a container, you would use:

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

Flexbox also offers:

  • Gap property
    This creates a fixed size gap between flex items.
  • Align-content
    This controls the alignment of multiple lines of flex items within the container (relevant when flex-wrap is set to allow wrapping).
  • Align-self
    This allows overriding the align-items setting for individual flex items.


Horizontal Alignment with justify-content

This code shows three boxes aligned horizontally within a container using different justify-content values:

<div class="container">
  <div class="box">Item 1</div>
  <div class="box">Item 2</div>
  <div class="box">Item 3</div>
</div>
.container {
  display: flex; /* Make the container a flexbox */
  width: 400px; /* Set a width for the container */
}

.box {
  background-color: lightblue; /* Style the boxes */
  padding: 10px;
  margin: 5px;
}

/* Try different justify-content values here */
.container.justify-start {
  justify-content: flex-start;  /* Align items to the left */
}

.container.justify-center {
  justify-content: center;  /* Center items horizontally */
}

.container.justify-end {
  justify-content: flex-end;  /* Align items to the right */
}

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

.container.justify-around {
  justify-content: space-around;  /* Distribute items with space around and between */
}

You can change the class names on the container (container.justify-start, etc.) to see how each justify-content value affects the alignment.

Vertical Alignment with align-items

This code shows three boxes with different vertical alignments within the container using align-items:

<div class="container">
  <div class="box">Item 1</div>
  <div class="box">Item 2 (Taller)</div>
  <div class="box">Item 3</div>
</div>
.container {
  display: flex;
  flex-direction: column; /* Stack boxes vertically */
  height: 200px; /* Set a height for the container */
}

.box {
  background-color: lightgreen;
  padding: 10px;
  margin: 5px;
}

/* Try different align-items values here */
.container.align-start {
  align-items: flex-start; /* Align to top */
}

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

.container.align-end {
  align-items: flex-end; /* Align to bottom */
}

Similar to the previous example, change the class names to see how align-items affects the vertical positioning of the boxes within the container.

Overriding Alignment with align-self

This code demonstrates how to override the align-items setting for a specific box using align-self:

<div class="container">
  <div class="box">Item 1</div>
  <div class="box special">Item 2 (Centered)</div>
  <div class="box">Item 3</div>
</div>
.container {
  display: flex;
  flex-direction: column;
  height: 200px;
}

.box {
  background-color: lightcoral;
  padding: 10px;
  margin: 5px;
}

.container > .box { /* Align all boxes to top by default */
  align-items: flex-start;
}

.special {  /* Center only the box with class "special" */
  align-self: center;
}

In this example, even though align-items is set to flex-start (aligning all boxes to the top), the box with the class "special" overrides this with align-self: center, making it centered vertically.



  1. Float
    This property allows elements to float to the left or right of their container, enabling some basic horizontal alignment. However, it can be tricky to manage vertical alignment and can cause layout issues in responsive design.

  2. Inline-block
    Setting elements to display: inline-block allows them to sit side-by-side like inline elements, but with a width and height. You can then use margins and padding for basic alignment. This approach doesn't offer the same level of control as flexbox for complex layouts.

  3. Tables
    While not ideal for modern layouts, tables can be used for basic alignment by setting cells to specific widths and heights. However, tables are not semantically appropriate for most layout purposes and can be inflexible for responsive design.

  4. CSS Grid
    This is another powerful layout model gaining traction. It offers a grid-based system for positioning elements, but it has a steeper learning curve compared to flexbox. Grid might be a good alternative for complex, two-dimensional layouts where flexbox might not be sufficient.

FeatureFlexboxFloatInline-blockTableCSS Grid
Alignment FlexibilityHighMediumLowLowHigh
ResponsivenessExcellentLimitedLimitedLimitedExcellent
Code SimplicityModerateModerateLowHighModerate
Semantic MeaningfulnessHighLowLowHighHigh