17

How do I get auto-width-columns in Bootstrap like in UIKit?

I need 5 columns with auto width.

In UIKit you get it with <div class="uk-width-1-5">

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Jack
  • 407
  • 2
  • 6
  • 15

3 Answers3

25

Bootstrap 4+ you can use the col class alone and it will resize to what space is available.

<div class="row">
  <div class="col"></div>
  <div class="col"></div>
  <div class="col"></div>
  <div class="col"></div>
  <div class="col"></div>
</div>
Josh Bradley
  • 1,854
  • 1
  • 15
  • 31
  • 4
    Hi Josh. How can I implement that with bootstrap 3.3.7? – Allan Jebaraj May 19 '18 at 10:35
  • 2
    You can't. Auto width calculation is a feature only available with `flexbox` – Josh Bradley Oct 10 '18 at 20:36
  • 2
    @AllanJebaraj although flexbox is only present at Bootstrap 4+, it doesn't mean you can't. You can study flexbox and apply your own CSS rules to override the columns styles and apply the auto-width. =] – giovannipds Dec 20 '19 at 17:39
  • It's been a year and a half ! :P Thank you @giovannipds I didn't have control over the library as many components and layouts have been defined in that version and I didn't want to complicate the already complicated codebase ;) – Allan Jebaraj Dec 23 '19 at 06:02
  • @AllanJebaraj haha, no worries! If you need it anytime, just say it, it's simples to implement a little css class extension for that. =] – giovannipds Dec 26 '19 at 12:57
22

Using class="col" will automatically make the columns equal in size.

But if you want some column(s) to resize depending on its/there content(s), use something like class="col-auto".

Use class="col-{breakpoint}-auto" classes to size columns based on the natural width of their content.

Reference: https://getbootstrap.com/docs/4.0/layout/grid/#variable-width-content

Muhammad Altabba
  • 2,583
  • 19
  • 31
1

Bootstrap 5 (update 2023)

Bootstrap 5 still uses a flexbox grid so auto-sizing is still possible using the .col to .col-auto classes so that columns take up as much space as needed.

Bootstrap 4

Bootstrap 4 uses a flexbox grid so auto-width columns are achieved using the col and/or col-auto classes. The col class will create equal width columns, while col-auto columns will shrink to fit the width of their content.

Bootstrap 3 (original answer)

The 5 columns aspect of this question has been answered here. Since Bootstrap has 12 columns, you only use 10 of them with 5 col-*-2 units

http://codeply.com/go/QXiPmtgJUJ

In terms of "auto-width", Bootstrap 4 will have new auto-layout columns that consume the remaining portion of any row, but this is still based on a 12 column grid.

Carol Skelly
  • 351,302
  • 90
  • 710
  • 624