SHARE
Facebook X Pinterest WhatsApp

Introduction to CSS Flexible Box Layout

Written By
thumbnail
Vipul Patel
Vipul Patel
Jun 22, 2014

In an earlier article

https://www.htmlgoodies.com/css/working-with-css-box-model-in-your-web-apps-part-1/

, we learned about block-level box layout, line box layout and inline-level box layout.

In this article, we will explore another type – flexible box layout.

Quoting the CSS3 specification for flexible box layout,

Source: http://www.w3.org/TR/css3-flexbox/

In the flex layout model, the children of a flex container can be laid out in any direction, and can “flex” their sizes, either growing to fill unused space or shrinking to avoid overflowing the parent.

 

This implies that unlike the earlier box layouts, there is more flexibility with this layout w.r.t horizontal and vertical alignment.

Here are a few characteristics of flexible box layout.

  • They can flow in any direction – left, right, down or even upwards
  • Display order can be reversed or rearranged
  • They can be laid out linearly along a single axis
  • They can be wrapped into multiple lines along a secondary axis.
  • They can be sized according to available space
  • They support alignment to their container element or with each other
  • They can be collapsed keeping relative cross size.

Properties

display

The display property now supports new value corresponding to flex.

When ‘display’ is set to ‘flex’, it cases an element to generate a block-level flex container box.

When ‘display’ is set to ‘inline-flex’, it cases an element to generate an inline-level flex container box.

flex-direction

This propery specifies how flex items are placed in a flex container. The valid values are “row”, “row-reverse”, “column”, and “column-reverse”.

Value of “row” implies that flex-container’s main axis has the same orientation as the inline axis.

Value of “row-reverse” implies the same as “row”, except the “main-start” and “main-end” directions are opposite.

Value of “column” implies that flex-container’s main axis has the same orientation as the block axis.

Value of “column-reverse” implies the same as “column”, except the “main-start” and “main-end” directions are opposite.

 

flex-wrap

This property contains whether the flex container is single-line or multi-line. The acceptable values are “nowrap”, “wrap”, and “wrap-reverse”.

 

flex-flow

This property defines the flex container’s main and cross axes.

The acceptable values are ” flex-direction” and “flex-wrap”.

 

flex

This property specifies the components of flexible length. The acceptable values for this property are “none”,  “flex-grow”, “flex-shrink”, and “flex-basics”.

 

 

Here are some differences between a flex container and a block container.

–          Any “column-*” properties are ignored.

–          “float” and “clear” properties have no effect.

–          “vertical-align” property has no effect.

 

Here is an example declaration of a flexible box layout.

 

<style>

#myId {

display: flex;

flex-flow : column;

}

</style>

 

As we can see, flexible box layout removes the rigidity of element layout in the box layout model. I hope you have found this information useful.

 

About the author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com

 

Recommended for you...

Importing Custom Less Styles at Run-time in Angular
Rob Gravelle
Jun 14, 2022
Setting CSS Values from Less Variables in Angular
Rob Gravelle
Jun 4, 2022
Color Manipulation with JavaScript
Rob Gravelle
May 21, 2022
An Introduction to CSS-in-JS
Rob Gravelle
May 14, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.