Premium CSS Course

CSS Flexbox

Flexbox, short for Flexible Box, is a layout model in CSS designed to help you create complex, responsive layouts with ease. It allows for dynamic and flexible arrangements of elements within a container, making it perfect for building web pages that adapt to different screen sizes and resolutions.

What is Flexbox?

Flexbox works by applying a flex container to a parent element, with the child elements (flex items) automatically adjusting their size and placement based on the available space and the properties set on the container.

Creating a Flex Container

To start using Flexbox, you need to define a flex container. This is done by applying display: flex or display: inline-flex to the parent element.


        .container {
            display: flex;
        }
    

The display: flex property establishes a flex container, and all its direct child elements automatically become flex items.

Flexbox Properties

Flexbox provides several properties to control the layout of both the container and the items inside it.

Flexbox Item Properties

Flex items also have their own set of properties to control their size and positioning within the container.

Example of Flexbox Layout

Here’s an example of how to create a simple flexbox layout:


        .container {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
        }

        .item {
            flex: 1;
            padding: 20px;
            background-color: #f0f0f0;
            text-align: center;
        }
    

This example sets up a flex container with horizontal layout, space between items, and center alignment along the vertical axis.

Best Practices for Using Flexbox