×

CSS Flex-Box

Introduction

The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout.

Flex-box is a one dimensional layout model, i.e. it works either with a row or with a column. This module provides an effective way to use the space among the items of a container, even when the size is variable or unknown.

The primary aim of flex-box is to allow the container to change the width or height of its items and to accommodate the available space (according to different devices and screen sizes).

There are two main components of flex-box:

Flex container: It is an area of a document laid out using flex-box. The flex container is created by assigning the values flex or inline-flex to the display property. This component acts as a parent element of the flex-box.

Flex items: The flex item is the direct child of the flex container (parent element). The amount of flex items may be one or more than one.

CSS Flex-Box

The Main Axis:

The flex-direction property defines the main axis. It has four property values.

  • row
  • row-reverse
  • column
  • column-reverse

When you select row or row-reverse, the main-axis will move along the row in inline direction.

CSS Flex-Box

When you select column or column-reverse, the main-axis will move from top to the bottom of page, in block direction.

CSS Flex-Box

The Cross Axis:

When the flex-directionor the main axis is assigned the values, row or row-reverse, cross axis moves down the columns. Thus, the cross axis runs perpendicular to main axis.

CSS Flex-Box

When the flex-directionor is assigned the values column or column-reverse, cross axis moves along the rows.

CSS Flex-Box

Consider the example:

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
    display: -webkit-flex;
    display: flex;
    width: 300px;
    height: 150px;
    background-color: skyblue;
}
.flex-item {
    background-color: white;
    margin: 10px;
    padding: 20px;
    height: 80px;
}
</style>
</head>
<body>
<h3>CSS Flex Box</h3>
<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>

Output:

CSS Flex-Box

Here, the output displays three flex items in a flex container. horizontal flex line is the default value of flex items.

Consider another example, here we use the direction property, and it is set to rtl (right to left).

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
    display: -webkit-flex;
    display: flex;
    width: 300px;
    height: 150px;
    background-color: skyblue;
}
.flex-item {
    background-color: white;
    margin: 10px;
    padding: 20px;
    height: 80px;
}
body{
  direction: rtl;
}
</style>
</head>
<body>
<h3>CSS Flex Box</h3>
<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>

Output:

CSS Flex-Box

In the above output, we use the direction: rtl; property.

CSS3 Flexbox has many notable properties for both the parent (flex container) and the children (flex items):

For the parent (flex container):

  • o display:
    This property is used to select the type of box. It displays a flex container, either block or inline.

Syntax:

.container{
display: flex | inline-flex | block | inline-block;}
  • flex-direction:
    This property creates the main axis, and defines the direction of flex items placed in the flex container. 

Syntax:

.container{
flex-direction:  row | row-reverse | column | column-reverse;          }

where,

  • row: It is the default value. Forleft toright, value is ltr and for right to left, value is rtl.
  • row-reverse: ltr is for right to left and rtl is for left to right.
  • column: It is similar to row but the flow is top to bottom.
  • column-reverse: It is similar to row-reverse but the flow is bottom to top.
  • flex-wrap:
    By default, all the flex items try to fir in a single line. Flex-wrap property allows you to wrap the items if necessary. 

Syntax:

.container{
flex-wrap:  nowrap | wrap | wrap-reverse;                                         }

where,

  • nowrap is the default value of flex-wrap. When you use nowrap all the flex items will be in a single line.
  • wrap: the flex items are wrapped onto multiple lines from top to bottom.
  • wrap-reverse: the flex items are wrapped onto multiple lines from bottom to top.
  • flex-flow:
    This property acts as a shorthand for flex-wrap and flex-direction properties. It defines the main axes and cross axes of flex container.
            The default value of flex-flow is row nowrap.

Syntax:

.container{
flex-flow: row wrap;
}
  • justify-content:
    This property specifies the alignment along the row i.e. the main axis.
    It distributes the space left when all the flex items have reached the maximum size, or the items in the line are inflexible.

Syntax:

.container{
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly | start | end | left | right … + safe | unsafe;
}
  • flex-start: It is the default value. It packs the items to the start of flex-direction.
  • flex-end: It packs the items to the end of flex-direction.
  • start: It packs the items to the start of writing-mode direction.
  • end: It packs the items to the end of writing-mode direction.
  • left: It packs the items to left side of the container.
  • right: It packs the items to right side of the container.
  • center: It places the items to the center of container.
  • space-between: It distributes the items evenly in the line.
  • space-around: It distributes the items evenly in the line but with equal space around them.
  • space-evenly: It distributes the items such that there is equal space between two consecutive items and the edges.
  • align-items:
    The property defines how the flex items are displayed along cross axis on the line. 

Syntax:

.container{
align-items:  stretch | flex-start | flex-end | center | baseline | first baseline | last baseline | start | end | self-start; }

where,

  • stretch: is the default value and uses to fill the container.
  • flex-start / self-start / start : It places the items to the start of cross axis.
  • flex-end / self-end / end: It places the items to the end of cross axis.
  • center: It places the items to center of cross axis.
  • baseline: It aligns the items according to the baseline.
  • align-content:
    This property aligns the lines of flex container, when there is extra space in cross axis. This property is similar to justify-content where it aligns the items in the main axis.

Syntax:

.container{
align-content: flex-start | flex-end | center | space-between | space-around | space-evenly | stretch | start | end | baseline | first baseline | last baseline;

where,

  • normal: is the default value where the items are packed in default position.
  • flex-start/ start: It packs the items to the start of flex container.
  • flex-end / end: It packs the items to the end of the container.
  • center: It places the items to the center of container.
  • space-between: It distributes the items evenly in the line.
  • space-around: It distributes the items evenly in the line but with equal space around them.
  • space-evenly: It distributes the items such that there is equal space between two consecutive items and the edges.
  • stretch: the lines are stretched to cover the remaining space.

For the children (flex items):

  • order:
    It controls the order of the flex items in which they are visible in the container. The default value of order property is 0.

Syntax:

.item{
order: value; 
}
  • flex-grow:
    This property allows the flex item to grow if required. You can set a value to the flex-grow, the value has no unit. Default value of flex-grow is 0.

Syntax:

.item{
flex-grow: value; 
}
  • flex-shrink:
    It allows flex items to shrink if necessary. The default of flex-shrink is 1 and negative values are invalid.

Syntax:

.item{
flex-shrink: value;
}
  • flex-basis:
    The flex-basis property sets default size of an element before the remaining space is distributed. 

Syntax:

.item{
flex-basis: value | auto;
}

When the flex-basis is set to 0, extra space around content is not factored in. When the value is set to auto(default value), extra space is divided according to value of flex-grow property.

  • flex:
    This property is shorthand for flex-grow, flex-basis, flex-shrink together.
    The last two parameters (flex-basis & flex-shrink) are optional. 

Syntax:

.item{
flex: none | [ <'flex-grow'><'flex-shrink'> ? || <'flex-basis'> ]      }

It is recommended to use flex shorthand property rather than the individual properties.

  • align-self:
    It allows the individual flex items to align themselves, by overriding the alignment sets by align-items property.

Syntax:

.item{
align-self: auto | flex-start | flex-end | center | baseline | stretch;}

Supporting Browsers:

Property
Browser name Basic support (single-line flexbox) multi-line flexbox
Google Chrome 29.0 21.0 - webkit- 29.021.0 - webkit-
Internet Explorer 11.0 11.0
Mozilla Firefox 22.0 18.0-moz- 28.0
Opera Browser 12.1 - webkit- 17.0 15.0 - webkit- 12.1
Safari Browser 6.1 - webkit- 6.1 - webkit-

In this way, we have learnt the Flexbox (Flexible Box Layout) Module of CSS and its various properties to create a flexible layout environment.


Related Topics

CSS 3D Transform

Introduction To understand 3D transformation property of transform you need to first understand the difference between 2D and 3D transform. 2D transform is a CSS effect which transforms the elements according to...

6 minutes read.

CSS Page-break-after Attribute

Page-break-after Attribute As its name implies, the page-break-before property is applied to include page break after any element, at the time of printing a document. It adds the page break after...

5 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

CSS Text-Overflow

Introduction: The text-overflow property of CSS is used to format the text which is overflowing the boundaries of the container. This property helps user to clip the text, to display the...

5 minutes read.

Radial-gradient() CSS function

Radial-gradient() CSS function It defines a CSS in-built function, which produces a smooth transition among multiple colors. It is used to set any radial-gradient that is same as a background-image. This...

4 minutes read.

CSS Arrow

CSS Arrow The arrow of CSS is applied to insert an array with the tooltip. It can make a tooltip as any speech bubble. Also, this arrow can be illustrated in...

4 minutes read.

CSS Images

Images in CSS: Images are a critical part of a web application. It includes various types of images in the web application that cannot be recommended. However, it is essential...

4 minutes read.

How to include CSS

How to include CSS CSS is included in HTML pages for formatting the documents based on the information inside a style sheet. We can add CSS in HTML documents in the following three ways: Inline...

5 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Important

CSS Important: CSS !important property is used to provide more importance, as compared to other CSS properties. The meaning of this property is “This is important.” This property enables CSS...

2 minutes read.

CSS User Interface

Introduction User Interface can be defined as series of visual elements like screens, buttons, forms that help the user interact with the software application or hardware device. CSS User Interface allows the...

6 minutes read.

How to change the font in CSS

How to change the font in CSS? Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish...

4 minutes read.

Combine Background Image with Gradient Overlay

With the help of CSS gradient, we can display smooth and straightforward transitions between two or more colours. We can add the background images by combining the gradient property with...

2 minutes read.

Grid Vs Flexbox in CSS

Grid: - CSS Grid Layout is a 2D grid-based matrix-based layout method with columns and rows that simplifies web pages created by eliminating the need for floats and placement. A grid layout, like...

4 minutes read.

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.

CSS Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.

CSS Navigation bar

CSS Navigation bar: A navigation system or navigation bar comes upon GUI that can help many visitors to access the information. A Navigation bar is a UI component on the...

11 minutes read.

CSS Font

CSS Font: CSS has font property, which makes the text more attractive. We can alter the entire look by changing font color, font size, font style, and much more. In...

5 minutes read.

CSS Flex-Box

Introduction The flex-box property of CSS stands for Flexible Box Layout Module. It is specifically used to design a flexible responsive layout. Flex-box is a one dimensional layout model, i.e. it works...

6 minutes read.

CSS Comments

CSS Comments: Generally, comments in CSS are used to describe our code. Further, it will provide support to many users who are concerned with the code. Somewhat, these are ignored...

2 minutes read.