Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing col with d-flex in Bootstrap 4

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:

  1. Horizontal and vertical elements - d-flex supports creating elements both horizontally (.flex-row) and vertically (.flex-column). Col only support horizontally.

  2. 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.

  3. 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.

like image 615
Vincent Chua Avatar asked Aug 11 '17 09:08

Vincent Chua


People also ask

What does D-Flex do in Bootstrap?

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.

Can I use flexbox instead of Bootstrap?

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.

How do I change the order of columns 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...


1 Answers

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:

  1. 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.

  2. Inline element

    The column width is not necessarily fixed:

    • you can use .col-auto to set width: auto on your column
    • you can use the unit-less .col class to make the column use the available space
    • you can align them with all the flex utilities Bootstrap provides
    • you can mix column sizes (some fixed and some dynamic): e.g. .col-md-auto followed by .col.col-lg-2
  3. Advance re-ordering

    • .col-push/pull-{breakpoint} has been replaced by .order-{breakpoint}.
    • you can use the margin utilities (e.g. m-{breakpoint}-auto) to replace .col-offset-{breakpoint}
like image 164
Toni Fisler Avatar answered Oct 10 '22 15:10

Toni Fisler