Close

CSS Flexbox – An Exploration

CSS3 has introduced a new layout method, the ‘revamped’ Flexbox with excellent browser support. I had written a small reference about Flexbox in my previous article, The Most Useful CSS3 Features.

The Flexbox was created to overcome the limitations of Floats, Display, Relative units (% or em) for sizing, Media queries and JavaScript. Before Flexbox, the layout methods were very rigid that needed a lot of coding.

What is Flexbox?

Flexbox is a new layout model in CSS3. Flexbox, a term taken from ‘Flexible box’, makes the positioning of child elements much easier and complex layouts can be achieved with less effort and cleaner code.

“The Flexbox layout can arrange its child elements which can become growable or shrinkable to fit into a given space and to display on all kinds of devices and screens.”

 

Flexbox – A Visual Guide

How to use Flexbox?

Applying the Flexbox method is really simple. Say, we have a parent element that has a few child elements. To make the flexbox work, we should declare it by setting display:flex; in the parent element. A basic example with the codes are given below:

Note: For the sake of readability, I’m omitting some vendor prefix codes here. You can see them in the live demo.

.flex-container {
  display: -webkit-flex; /*for webkit browsers*/
  display: flex; /*all browsers*/
}

/* for demo purpose only */
.flex-container .child-item {
  background-color: #fff;
  color: #000;
  padding: 10px;
  margin: 10px;
  font-size: 13px;
}

The result should be like this

The above example shows three flex items. They are positioned by default: along the horizontal flex line, from left to right (If page direction: ltr;).

Some of the most popular flexbox properties are:

For Flex container

  1. flex-direction: row | row-reverse | column | column-reverse ;
  2. flex-wrap: nowrap | wrap | wrap-reverse;
  3. align-items: flex-start | flex-end | center | baseline | stretch;
  4. justify-content: flex-start | flex-end | space-between | center | space-around;
  5. align-content: flex-start | flex-end | center | space-between | space-around | stretch;

For Flex items

  1. align-self: auto | flex-start | flex-end | center | baseline | stretch;
  2. flex-grow: number;
  3. flex-shrink: number;
  4. flex-basis: auto | width;
  5. order: number;
  6. flex: none | auto | [ <flex-grow> <flex-shrink>? || <flex-basis> ];

Let’s play with a few examples

For Flex container

Flex container is the parent of flex items / flex childs. To enable Flexbox feature, we should put display: flex; to the flex container. However the flex container supports other awesome stuffs, that are only allowed in the flex container. So we don’t need to add flex properties to their child items. Let’s discuss about the main flex properties which work only in the flex container.

1. flex-direction

The flex-direction property specifies how these items are placed in the flex container, by setting the direction of the container’s main axis. This determines the direction that the flex items are laid out in.  

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

 

The result should be like this

See the Pen Flexbox Example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

2. flex-wrap

The CSS flex-wrap property specifies whether flex items are forced into a single line or can be wrapped onto multiple lines. If wrapping is allowed, this property also enables you to control the direction in which lines are stacked.

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

 

The result should be like this

See the Pen Flexwrap Example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

3. align-items

The CSS align-items property, aligns flex items of the current flex line in the same way as justify-content but this in the perpendicular direction (align flex items in the cross-axis).  

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

 

The result should be like this

See the Pen Flex Align-items Example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

4. justify-content

The CSS justify-content property defines how the browser distributes space between and around flex items along the main-axis of their container.

.flex-container {
 justify-content: flex-start | flex-end | space-between | center | space-around;
}

 

The result should be like this

See the Pen Flex Justify Content Example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

5. align-content

The CSS align-content property coordinates a flex container’s lines within the flex container when there is extra space on the cross-axis. Align-content property affects only those flex items that has multiple lines. So, it won’t work when the flex items are in single line. To ensure that the items are spread across multiple lines, we should add the flex-wrap property to wrap the flex items in the container.

.flex-container {
  align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}

 

The result should be like this

See the Pen Flex Align Content Example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

For Flex items

Flex items are the items of a flex container. They are positioned along a main axis and a cross axis. The main axis is horizontal by default. So the items flow into a row that can flip the main axis by setting flex-direction to column and it’s set to row by default in adding flex container.

1. align-self

The align-self CSS property aligns flex items of the current flex line overriding the align-items value. If any of the flex item’s cross-axis margin is set to auto, then align-self is ignored.

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

 

The result should be like this

See the Pen Flex align-self Example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

2. flex-grow

A flex-grow value is a number which determines how much the flex item will grow, relative to the rest of the flex items in the flex container, when positive free space is distributed. The initial value is zero, and negative numbers are invalid.

.child-item {
  flex-grow: number;
}

 

The result should be like this

See the Pen Flex Grow Example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

3. flex-shrink

The flex-shrink CSS property specifies the flex shrink factor of a flex item. It determines how much a flex item can grow relative to the rest of the flex items in the flex container when the negative free space is distributed.

By default, all flex items can be shrunk, but if we set it to 0 (don’t shrink) they will maintain the original size. Negative numbers are invalid.

.child-item {
  flex-shrink: number;
}

 

The result should be like this

See the Pen Flex Shrink Example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

4. flex-basis

This property takes the same values as the width and height properties, and specifies the initial main size of the flex item, before free space is distributed according to the flex factors.

.child-item {
  flex-basis: auto | width;
}

 

The result should be like this

See the Pen Flex Basis Example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

5. order

Flex items are aligned in the same order as we declare them in the source code. But the flex order allows changes to be made to this order based on our requirement. The order property controls the order in which the flex items appear within their flex container, by assigning them to ordinal groups.

Note that the order of the flex items in the flex container will not change in the source code, it will affect only the output result once the browser is rendered.

.child-item {
  order: number;
}

 

The result should be like this

See the Pen Flex order example by Nikhil Toobler (@nikhil-toobler) on CodePen.

 

6. flex

Flex property is the shorthand for the flex-grow, flex-shrink and flex-basis properties.

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

 

Browser support

This is a tough one to answer. Flexbox layout is completely supported by popular browsers like Chrome, Firefox, Safari, etc which are always  up-to-date and the older versions don’t support Flexbox. Can I use allows us to refer to the current information they have posted on their website.

Check out the latest screenshot from Can I use, and figure out the spectacular browser support.

Summing-up

Flexbox is an extremely powerful layout model in CSS3, as it helps web developers to achieve more condensed layout in small bits of code, in the best timely manner. Even Bootstrap included flexbox in their latest version of Bootstrap 4 which is powered with their grid system.

In the absence of flexbox, we may be using traditional CSS properties like Floats, Display, Relative units (% or em) for sizing, Media queries and JavaScript. But two of the major drawbacks of using flexbox are the performance issues and browser compatibility, it is not supported by most of the browsers and it has been reported that the use of flexbox increases the page loading time for larger projects. Let’s hope that these issues will be solved in the near future.We are also able to write less code, thus making it more versatile. It is of tremendous value for responsive layouts as it doesn’t require traditional coding either.

Furthermore, we have a new CSS3 feature, the CSS GRID, the native css grid feature which is slightly same as the flexbox. But as both flexbox and CSS grid have varied applications, they should be used together, rather than as alternatives to one another. Let’s catch up on this in another article.

Hope this will enlighten your knowledge on Flexbox. And be sure to let me know your valuable thoughts in the comments below.

Want to get in touch with us? Click here

 

Latest stories

  • If you are in Jordan looking for app development support you should read this

    From downloading a mobile app  to visiting a brick-and-mortar store, customers today value every interaction with a brand. A great customer experience leaves a lasting impression in their minds about the happy and satisfying experience.  Winning the customer experience race Every  interaction offers  a make-or-break opportunity for a brand to either  build a loyal customer […]

  • Enhanced Efficiency & Optimized Cost of Project Development with Sweden Hybrid Model.

    Going beyond the cost factor and striving to deliver quality, innovation and flexibility to its customers across the globe, Toobler introduced a Hybrid (Onshore and Offshore) Software Development Model. Huge popularity of the hybrid model is fuelled by scores of organizations choosing to keep design and architecture phases at geographic proximity.  Driven by a customer-centric […]

  • 7 Pro Tips: Network Like a Boss at GITEX 2019

    The biggest tech show in the Middle East, North Africa & South Asia- GITEX Technology week takes place in Dubai World Trade Centre from Oct 6 – Oct 10. The opportunity to network with prospects, key decision-makers and business heads of the tech industry makes GITEX an unmissable event.  What makes the show exceptional is […]

Leave a Reply

Your email address will not be published. Required fields are marked *