Blog, something for everyone!

Designing CSS Layouts with Flexbox Is As Simple As Making a Pie

Flexible box layout (or flexbox) is a new box model optimized for UI layout. As one of the first CSS segments developed for real structure (floats were really intended mostly for things such as covering written text around images), it makes a lot of projects much easier, or even possible at all. Flexbox’s collection contains the simple focusing of components (both flat in a trench and vertically), the development and shrinkage of components to finish available area, and source-code separate structure, among others capabilities.

Flexbox has resided a varied lifestyle. It began as a function of Mozilla’s XUL, where it was used to lay out program UI, such as the tool bars in Firefox, and it has since been rewritten many times. The requirements has only lately achieved balance, and we have pretty finish assistance across the newest editions of the top internet traveler.

There are, however, some caveats. The requirements modified between the execution in Internet Explorer (IE) and the discharge of IE 10, so you will need to use a a little bit different format. Firefox currently still needs the -webkit- prefix, and Firefox and Opera are still on the much mature format. Firefox has modified to the newest requirements, but that execution is currently behind a playback banner until it is regarded constant and bug-free enough to be switched on by standard. Until then, Firefox still needs the old format.

When you specify that a component will use the flexbox design, its kids are set out along either the straight or horizontally axis, based on the route specified. The sizes of these kids increase or agreement to finish the available area, based on the versatile duration they are allocated.

Example: Horizontally and Straight Focusing (Or The Sacred Grail Of Web Design)

Being able to middle a component on the site is perhaps the variety one wish among Web developers — yes, probably even greater than getting the extremely valued mother or father selector or placing IE 6 out of its agony (OK, maybe a near second then). With flexbox, this is trivially simple. Starting with a primary HTML template, with a heading that we want to centre. Gradually, once we’ve included all the style, it will end up looking like this vertically and horizontally in a trenched based trial.


Bring an element to a centre of a page

OMG, I’m centered




Nothing tricky here, not even a wrapper div. The tricks happens in the CSS.
html { height: 100%;
} body { display: -webkit-box; /* OLD: Safari, iOS, Android browser, older WebKit browsers. */ display: -moz-box; /* OLD: Firefox (buggy) */ display: -ms-flexbox; /* MID: IE 10 */ display: -webkit-flex; /* NEW, Chrome 21+ */ display: flex; /* NEW: Opera 12.1, Firefox 22+ */ -webkit-box-align: center; -moz-box-align: center; /* OLD… */ -ms-flex-align: center; /* You know the drill now… */ -webkit-align-items: center; align-items: center; -webkit-box-pack: center; -moz-box-pack: center; -ms-flex-pack: center; -webkit-justify-content: center; justify-content: center; margin: 0; height: 100%; width: 100% /* needed for Firefox */
} h1 { display: -webkit-box; display: -moz-box; display: -ms-flexbox; display: -webkit-flex; display: flex; -webkit-box-align: center; -moz-box-align: center; -ms-flex-align: center; -webkit-align-items: center; align-items: center; height: 10rem;

I’ve involved all of the different prefixed editions in the CSS above, from the very earliest, which is still required, to the contemporary and hopefully last format. This might look complicated, but the different syntaxes map pretty well to each other, and I’ve involved platforms at the end of this content to demonstrate the actual mappings.

This is not exactly all of the CSS required for our example, because I’ve removed out the additional design that you probably already know how to use to preserve area.

Let’s look at the CSS that is required to middle the going on the site. First, we set the web-page coding and whole body components to have 100% size and eliminate any edges. This will create the package of our h1 take up the complete size of the browser’s screen. Chrome also needs a size specified on the whole body to power it something. Now, we just need to middle everything.

Allowing Flexbox

Because the body element factor contains the going that we want to middle, we will set its display value to flex:
body { display: flex;

This changes the body factor to use the flexbox structure, rather than the frequent prevent structure. All of its kids in the circulation of the papers (i.e. not definitely placed elements) will now become flex products.
The syntax that is used in IE 10 is : display: -ms-flexbox, and in the older Firefox and Webkit browser it is: display: -prefix-box [where prefix is moz or webkit]. You can see the platforms at the end of this content to see the mappings of the various editions.
What do we obtain now that our components have been to yoga exercises category and become all flexible? They obtain unknown powers: they can bend their dimension and place comparative to the available space; they can be set out either flat in a trench or vertically; and they can even accomplish source-order freedom. (Two sacred grails in one specification? We’re doing well.)

Focusing Horizontally

Next, we want to flat in a trench middle our h1 factor. No big cope, you might say; but it is somewhat simpler than enjoying around with automatic edges. We just need to tell the flexbox to middle its bend products. By standard, bend products are set out flat in a trench, so establishing the justify-content property will align the elements in the main axis.
body { display: flex; justify-content: center;

For IE 10 the property is called flex-pack, and for older browser it will be box-pack [again, with the necessary prefixes] The other possible principles are flex-start, flex-end, space-between and space-around. These are begin, end, rationalize and spread, respectively, in IE 10 and the old requirements (distribute is, however, not reinforced in the old specification). The flex-start value adjusts to the remaining (or to the right with right-to-left text), flex-end adjusts to the right, space-between equally markets the components along the axis, and space-around equally markets along the axis, with half-sized areas at the begin and end of the range.

To clearly set the axis that the factor is arranged along, you can do this with the flex-flow residence. The standard is row, which will provide us the same outcome that we’ve just obtained. To arrange along the straight axis, we can use flex-flow: range. If we add this to our example, you will see that the factor is top to bottom based but drops the horizontally focusing. Treating the transaction by appending -reverse to the row or range principles is also possible (flex-flow: row-reverse or flex-flow: column-reverse), but that will not do much in our example because we have only one product.

There are some variations here in the various editions of the requirements, which are outlined at the end of this content. Another warning to remember is that flex-flow guidelines are writing-mode delicate. That is, when using writing-mode: vertical-rl to change to straight written text structure (as used typically in Chinese suppliers, Asia and Korea), flex-flow: row will arrange the products top to bottom, and range will arrange them flat in a trench.

Centering Vertically

Centering top to bottom is as simple as focusing flat in a trench. We just need to use the appropriate residence to arrange along the “cross-axis.” The what? The cross-axis is generally the axis verticle with respect to the primary one. So, if bend products are arranged flat in a trench, then the cross-axis would be straight, and viceversa. We set this with the align-items residence (flex-align in IE 10, and box-align for mature browsers):

body { /* Remember to use the other versions for IE 10 and older browsers! */ display: flex; justify-content: center; align-items: center;

This is all there is to focusing components with flexbox! We can also use the flex-start (start) and flex-end (end) principles, as well as guideline and expand. Let us have another look at the completed example:


Simple straight and horizontally focusing using flexbox. Bigger view.

You might observe that the writing is also center-aligned top to bottom within the h1 factor. This could have been done with edges or a range size, but we used flexbox again to show that it works with confidential containers (in this case, the range of written text within the h1 element). No matter how high the h1 factor gets, the writing will always be in the center:
 h1 { /* Remember to use the other versions for IE 10 and older browsers! */ display: flex; align-items: center; height: 10rem;

Versatile Sizes

If focusing components was all flexbox could do, it’d be really awesome. But there is more. Let us see how bend products can increase and agreement to fit the available area within a flexbox factor. Point your web browser to this next example.
An innovative slideshow using flexbox:

The HTML and CSS for this example are just like the past someone’s. We’re allowing flexbox and focusing the components on the site in the same way. Moreover, we want to make the headline (inside the headlines element) consistency in dimension, while the five containers (the area elements) modify in dimension to complete the size of the screen. To do this, we use the new bend property:
section { /* removed other styles to save space */ -prefix-box-flex: 1; /* old spec webkit, moz */ flex: 1; height: 250px;

What we’ve just done here is to create each area factor take up 1 bend device. Because we have not set any precise dimension, each of the five containers will be the same dimension. The headlines factor will take up a set dimension (277 pixels) because it is not versatile. We split the staying dimension within the body factor by 5 to determine the dimension each of the area components. Now, if we re-size the web browser screen, the area components will develop or reduce.
In this example, we’ve set a regular dimension, but this could be set to be versatile, too, in exactly the same way. We probably would not always want all components to be the same dimension, so let us create one larger. On float, we’ve set the factor to take up 2 bend units:
section:hover { -prefix-box-flex: 2; flex: 2; cursor: pointer;

Now the available area is separated by 6 rather than 5, and the hovered factor gets twice the platform amount. Observe that a component with 2 bend models does not actually become twice as extensive as one with 1 device. It just gets twice the discuss of the available area included to its “preferred size.” In our illustrations, the “preferred width” is 0 (the default).

Source-Order Independence

For our last celebration technique, we’ll research how to accomplish source-order freedom in our templates. When simply simply clicking a box, we will tell that factor to shift to the remaining of all the other containers, straight after the headline. All we have to do is set the transaction with the transaction residence. By standard, all bend products are in the 0 place. Because they are in the same place, they adhere to the resource purchase.


Well, that is a (flex-)wrap. Here, I’ve presented some of the variety of opportunities provided by flexbox. Be it source-order freedom, versatile measurement or just the modest focusing of components, I’m sure you can find methods to implement flexbox in your sites and programs. The format has resolved down (finally!), and implementations are here. All significant internet explorer now assistance flexbox in at least their newest editions.

While some internet explorer use an mature format, Firefox looks like it is near to upgrading, and IE 11 uses the newest edition in released Windows Red creates. There is currently no term on Opera, but it is a no-brainer considering that Firefox had the newest format before the Blink-WebKit divided. For the time being, use the platforms above to map the various syntaxes, and get your bend on.

Layout in CSS is only getting more highly effective, and flexbox is one of the first actions out of the problem we’ve discovered ourselves in over the decades, first with table-based templates, then float-based templates. IE 10 already facilitates an beginning set up of the Lines structure requirements, which is great for web page structure, and Areas and Exceptions will change how we manage material circulation and structure.

Flexbox can be used these days if you only need to back up relatively contemporary internet explorer or can offer a fallback, and in the foreseeable upcoming, all kinds of choices will be available, so that we can use the best device for the job. Flexbox is forming up to be an excellent excellent device.