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?

Pragmateek
  • 13,174
  • 9
  • 74
  • 108

5 Answers5

18

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

Michael Coker
  • 52,626
  • 5
  • 64
  • 64
3

For React-Bootstrap users:

<ButtonGroup style={{ flexWrap: "wrap" }}>
...
</ButtonGroup>
sebtheiler
  • 2,159
  • 3
  • 16
  • 29
2

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

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/bootstrap@4.6.1/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.

Matt
  • 659
  • 6
  • 11
0

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