Patrick
Patrick

Reputation: 1275

resize buttons responsively in bootstrap

I'm using bootstrap, and the CSS is responsive, so it scales down nicely on smaller devices. However, I would like buttons to shrink too when the viewport is smaller (eg. on mobile).

<button name="button" type="submit" id="edit_button-46" class="btn btn-default" value="edit" >Edit</button>

Basically on smaller devices, the class btn-xs should be added to all buttons.

I can probably accomplish this via Jquery, but was wondering if bootstrap already had this functionality?

Upvotes: 25

Views: 70027

Answers (3)

Gene Parcellano
Gene Parcellano

Reputation: 6044

Just wanted to add another alternative. If you're using Bootstrap's LESS you can also use the mixins.

If you look inside Bootstrap's LESS folder (bootstrap > less > mixins) you will see the buttons.less file. If you open that up you will find the .button-size() mixin.


Here is the mixin:

.button-size(
    vertical padding;
    horizontal padding;
    font size;
    line height;
    border-radius
);

Here's how you dynamically create a button:

You will need to pass certain parameters. It will brake if one is missing

.button-size(10px; 10px; 1em; 1.5em; 0);

Here's an example using the existing Bootstrap LESS variables:

    .btn {
        @media (min-width: @screen-sm-min) {
            .button-size(
                @padding-small-vertical;
                @padding-small-horizontal;
                @font-size-small;
                @line-height-base;
                @border-radius-small
            );
        }
        @media (min-width: @screen-md-min) {
            .button-size(
                @padding-large-vertical;
                @padding-large-horizontal;
                @font-size-large;
                @line-height-base;
                @border-radius-large
            );
        }
    }

Upvotes: 3

user2912589
user2912589

Reputation:

Expanding ZimSystem answer..

@media (max-width: 768px) {
  .btn {
    font-size:11px;
    padding:4px 6px;
  }
}

@media (min-width: 768px) {
  .btn {
    font-size:12px;
    padding:6px 12px;
  }
}

@media (min-width: 992px) {
  .btn {
    font-size:14px;
    padding:8px 12px;
  }
}

@media (min-width: 1200px) {
  .btn {
    padding:10px 16px;
    font-size:18px;
  }
}

Upvotes: 6

Carol Skelly
Carol Skelly

Reputation: 362380

You could use CSS @media queries to scale the buttons accordingly..

@media (max-width: 768px) {
  .btn-responsive {
    padding:2px 4px;
    font-size:80%;
    line-height: 1;
  }
}

@media (min-width: 769px) and (max-width: 992px) {
  .btn-responsive {
    padding:4px 9px;
    font-size:90%;
    line-height: 1.2;
  }
}

Demo: http://bootply.com/93706

Update for Bootstrap 4:
Bootstrap 4 responsive button size

Upvotes: 39

Related Questions