Button group not wrapping when too long
Asked Answered
R

5

8

I have a very long button group : https://jsfiddle.net/cyu4bvak/

<div class="btn-group" data-toggle="buttons">
    <label  class="btn btn-primary active">
        <input type="checkbox" />ABCDEFGHIJKLMNOPQRSTUVWXYZ
    </label>
    <label  class="btn btn-primary active">
        <input type="checkbox" />ABCDEFGHIJKLMNOPQRSTUVWXYZ
    </label>
    <label  class="btn btn-primary active">
        <input type="checkbox" />ABCDEFGHIJKLMNOPQRSTUVWXYZ
    </label>
    ...
</div>

Whatever the size of the viewport it is always taking one long line causing scrolling.

Is it possible to make it wrap so that on smaller viewports it will wrap on more lines without causing scrolling?

If no which alternative(s) do I have to obtain the expected behavior?

Ronald answered 13/1, 2017 at 0:5 Comment(0)
R
18

Add flex-wrap: wrap; to the flex parent (.btn-group) if you want the flex children to wrap.

Roberge answered 13/1, 2017 at 0:6 Comment(4)
Simply brilliant! Thanks a lot. :-)Ronald
@Ronald fo sho!Roberge
when one button alone wraps onto the next line it doesn't look great, so I wanted to put the last three buttons in another div, so that a min of 3 buttons wrap. But when I do this, the last div always begins on a new line. Any solution to that?Avogadro
YOU ARE THE MANWanyen
V
3

For React-Bootstrap users:

<ButtonGroup style={{ flexWrap: "wrap" }}>
...
</ButtonGroup>
Virulent answered 16/9, 2020 at 19:17 Comment(0)
L
2

In Bootstrap 5 you can add flex-wrap class to element with btn-group.

Luckey answered 2/12, 2021 at 15:20 Comment(0)
M
2

Using flex-wrap: wrap; on the container is a great option, although sometimes I think it looks strange to have different sized buttons:

stretched rows of buttons

It's a little better if you also set flex: 0 !important; on each button, but the jagged block still isn't visually appealing to me:

jagged rows of buttons

Sometimes, I prefer a vertical column of buttons for screens below a certain width. For example:

@media screen and (max-width: 600px) {
  .btn-group {
    flex-direction: column;
  }
}

With a few more styles, you can also get nicely rounded corners: vertical stack of buttons

Here is the full code:

/* Responsive: Use a vertical column if under 450px wide */
@media screen and (max-width: 450px) {
  .btn-group {
    flex-direction: column;
  }
  .btn-group .btn {
    border-radius: 0 !important;
    margin-left: 0px !important;
    margin-top: -1px !important;
  }
  .btn-group .btn:first-child {
    border-top-left-radius: 4px !important;
    border-top-right-radius: 4px !important;
  }
  .btn-group .btn:last-child {
    border-bottom-left-radius: 4px !important;
    border-bottom-right-radius: 4px !important;
  }
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <h2>Responsive Button Group</h2>
  <div class="btn-group">
    <button type="button" class="btn btn-outline-primary">Samsung</button>
    <button type="button" class="btn btn-outline-primary">Apple</button>
    <button type="button" class="btn btn-outline-primary">Xiaomi</button>
    <button type="button" class="btn btn-outline-primary">Huawei</button>
    <button type="button" class="btn btn-outline-primary">Oppo</button>
    <button type="button" class="btn btn-outline-primary">Vivo</button>
  </div>
</div>

P.S. In case it wasn't clear, both flex-direction: column; and flex-wrap: wrap; work for both btn-group and btn-group-toggle.

Machinegun answered 4/3, 2022 at 20:30 Comment(1)
Thanx for your example. I used the jagged Block but also put a border around it so that it looks better.Spradling
A
1

Adding flex-wrap to the button group will work in boot strap 5, but you should add flex-grow-0 to each button so buttons will not grow to fill the container width

Ashly answered 11/7, 2022 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.