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

Display Flex


This essential CSS property transforms a block-level element into a flex container. By applying display: flex, you activate the Flexbox model, and all direct children of that element automatically become flex items. This allows you to arrange and align these items along a flex line.

Example 1: Creating a Basic Flex Container

HTML

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

CSS

/* This is the parent container that will hold our flex items. */
.flex-container {
  display: flex; /* This declaration turns the element into a flex container. */
  border: 2px solid #000;
}

/* Basic styling for the flex items for better visibility. */
.item {
  background-color: #f0f0f0;
  padding: 20px;
  margin: 5px;
  text-align: center;
}

Explanation

The .flex-container div is converted into a flex container by setting its display property to flex. Its direct children, the .item divs, are now flex items and are arranged horizontally by default.


Example 2: Inline Flex Container

HTML

<span>I am text before the container.</span>
<div class="inline-flex-container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
</div>
<span>I am text after the container.</span>

CSS

/* This container will behave like an inline element. */
.inline-flex-container {
  display: inline-flex; /* Creates a flex container that is inline with surrounding content. */
  border: 2px solid #4CAF50;
}

/* Styling for the child items. */
.item {
  background-color: #e7f7e8;
  padding: 15px;
}

Explanation

Using display: inline-flex makes the container an inline-level element, allowing it to sit on the same line as other text or inline elements. The children inside still benefit from flex properties.


Example 3: Nested Flex Containers

HTML

<div class="outer-container">
  <div class="item">Outer 1</div>
  <div class="nested-container">
    <div class="nested-item">Nested 1</div>
    <div class="nested-item">Nested 2</div>
  </div>
  <div class="item">Outer 3</div>
</div>

CSS

/* The main flex container. */
.outer-container {
  display: flex; /* Establishes the primary flex context. */
  border: 2px solid #337ab7;
  padding: 10px;
}

/* The nested div which is also a flex container. */
.nested-container {
  display: flex; /* This flex container is an item of the outer container. */
  border: 2px dashed #f0ad4e;
  margin: 0 10px;
}

Explanation

A flex item can also be a flex container. Here, .nested-container is a flex item of .outer-container and a flex container for the .nested-item elements, allowing for complex, multi-level layouts.


Example 4: Flexbox for Navigation Bar

HTML

<nav class="nav-menu">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Contact</a>
</nav>

CSS

/* Using flexbox to create a horizontal navigation menu. */
.nav-menu {
  display: flex; /* Turns the nav element into a flex container. */
  background-color: #333;
  list-style-type: none;
  padding: 0;
  margin: 0;
}

.nav-menu a {
  color: white;
  padding: 14px 20px;
  text-decoration: none;
  text-align: center;
}

Explanation

Applying display: flex to the <nav> element is a modern and efficient way to create a navigation bar. The anchor tags automatically align horizontally, simplifying the code required.


Example 5: Centering a Single Item

HTML

<div class="centering-container">
  <div class="centered-item">Centered!</div>
</div>

CSS

/* A common use case: centering an item both vertically and horizontally. */
.centering-container {
  display: flex; /* Activates flexbox. */
  height: 200px;
  border: 2px solid #d9534f;
  justify-content: center; /* Centers horizontally. */
  align-items: center;     /* Centers vertically. */
}

.centered-item {
  background-color: #f7e6e5;
  padding: 25px;
}

Explanation

By setting display: flex on the parent, we can use the justify-content and align-items properties to perfectly center the child element within the container. This is a classic and highly practical use of Flexbox.


Example 6: Switching from block to flex on Hover

HTML

<div class="dynamic-container">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
</div>

CSS

/* The container starts as a standard block element. */
.dynamic-container {
  display: block;
  border: 2px solid #5bc0de;
  padding: 5px;
  transition: all 0.5s ease; /* Smooth transition for the change. */
}

/* On hover, the container becomes a flex container. */
.dynamic-container:hover {
  display: flex; /* Items will align horizontally when the user hovers. */
}

Explanation

This example demonstrates how display: flex can be used dynamically. The items are initially stacked vertically (display: block), but transition to a horizontal flex layout when the user hovers over the container.


Example 7: A Form Layout with Flexbox

HTML

<form class="flex-form">
  <input type="email" placeholder="Enter your email">
  <button type="submit">Subscribe</button>
</form>

CSS

/* Using flexbox to align form elements. */
.flex-form {
  display: flex; /* Aligns the input and button on the same line. */
  width: 400px;
}

.flex-form input[type="email"] {
  flex-grow: 1; /* Allows the input field to take up available space. */
  border: 1px solid #ccc;
  padding: 10px;
}

.flex-form button {
  padding: 10px 20px;
  border: none;
  background-color: #0275d8;
  color: white;
}

Explanation

display: flex is applied to the form to place the input field and button side-by-side. The flex-grow property is then used on the input to make it expand and fill the remaining space in the container.