resize buttons responsively in bootstrap

后端 未结 3 1756
后悔当初
后悔当初 2020-12-10 01:12

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. o

相关标签:
3条回答
  • 2020-12-10 01:46

    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;
      }
    }
    
    0 讨论(0)
  • 2020-12-10 01:48

    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
                );
            }
        }
    
    0 讨论(0)
  • 2020-12-10 01:51

    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

    0 讨论(0)
提交回复
热议问题