CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Flex Direction


This property defines the direction of the main axis, which is the primary axis along which flex items are laid out. It allows you to control whether flex items are arranged in a row (horizontally) or a column (vertically), and in which direction.

Example 1: Default Row Direction

HTML

<div class="flex-container row-direction">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

CSS

.flex-container {
  display: flex;
  border: 2px solid #000;
}

/* Arranges items from left to right in a row. */
.row-direction {
  flex-direction: row; /* This is the default value. */
}

Explanation

flex-direction: row arranges the flex items horizontally from left to right. This is the default behavior of a flex container, so explicitly setting it is often unnecessary unless overriding another style.


Example 2: Row Reverse Direction

HTML

<div class="flex-container row-reverse-direction">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

CSS

.flex-container {
  display: flex;
  border: 2px solid #000;
}

/* Arranges items from right to left in a row. */
.row-reverse-direction {
  flex-direction: row-reverse; /* The items are laid out in reverse order. */
}

Explanation

flex-direction: row-reverse lays out the flex items horizontally but in the opposite direction, from right to left. The first item in the HTML source appears last in the rendered layout.


Example 3: Column Direction

HTML

<div class="flex-container column-direction">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

CSS

.flex-container {
  display: flex;
  border: 2px solid #000;
  height: 250px; /* Giving the container height to visualize the column. */
}

/* Stacks items vertically from top to bottom. */
.column-direction {
  flex-direction: column; /* Changes the main axis to vertical. */
}

Explanation

With flex-direction: column, the main axis is switched from horizontal to vertical. Flex items are now stacked on top of each other, from top to bottom.


Example 4: Column Reverse Direction

HTML

<div class="flex-container column-reverse-direction">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>

CSS

.flex-container {
  display: flex;
  border: 2px solid #000;
  height: 250px;
}

/* Stacks items vertically from bottom to top. */
.column-reverse-direction {
  flex-direction: column-reverse; /* Lays out items from bottom to top. */
}

Explanation

flex-direction: column-reverse also stacks the flex items vertically, but it reverses the order. The layout starts from the bottom of the container and goes up.


Example 5: Responsive Direction Change

HTML

<div class="responsive-container">
  <div class="item">Sidebar</div>
  <div class="item">Main Content</div>
</div>

CSS

.responsive-container {
  display: flex;
  flex-direction: column; /* Start with a column layout for mobile. */
  border: 2px solid #4CAF50;
}

/* On larger screens, switch to a row layout. */
@media (min-width: 768px) {
  .responsive-container {
    flex-direction: row; /* Side-by-side layout for tablets and desktops. */
  }
}

Explanation

This demonstrates a common responsive design pattern. The layout is a single column on small screens and switches to a horizontal row on screens wider than 768px by changing the flex-direction.


Example 6: Reversing Chat Message Order

HTML

<div class="chat-window">
  <div class="message">Hello!</div>
  <div class="message">How are you?</div>
  <div class="message">This is the latest message.</div>
</div>

CSS

.chat-window {
  display: flex;
  flex-direction: column-reverse; /* New messages appear at the bottom. */
  border: 2px solid #337ab7;
  height: 200px;
  overflow-y: auto; /* Allow scrolling if messages overflow. */
}

.message {
  padding: 10px;
  margin: 5px;
  background-color: #f0f8ff;
  border-radius: 5px;
}

Explanation

flex-direction: column-reverse is perfect for chat interfaces. It keeps the container scrolled to the bottom where new messages appear, while older messages are pushed upwards.


Example 7: Alternating Content Flow

HTML

<section class="alternating-layout">
  <div class="content-block">
    <div class="text">Text First</div>
    <div class="image">Image Second</div>
  </div>
  <div class="content-block reverse">
    <div class="text">Text Second</div>
    <div class="image">Image First</div>
  </div>
</section>

CSS

.content-block {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

/* The second content block has its direction reversed. */
.content-block.reverse {
  flex-direction: row-reverse; /* Flips the order of the image and text. */
}

Explanation

By applying a class that sets flex-direction: row-reverse, you can easily create an alternating layout where the order of elements like images and text is swapped in subsequent sections.