1. Home
css

CSS Tutorial: A Comprehensive Guide

Master CSSS with our in-depth tutorials. From beginner to advanced topics, explore essential concepts and upskill yourself.

  • 23
  • 4
right-top-arrow
14

CSS Flexbox

Updated on 23/09/2024344 Views

In web design, achieving responsive and adaptable layouts is paramount. One of the cornerstone technologies enabling this flexibility is CSS Flexbox. Its approach to layout management, Flexbox simplifies the process of creating complex structures while maintaining responsiveness across various screen sizes. At the heart of this innovation lies the 'display flex' property, serving as the gateway to a world of versatile design possibilities.

Overview

In the realm of web design, CSS Flexbox stands as a powerful tool for creating flexible and responsive layouts. At its core, Flexbox revolves around the concept of flex containers, providing developers with a streamlined approach to organizing and aligning content. Key properties such as flex-direction, flex-wrap, justify-content, and align-items offer granular control over layout behavior, allowing easy creation of intricate designs. Whether aiming to center content, distribute space evenly, or achieve multi-dimensional alignment, CSS Flexbox provides the framework to bring your vision to life.

Understanding Flex Containers

Understanding how to effectively use flex containers can significantly improve the efficiency and scalability of web development projects.

Explanation of Flex Containers

  • Flex containers serve as the foundation of the CSS Flexbox layout.
  • They are elements with the "display: flex" property applied, enabling them to control the layout and alignment of their children (flex items).
  • Flex containers establish a flex formatting context, allowing for flexible and dynamic layouts that adapt to screen sizes and content variations.

Display Flex

  • The "display: flex" property is the primary command to create a flex container.
  • When applied to an element, it transforms into a flex container, allowing its children to become flex items.
  • This property initiates the flex layout context, enabling flexbox properties to control the arrangement and alignment of child elements.

Exploring Flex Container Properties

Flex container’s functionalities and best practices for implementation in web development are discussed below.Flex Direction

Reveals the direction in which flex items are placed within the flex container.

  • Values include "row" (default), "row-reverse", "column", and "column-reverse".Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Flex Direction Example</title>

<style>

.container {

display: flex;

flex-direction: row-reverse;

}

.item {

width: 100px;

height: 100px;

background-color: #f0f0f0;

margin: 5px;

}

</style>

</head>

<body>

<div class="container">

<div class="item">1</div>

<div class="item">2</div>

<div class="item">3</div>

</div>

</body>

</html>

Output:

Flex Wrap

  • Specifies whether flex items should wrap onto multiple lines or not.
  • Values include "nowrap" (default), "wrap", and "wrap-reverse".

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Flex Wrap Example</title>

<style>

.container {

display: flex;

flex-wrap: wrap;

}

.item {

width: 100px;

height: 100px;

background-color: #f0f0f0;

margin: 5px;

}

</style>

</head>

<body>

<div class="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 class="item">10</div>

</div>

</body>

</html>

Output:

Flex Flow

  • A shorthand property combining flex-direction and flex-wrap.
  • Syntax: flex-flow: <flex-direction> <flex-wrap>;

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Flex Flow Example</title>

<style>

.container {

display: flex;

flex-flow: column wrap;

height: 300px; /* Setting a fixed height for demonstration */

}

.item {

width: 100px;

height: 100px;

background-color: #f0f0f0;

margin: 5px;

}

</style>

</head>

<body>

<div class="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 class="item">10</div>

</div>

</body>

</html>

Output:

Justify Content

  • Align flex items to the central axis of the flex container.
  • Values include "flex-start" (default), "flex-end", "center", "space-between", "space-around", and "space-evenly".

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Justify Content Example</title>

<style>

.container {

display: flex;

justify-content: center;

height: 200px; /* Setting a fixed height for demonstration */

background-color: #e0e0e0;

}

.item {

width: 100px;

height: 100px;

background-color: #f0f0f0;

margin: 5px;

}

</style>

</head>

<body>

<div class="container">

<div class="item">1</div>

<div class="item">2</div>

<div class="item">3</div>

</div>

</body>

</html>

Output:

Align Items

  • Align flex items to the cross-axis of the flex container.
  • Values include "stretch" (default), "flex-start", "flex-end", "center", and "baseline".

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Align Items Example</title>

<style>

.container {

display: flex;

align-items: flex-start;

height: 200px; /* Setting a fixed height for demonstration */

background-color: #e0e0e0;

}

.item {

width: 100px;

height: 100px;

background-color: #f0f0f0;

margin: 5px;

}

</style>

</head>

<body>

<div class="container">

<div class="item">1</div>

<div class="item">2</div>

<div class="item">3</div>

</div>

</body>

</html>

Output:

Align Content

  • Aligns flex lines within the flex container when there is extra space on the cross-axis.
  • Values include "stretch" (default), "flex-start", "flex-end", "center", "space-between", and "space-around".

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Align Content Example</title>

<style>

.container {

display: flex;

align-content: center;

flex-wrap: wrap;

height: 300px; /* Setting a fixed height for demonstration */

background-color: #e0e0e0;

}

.item {

width: 100px;

height: 100px;

background-color: #f0f0f0;

margin: 5px;

}

</style>

</head>

<body>

<div class="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 class="item">10</div>

</div>

</body>

</html>

Output:

Importance of Flexbox in CSS Layouts

Flexbox holds significant importance in CSS layouts due to its ability to create flexible and responsive designs with ease. Here's why Flexbox is crucial in CSS layouts, along with examples and their output.Flexible Layouts

Flexbox allows elements to dynamically adjust their size and position based on available space, making layouts adaptable to different screen sizes and content variations.

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Flexible Layout Example</title>

<style>

.container {

display: flex;

}

.item {

flex: 1;

background-color: #f0f0f0;

margin: 5px;

padding: 10px;

}

</style>

</head>

<body>

<div class="container">

<div class="item">1</div>

<div class="item">2</div>

<div class="item">3</div>

</div>

</body>

</html>

Output:

Effortless Alignment

Flexbox simplifies the alignment of elements horizontally and vertically, eliminating the need for complex CSS hacks or positioning techniques.

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Effortless Alignment Example</title>

<style>

.container {

display: flex;

justify-content: center;

align-items: center;

height: 300px; /* Setting a fixed height for demonstration */

background-color: #e0e0e0;

}

.item {

width: 100px;

height: 100px;

background-color: #f0f0f0;

margin: 5px;

}

</style>

</head>

<body>

<div class="container">

<div class="item">1</div>

<div class="item">2</div>

<div class="item">3</div>

</div>

</body>

</html>

Output:

Ordering Flex Items

Flexbox allows for easy reordering of elements without altering the source order in the HTML, providing greater flexibility in layout design.

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Ordering Flex Items Example</title>

<style>

.container {

display: flex;

}

.item {

width: 100px;

height: 100px;

background-color: #f0f0f0;

margin: 5px;

}

.item:nth-child(2) {

order: 1;

}

</style>

</head>

<body>

<div class="container">

<div class="item">1</div>

<div class="item">2 (Reordered)</div>

<div class="item">3</div>

</div>

</body>

</html>

Output:

Responsive Design

Flexbox simplifies the creation of responsive layouts, enabling elements to rearrange themselves automatically based on screen size or device orientation.

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Responsive Design Example</title>

<style>

.container {

display: flex;

}

.item {

width: 100px;

height: 100px;

background-color: #f0f0f0;

margin: 5px;

}

@media screen and (max-width: 600px) {

.container {

flex-direction: column;

}

}

</style>

</head>

<body>

<div class="container">

<div class="item">1</div>

<div class="item">2</div>

<div class="item">3</div>

</div>

</body>

</html>

Output:

Complex Layouts Made Simple

Flexbox provides powerful tools for creating complex layouts, such as navigation menus, grids, or card-based designs, with minimal CSS code.

Code:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Complex Layouts Made Simple Example</title>

<style>

.container {

display: flex;

flex-wrap: wrap;

justify-content: space-between;

}

.item {

flex-basis: calc(33.33% - 20px);

height: 100px; /* Adjust as needed */

background-color: #f0f0f0;

margin-bottom: 20px;

}

</style>

</head>

<body>

<div class="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>

</body>

</html>

Output:

Advantages of CSS Flexbox

The advantages of CSS Flexbox include:

  1. Flexible Layouts: Flexbox enables the creation of dynamic and flexible layouts that adapt to varying screen sizes and device orientations.
  2. Easy Alignment: Flexbox simplifies the alignment of elements horizontally and vertically, eliminating the need for complex CSS hacks or positioning techniques.
  3. Efficient Space Distribution: Flexbox provides powerful tools for distributing space among elements within a container, making it easier to achieve balanced and visually appealing layouts.
  4. Responsive Design: Flexbox facilitates the creation of responsive layouts, allowing elements to rearrange themselves automatically based on screen size or device orientation.
  5. Ordering Flexibility: Flexbox allows for easy reordering of elements without altering the source order in the HTML, providing greater flexibility in layout design.
  6. Minimal CSS Code: Flexbox promotes cleaner and more maintainable CSS code by reducing the reliance on intricate positioning techniques and hacks.
  7. Cross-Browser Compatibility: Flexbox is supported by modern web browsers, ensuring consistent behavior across several platforms and devices.

Final Words

The CSS Display Flexbox Container is a transformative technology in web development, offering unparalleled flexibility and control over layout design. Its intuitive features simplify complex layout challenges while promoting cleaner and more maintainable code. As a cornerstone of modern web design, Flexbox empowers developers to create responsive, visually appealing layouts that adapt seamlessly across different devices. With its importance only growing, Flexbox remains a vital tool in shaping the future of CSS layouts.

FAQs

Q. What is display flexbox in CSS?

A. 'display: flex' is a CSS property that transforms an element into a flex container, laying out its child elements in flexible and responsive ways.

Q. How do you make a container flex in CSS?

A. To make a container flex in CSS, apply the 'display: flex;' property to the container element.

Q. How do I enable flex boxes in CSS?

A. To enable flexboxes in CSS, set the 'display' property to 'flex' of the container element. This can be achieved by adding the following CSS rule to the container element:

.container {

display: flex;

}

Q. How do I align a flexbox container?

A. To align a flexbox container's items, you can use properties like 'justify-content' for horizontal alignment along the main axis and 'align-items' for vertical alignment along the cross-axis. Here's how you can align a flexbox container:

.container {

display: flex;

justify-content: center; /* Horizontal alignment */

align-items: center; /* Vertical alignment */

}

Q. What is the difference between CSS and CSS flexbox?

A. CSS (Cascading Style Sheets) is a styling language that defines a document's presentation created using a markup language like HTML. It defines how elements are displayed, including their layout, colors, fonts, and spacing.On the other hand, CSS Flexbox (Flexible Box Layout) is a layout model in CSS designed for creating flexible and efficient layouts. Flexbox introduces a new way to determine, align, and divide space among items in a container, making it easier to achieve complex layouts that adapt to different devices and screen sizes.

Q. Is flexbox CSS or HTML?

A. Flexbox is a CSS feature, not HTML. It is a layout model introduced in CSS3 to facilitate a more efficient design of flexible and responsive layouts. Flexbox allows developers to control the arrangement, alignment, and distribution of elements within a container, enhancing the capabilities of CSS for layout design. While HTML sets the web page structure and content, CSS, including Flexbox, is used to style and layout that content.

Q. Which is better: flexbox or grid?

A. It depends on the layout requirements. Flexbox is ideal for one-dimensional layouts, while Grid is better for two-dimensional layouts. In many cases, using both together offers the most flexibility and control.

image

Mukesh

Working with upGrad as a Senior Engineering Manager with more than 10+ years of experience in Software Development and Product Management.

Talk to Career Expert
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...