Ilia Shakitko
Ilia Shakitko

Reputation: 1457

Bootstrap 3 "btn-group" non-responsive behavior on small screens

Is there a bootstrap 3 way to handle small screen sizes for "btn-group"?

Button group is placed in <td> and wrapped in <div class="btn-group">:

btn-group

Looks okay, until you re-size it to <768px. Then you have:

enter image description here

How to make them persistent? I tried to add class btn-group-justified. But it gives as a result full width buttons and looks even worse on re-size to small screen size.

P.S> I have an idea, how to implement it adding full set of custom classes. My question is about bootstrap3 way. May be I missed something.

Upvotes: 27

Views: 32395

Answers (9)

Alex
Alex

Reputation: 11

This help for me. It doesn't make the buttons vertical, but it doesn't compress them either

<div class="btn-group flex-wrap" data-toggle="buttons">

Upvotes: 1

Stosh15
Stosh15

Reputation: 211

<div id="secondNav" class="btn-group" role="group">
    <button class='btn btn-default'>View</button>
    <button class='btn btn-default'>Delete</button>
</div>

You can accomplish this with some simple jQuery

<script>
$(window).resize(function() {
    justifyBtnGroup('secondNav');
});

function justifyBtnGroup(id) {
    var btnGroup = $('#' + id);
    if($(window).width() > 768) {
        btnGroup.addClass('btn-group').removeClass('btn-group-vertical');
    } else {
        btnGroup.removeClass('btn-group').addClass('btn-group-vertical');
    }
}
justifyBtnGroup('secondNav'); // will run when page loads
</script>

Upvotes: 2

Jose Castellanos
Jose Castellanos

Reputation: 548

Here's an alternative to @fracz's answer you can try that won't duplicate HTML content. Just copied the css from btn-vertical-group and btn-group and used a media query.

All you have to do is add btn-toolbar-responsive to the toolbar div's classes.

It's in scss for simplicity although you can convert it online easily. Here is the JS Bin: demo

SCSS:

.btn-toolbar.btn-toolbar-responsive{
  text-align: center;
  margin: 0;
  .btn-group{
    float: none;
  }

  @media (max-width: 767px){
    .btn + .btn,
    .btn + .btn-group,
    .btn-group + .btn,
    .btn-group + .btn-group {
      margin-top: -1px;
      margin-left: 0;
    }


    .btn-group{
      position: relative;
      display: block;
      vertical-align: middle;
      margin: 0;
      .btn {
        display: block;
        float: none;
        max-width: 100%;

      }
      .btn:not(:first-child):not(:last-child) {
        border-radius: 0;
      }
      .btn:first-child:not(:last-child) {
        border-top-right-radius: 4px;
        border-top-left-radius: 4px;
        border-bottom-right-radius: 0;
        border-bottom-left-radius: 0;
      }
      .btn:last-child:not(:first-child) {
        border-top-right-radius: 0;
        border-top-left-radius: 0;
        border-bottom-right-radius: 4px;
        border-bottom-left-radius: 4px;
      }

    }
    .btn-group:not(:first-child):not(:last-child) {
      .btn {
        border-radius: 0;
      }
    }

    .btn-group:first-child:not(:last-child) {
      .btn:last-child, .dropdown-toggle {
        border-bottom-right-radius: 0;
        border-bottom-left-radius: 0;
      }
    }

    .btn-group:last-child:not(:first-child) {
      .btn:first-child {
        border-top-right-radius: 0;
        border-top-left-radius: 0;
      }
    }
  }

}

Upvotes: 3

Jeen Jay
Jeen Jay

Reputation: 426

I want to offer you a version with icons from FontAwesome. With a minimum screen resolution text hide leaving only icons.

Sorry for my english.

<div class="btn-group">
    <button class="btn btn-default" title="View"><i class="fa fa-eye"></i><span class="hidden-xs"> View</span></button>
    <button class="btn btn-default" title="Delete"><i class="fa fa-times"></i><span class="hidden-xs"> Delete</span></button>
</div>  

UPDATE by Vishal Kumar: add GLYPHICONS preview

Check this fiddle: http://jsfiddle.net/Jeen/w33GD/4/

Upvotes: 17

fracz
fracz

Reputation: 21249

You can create two button groups with the same buttons and make one of them btn-group-vertical. Then after applying the hidden-xs and visible-xs to them you can hide and show vertical group on appropriate screen size.

<div class="btn-group hidden-xs">
    <button class="btn btn-default">View</button>
    <button class="btn btn-default">Delete</button>
</div>
<div class="btn-group-vertical visible-xs">
    <button class="btn btn-default">View</button>
    <button class="btn btn-default">Delete</button>
</div>

Unfortunately, this requries repeating the markup of the buttons but it should not be an issue if you use any templating tool (define the markup once and include it twice).

Wide screen:

Wide screen buttons

Narrow screen:

Narrow screen buttons

See the JSFiddle.

Upvotes: 26

Leon Matz
Leon Matz

Reputation: 21

try to set min-width on <td>

<td style="min-width: 90px">
    <div class="btn-group">
        <button class=" btn btn-default btn-circle" type="button">
            <i class="fa fa-check"></i>
        </button>
        <button class=" btn btn-default btn-circle" type="button">
            <i class="fa fa-question"></i>
        </button>
        <button class=" btn btn-default btn-circle" type="button">
            <i class="fa fa-times"></i>
        </button>
    </div>
</td>

Upvotes: 2

Kevin Nelson
Kevin Nelson

Reputation: 7663

Okay, so this worked in a test page I made:

<div class='btn-group'>
    <button class='btn btn-default col-xs-6'>View</button>
    <button class='btn btn-default col-xs-6'>Delete</button>
</div>

Forcing each button to be 50% using col-xs-6 kept it from wrapping in my own test page modeled after your example. However, if you have a wider table than the example and you squish down to 320px, the text will overflow the buttons and it looks even worse than your bad example.

You may already know this and it may not be practical for your situation, so I apologize if I'm just presenting unhelpful examples. However, if your table is much wider than what you posted as an example, I would suggest making your rows using the BS grid instead of a table. What this allows you to do is make a single row become two rows when the page shrinks, e.g.

<div class='row'>
    <div class='col-xs-12 col-sm-6'>Some additional details</div>
    <div class='col-xs-6 col-sm-3'>Date</div>
    <div class='col-xs-6 col-sm-3'>
        <div class='btn-group'>
            <button class='btn btn-default col-xs-6'>View</button>
            <button class='btn btn-default col-xs-6'>Delete</button>
        </div>
    </div>
</div>

then, just find a way to alternate colors, add borders, or whatever you need to show the separation between the multiple row rows.

In the BootPly that I just made, as I said, the buttons start to overlap at very small sizes, but they don't wrap when inside a <td> in my browser tests:

http://www.bootply.com/117330

Upvotes: 2

VostanAzatyan
VostanAzatyan

Reputation: 647

<div class="btn-group btn-group-lg">...</div>
<div class="btn-group">...</div>
<div class="btn-group btn-group-sm">...</div>
<div class="btn-group btn-group-xs">...</div>

you can add the class (btn-group-xs,btn-group-sm) in the media < 720px

hope it will help you ;)

Upvotes: -8

PrivateAgent
PrivateAgent

Reputation: 17

No, If you open page in small screen, bootstrap wrap you buttons.

Upvotes: -2

Related Questions