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 Wrap


The flex-wrap property controls whether flex items are forced onto a single line or can wrap onto multiple lines if there isn't enough space. This is crucial for creating responsive layouts that adapt to different screen sizes.

Example 1: No Wrapping (Default)

HTML

<div class="flex-container no-wrap">
  <div class="item">One</div> <div class="item">Two</div> <div class="item">Three</div>
  <div class="item">Four</div> <div class="item">Five</div> <div class="item">Six</div>
</div>

CSS

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

.no-wrap {
  flex-wrap: nowrap; /* This is the default; items will shrink to fit on one line. */
}

.item { width: 150px; } /* Items have a fixed width. */

Explanation

By default, flex-wrap is nowrap. This means all flex items will be forced to stay on a single line, even if it causes them to overflow the container or shrink.


Example 2: Wrapping onto Multiple Lines

HTML

<div class="flex-container wrap">
  <div class="item">One</div> <div class="item">Two</div> <div class="item">Three</div>
  <div class="item">Four</div> <div class="item">Five</div> <div class="item">Six</div>
</div>

CSS

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

.wrap {
  flex-wrap: wrap; /* Allows items to wrap to a new line if needed. */
}

.item { width: 150px; }

Explanation

Setting flex-wrap: wrap allows the flex items to wrap onto additional lines if they exceed the width of the container. Items will wrap from top to bottom.


Example 3: Reverse Wrapping

HTML

<div class="flex-container wrap-reverse">
  <div class="item">One</div> <div class="item">Two</div> <div class="item">Three</div>
  <div class="item">Four</div> <div class="item">Five</div> <div class="item">Six</div>
</div>

CSS

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

.wrap-reverse {
  flex-wrap: wrap-reverse; /* Items wrap onto new lines from bottom to top. */
}

.item { width: 150px; }

Explanation

flex-wrap: wrap-reverse behaves like wrap, but new lines are added from the bottom of the container upwards instead of top-to-bottom. This creates a "stacked from the bottom" effect.


Example 4: A Responsive Grid of Cards

HTML

<div class="card-container">
  <div class="card">Card 1</div> <div class="card">Card 2</div>
  <div class="card">Card 3</div> <div class="card">Card 4</div>
</div>

CSS

.card-container {
  display: flex;
  flex-wrap: wrap; /* Ensures cards wrap on smaller screens. */
  border: 2px solid #d9534f;
}

.card {
  flex: 1 1 200px; /* Grow, shrink, with a base width of 200px. */
  margin: 10px;
  height: 100px;
  background: #f7e6e5;
}

Explanation

This is a classic responsive pattern. flex-wrap: wrap allows the cards to form a grid. As the viewport narrows, cards that don't fit will wrap to the next line automatically.


Example 5: Tag Cloud Layout

HTML

<div class="tag-cloud">
  <span class="tag">HTML</span> <span class="tag">CSS</span> <span class="tag">JavaScript</span>
  <span class="tag">Flexbox</span> <span class="tag">Grid</span> <span class="tag">Responsive</span>
  <span class="tag">Web Design</span>
</div>

CSS

.tag-cloud {
  display: flex;
  flex-wrap: wrap; /* Allows tags to flow naturally onto multiple lines. */
  padding: 10px;
  border: 2px solid #5bc0de;
}

.tag {
  background-color: #e8f7fc;
  padding: 5px 10px;
  margin: 5px;
  border-radius: 4px;
}

Explanation

flex-wrap: wrap is ideal for creating tag clouds or lists of chips. The tags will naturally flow and wrap based on the available width of the container, creating a clean and compact layout.


Example 6: Combining Wrap with Column Direction

HTML

<div class="multi-column-container">
  <div class="item">1</div> <div class="item">2</div> <div class="item">3</div>
  <div class="item">4</div> <div class="item">5</div> <div class="item">6</div>
  <div class="item">7</div> <div class="item">8</div> <div class="item">9</div>
</div>

CSS

.multi-column-container {
  display: flex;
  flex-direction: column; /* Main axis is vertical. */
  flex-wrap: wrap; /* Allows wrapping into new columns. */
  height: 150px; /* The container must have a fixed height for this to work. */
  border: 2px solid #f0ad4e;
}

.item { height: 40px; width: 100px; }

Explanation

When you combine flex-direction: column with flex-wrap: wrap, the items will fill the container's height and then wrap into a new column. This requires a defined height on the flex container.


Example 7: Preventing Wrap for a Squeezed Layout

HTML

<div class="squeezed-layout">
  <div class="item-no-shrink">Fixed</div>
  <div class="item-grows">Stretches</div>
  <div class="item-shrinks">This item shrinks</div>
</div>

CSS

.squeezed-layout {
  display: flex;
  flex-wrap: nowrap; /* Explicitly prevent wrapping. */
  width: 400px;
  border: 2px solid #333;
}

.item-no-shrink { flex-shrink: 0; } /* This item will not shrink. */
.item-grows { flex-grow: 1; } /* This item will grow to fill space. */
.item-shrinks { background-color: #eee; }

Explanation

Here, flex-wrap: nowrap is used to force all items onto one line. This demonstrates how individual flex item properties (flex-shrink, flex-grow) interact with the container's wrapping behavior to create specific layout effects.