CSS – Flex

By Published 4 min read

In CSS, “Flex” refers to “Flexbox,” which is short for “Flexible Box Layout.” Flexbox is a layout model that provides a flexible and efficient way to arrange and distribute space among items within a container, even when the sizes of those items are unknown or dynamic. It was introduced to simplify the design of complex layouts in web development.

With Flexbox, you can create both simple and intricate layouts, such as navigation menus, equal-height columns, or centered content, with relative ease. It offers a predictable way to control the alignment and positioning of elements, making it a valuable tool for responsive web design.

The key components of Flexbox include the use of a “flex container” and “flex items.” A flex container is an HTML element that becomes a parent for the elements you want to lay out in a flexible manner. Flex items are the child elements within the flex container that are arranged based on the rules defined by the container.

Flexbox properties, like display: flex;, flex-direction, justify-content, align-items, and others, allow you to control the flow, alignment, and distribution of space along the main and cross axes within the flex container.

It allows you to distribute space and align content within a container, even when the size of your items is unknown or dynamic. Here’s an explanation of CSS Flexbox:

1. display: flex;

  • Example:
   .flex-container {
     display: flex;
   }Code language: CSS (css)

The display: flex; property is applied to a container element to create a flex container. It enables the flex layout model for the children of that container.

2. flex-direction

  • Example:
   .flex-container {
     display: flex;
     flex-direction: row;
   }Code language: CSS (css)

The flex-direction property defines the main axis along which flex items will be placed within the container. It can be set to values like row, row-reverse, column, or column-reverse to control the direction of items.

3. justify-content

  • Example:
   .flex-container {
     display: flex;
     justify-content: space-between;
   }Code language: CSS (css)

justify-content determines how extra space is distributed along the main axis. It can be set to values like flex-start, flex-end, center, space-between, or space-around.

4. align-items

  • Example:
   .flex-container {
     display: flex;
     align-items: center;
   }Code language: CSS (css)

align-items controls how items are aligned along the cross-axis (perpendicular to the main axis). You can use values like flex-start, flex-end, center, baseline, or stretch.

5. flex-wrap

  • Example:
   .flex-container {
     display: flex;
     flex-wrap: wrap;
   }Code language: CSS (css)

The flex-wrap property determines whether flex items should wrap to the next line if they overflow the container. It can be set to nowrap, wrap, or wrap-reverse.

6. flex-grow, flex-shrink, and flex-basis

  • Example:
   .flex-item {
     flex: 1 0 auto;
   }Code language: CSS (css)

The flex shorthand property combines flex-grow, flex-shrink, and flex-basis. It allows you to specify how a flex item should grow, shrink, and its initial size.

7. order

  • Example:
   .flex-item:nth-child(2) {
     order: 1;
   }Code language: CSS (css)

The order property controls the order in which flex items are displayed within the container. Lower values place items earlier in the order.

8. align-self

  • Example:
   .flex-item:nth-child(2) {
     align-self: flex-end;
   }Code language: CSS (css)

align-self allows you to override the align-items property for individual flex items, customizing their alignment along the cross-axis.

CSS Flexbox simplifies the creation of complex layouts and makes it easier to build responsive designs. It’s especially useful for arranging items within a container when the number of items and their sizes are not fixed.

In summary, Flex in CSS, or Flexbox, is a layout model designed to simplify the creation of responsive and flexible web layouts. It provides a powerful way to manage the arrangement and positioning of elements, enabling web developers to achieve a wide range of design goals with less complex code.

Example :

<!DOCTYPE html>
<html>
<head>
    <style>
        /* Define a flex container using the flex property */
        .flex-container {
            display: flex;
            flex-direction: row; /* Horizontal flex layout */
            justify-content: space-between; /* Items are evenly spaced */
            align-items: center; /* Items are vertically centered */
            background-color: #f0f0f0; /* Background color for the flex container */
            padding: 20px; /* Padding for the flex container */
        }

        /* Style flex items */
        .flex-item {
            background-color: #336699; /* Background color for flex items */
            color: #ffffff; /* Text color for flex items */
            padding: 20px; /* Padding for flex items */
            text-align: center; /* Center text within flex items */
            flex: 1 1 auto; /* flex-grow, flex-shrink, flex-basis */
            max-width: 30%; /* Maximum width for flex items */
        }
    </style>
</head>
<body>
    <h2>CSS Flex Property Example</h2>

    <!-- Flex container with flex items -->
    <div class="flex-container">
        <div class="flex-item">Flex Item 1</div>
        <div class="flex-item">Flex Item 2</div>
        <div class="flex-item">Flex Item 3</div>
    </div>
</body>
</html>Code language: HTML, XML (xml)

Output :

In this example:

  • We have a basic HTML document with a heading (<h2>).
  • Inside the <style> element, we define styles for creating a flex layout using the flex property:
  • The .flex-container class defines a flex container using display: flex;.
  • We set flex-direction to row to create a horizontal flex layout.
  • justify-content is set to space-between to evenly space the flex items within the container.
  • align-items is set to center to vertically center the flex items.
  • We also set background color and padding for the flex container.
  • The .flex-item class is used to style the individual flex items with background color, text color, padding, and text alignment. The flex property is set to 1 1 auto to specify the flex-grow, flex-shrink, and flex-basis values for the items. max-width limits the maximum width of the items.
  • In the HTML body, we create a flex container with three flex items.

When you open this HTML file in a web browser, you will see a flex layout with three evenly spaced flex items, each styled according to the defined CSS properties. The flex property is a powerful tool for creating flexible and responsive layouts in CSS.

Khan Waseem is a tech lead and full-stack engineer in Bengaluru, building hospitality and SaaS software with Node.js, React and React Native. See the resume