I came up today in a discussion where I'm wondering what is the most performant way of having two div's beside each other.
On one side, i love using display:flex;
, on the other side there is the option to use calc()
, the reason is our div has padding and we need to reduce the width by the padding. Case:
<div class='container'>
<div class='inner'></div>
<div class='inner'></div>
</div>
Both should be on 50% width. The default css is:
* {
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
}
.container {
height: 100%;
width: 100%;
}
.inner {
width: 50%;
padding: 20px;
}
The display:flex;
way would be additional:
.container {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-wrap: nowrap;
align-items: stretch;
align-content: stretch;
}
The calc()
way would be:
.inner {
width: calc(100% - 40px);
display: inline-block;
}
or:
.inner {
width: calc(100% - 40px);
float: left;
}
I do not want any table layout in my css. Additionally, we do not need to take care of the browser versions, this should be only functional in the latest versions, always.
What would be recommended to use? What has more performance?
I already found an article that the performance has been increased already a lot. Link
In general, it should be much faster in the common case, but you can construct a case where it's equally as slow. That said, if you can get away with it, regular block layout (non-float), will usually be as fast or faster than new flexbox since it's always single-pass.
The main difference between display: flex and display: inline-flex is that display: inline-flex will make the flex container an inline element while its content maintains its flexbox properties.
To compare display: table to display: flex I wanted to compare two visually identical layouts. The same kind of treatment that can be solved with either layout method; a flexible row, with defined width sections and vertically centred text. It’s the kind of thing that display: table; has done well for years and Flex layout also excels at:
Flex value applies for the display property of a container which is called flex container as well as the container’s contents or flex child. Flexbox has a list of important properties that are used to make a flexible container.
If an identical visual can be achieved using the same quantity of markup, right now I’d choose display: table over display: flex. Not because it is any faster (although in this limited test scenario it universally is) but because it is more widely and robustly supported and implemented.
Going on the WebPageTest results, Table layout is 28% faster. You can see that in this incredibly limited test layout, Flex layout is slower on every browser tested. Don’t read too much into this though.
I ran a simple test out of curiosity and there don't seem to be any differences in performance between float+calc vs flex, other than IE rendering both much slower than FF and Chrome.
From a related article:
The new flexbox code has a lot fewer multi-pass layout codepaths. You can still hit multi-pass codepaths pretty easily though (e.g. flex-align: stretch is often 2-pass). In general, it should be much faster in the common case, but you can construct a case where it’s equally as slow.
That said, if you can get away with it, regular block layout (non-float), will usually be as fast or faster than new flexbox since it’s always single-pass. But new flexbox should be faster than using tables or writing custom JS-base layout code.
I'm pretty sure that calc()
makes a block layout require multiple passes too :)
LE: There was a bug in Firefox that made reflows very slow when you had 4-5 nested flexboxes, but it was fixed in the latest versions (37+).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With