×

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

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.

CSS Superscript and Subscript

Superscript and Subscript To specify the superscript and the subscript text, HTML provides us two tags that are <sup> and <sub>. The superscript content looks in the shorter font and half...

2 minutes read.

CSS Text-align

Text-align The text-align property of CSS sets the table-cell box’s horizontal alignment or any block element. The text-align property is the same as the CSS property vertical-align but towards any horizontal...

1 minute read.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

CSS Animation

CSS Animation: The animation property of CSS is applied to make animation over any webpage. It is used as the animation replacement made by JavaScript and Flash. Rules of CSS3 @keyframes The...

3 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 Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

CSS Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

4 minutes read.

Difference between CSS Grid and CSS Flexbox

The way that information is arranged on a website reveals a lot about you or your company. Organizational abilities are just as important as having the right materials. The design...

3 minutes read.

CSS Hover

CSS Hover: CSS :hover is used to select the various elements whenever we point the mouse on these elements. We can also use this selector on all the HTML elements,...

3 minutes read.

CSS Sticky

CSS Sticky The position property in CSS is applied to set an element’s position. This property is also applied to position an element behind the other element. It is useful for...

1 minute read.

CSS Pseudo-elements

Pseudo-elements The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be...

6 minutes read.

CSS Pseudo-class

Pseudo-class The pseudo-class may be depicted as the keyword which can be mixed to any selector that specifies the elected element’s unique state. It can be included to a selector to...

6 minutes read.

CSS Text Effects

Text Effects in CSS We can use distinct text effects used inside the HTML document. Few attributes can be applied to insert the text effects. We can also style various web documents...

5 minutes read.

CSS Background

CSS Background The properties of CSS background are applied to describe the effects of background on the element. Here are five properties of background that affects various elements of HTML: Background-colorBackground-imageBackground-repeatBackground-attachment Background-position Background-color The color of the background...

7 minutes read.

CSS Float

CSS Float: The float property of CSS is the positioning property. This property is applied to elements to push them either to left or right. It permits for wrapping facility...

4 minutes read.

How to change font size in CSS

How to change font size in CSS? Font Size: - Users may attract attention to content on a website page in various ways. One element in a sentence can be highlighted. You...

5 minutes read.

CSS White Space Property

White Space in CSS The white-space CSS property describes how to show the content inside an element. This property is applied to manage a white space within an element. Values of White-space...

2 minutes read.

CSS Specificity

CSS Specificity: If one or more conflicting rules of CSS express a similar element, a browser will pursue a few rules to examine the specific one. Specificity can be specified...

3 minutes read.

Divi in CSS

Divi is popular WordPress themes due to its versatility and user-friendly interface. It includes a built-in page builder for sophisticated design options and an assortment of customization features to help...

9 minutes read.