Since the new Bootstrap 4 is migrating from using 'floating' elements to the better 'flexbox' method, just wondering is it ok to construct the entire grid structure using .d-flex
rather than the existing .container .row .col
way?
Since both of them are fundamentally constructed using flexbox I don't see any downside of replacing one with another. In fact I feel that .d-flex
requires less css class name and make things more readable within the html.
There are other reasons why I prefer d-flex
than the old col
:
Horizontal and vertical elements - d-flex supports creating elements both horizontally (.flex-row) and vertically (.flex-column). Col only support horizontally.
Inline element - The element's width will inherit from the child and can be align dynamically using d-inline-flex. Whereas col grid is fix.
Advance re-ordering - d-flex uses .order-x
and col uses .push
and .pull
. In my opinion, d-flex
is more intuitive, the order of elements are represented through numbering and on the other hand the number in .col
is how many grids are being push and pull away from the previous state. It gets messy when you are building a more complex site....
Please correct me if I'm wrong.
d-flex will result in an element that takes up the whole width of its parent. And creating a Bootstrap 4 flex container with . d-inline-flex will make its width vary depending on the width of its content. . d-inline-flex will only take up the space it is necessary to display its content.
Because the truth is, there isn't a better system – both flexboxes vs Bootstrap are good at different things and should be used together, not as alternatives to one another. Basically, Flexbox is not an alternative to Bootstrap. As a matter of fact, Bootstrap also uses flexbox for its layout in Bootstrap 4.
Since Bootstrap 4 is flexbox, it's easy to change the order of columns. The cols can be ordered from order-1 to order-12 , responsively such as order-md-12 order-2 (last on md , 2nd on xs ) relative to the parent . row . It's also possible to change column order using the flexbox direction utils...
Bootstrap 4 beta just got out and the whole grid system is using flexbox from now on.
You can now achieve a whole grid with only flex and it has reduced the number of classes you need. The main advantage of the Bootstrap Grid System is that you have paddings around your columns and negative margins on the .row
wrapper. That way, when the columns wrap (e.g. on smaller screens), they will stay correctly aligned.
To answer your 3 points:
Horizontal and vertical elements
Just use the regular .row
class to achieve a flexbox grid. You can even add .flex-column
(even with breakpoints, like .flex-{breakpoint}-column
). Know that the .row
class has the flex-wrap: wrap
property.
Inline element
The column width is not necessarily fixed:
.col-auto
to set width: auto
on your column.col
class to make the column use the available space.col-md-auto
followed by .col.col-lg-2
Advance re-ordering
.col-push/pull-{breakpoint}
has been replaced by .order-{breakpoint}
.m-{breakpoint}-auto
) to replace .col-offset-{breakpoint}
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