Fixed width buttons with Bootstrap

后端 未结 12 536
臣服心动
臣服心动 2021-01-29 18:36

Does Bootstrap support fixed width buttons? Currently if I have 2 buttons, \"Save\" and \"Download\", the button size changes based on content.

Also what is the right wa

相关标签:
12条回答
  • 2021-01-29 19:10

    btn-group-justified and btn-group only work for static content but not on dynamically created buttons, and fixed with of button in css is not practical as it stay on the same width even all content are short.

    My solution: put the same class to group of buttons then loop to all of them, get the width of the longest button and apply it to all

    var bwidth=0
    $("button.btnGroup").each(function(i,v){
        if($(v).width()>bwidth) bwidth=$(v).width();
    });
    $("button.btnGroup").width(bwidth);
    

    0 讨论(0)
  • 2021-01-29 19:11

    A block width button could easily become responsive button if the parent container is responsive. I think that using a combination of a fixed width and a more detailed selector path instead of !important because:

    1) Its not a hack (setting min-width and max-width the same is however hacky)

    2) Does not use the !important tag which is bad practice

    3) Uses width so will be very readable and anyone who understands how cascading works in CSS will see whats going on (maybe leave a CSS comment for this?)

    4) Combine your selectors that apply to your targeted node for increased accuracy

    .parent_element .btn.btn-primary.save-button {
        width: 80px;
    }
    
    0 讨论(0)
  • 2021-01-29 19:13

    To do this you can come up with a width you feel is ok for both buttons and then create a custom class with the width and add it to your buttons like so:

    CSS

    .custom {
        width: 78px !important;
    }
    

    I can then use this class and add it to the buttons like so:

    <p><button href="#" class="btn btn-primary custom">Save</button></p>
    <p><button href="#" class="btn btn-success custom">Download</button></p>
    

    Demo: http://jsfiddle.net/yNsxU/

    You can take that custom class you create and place it inside your own stylesheet, which you load after the bootstrap stylesheet. We do this because any changes you place inside the bootstrap stylesheet might get accidentally lost when you update the framework, we also want your changes to take precedence over the default values.

    0 讨论(0)
  • 2021-01-29 19:16

    Expanding @kravits88 answer:

    This will stretch the buttons to fit whole width:

    <div className="btn-group-justified">
    <div className="btn-group">
      <button type="button" className="btn btn-primary">SAVE MY DEAR!</button>
    </div>
    <div className="btn-group">
      <button type="button" className="btn btn-default">CANCEL</button>
    </div>
    </div>
    
    0 讨论(0)
  • 2021-01-29 19:21

    With BS 4, you can also use the sizing, and apply w-100 so that the button can occupy the complete width of the parent container.

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <p>
      Using btn-block
    </p>
    <div class="container-fluid">
      <div class="btn-group col" role="group" aria-label="Basic example">
        <button type="button" class="btn btn-outline-secondary">Left</button>
        <button type="button" class="btn btn-outline-secondary btn-block">Middle</button>
        <button type="button" class="btn btn-outline-secondary">Right</button>
      </div>
    </div>
    
    <p>
      Using w-100
    </p>
    <div class="container-fluid">
      <div class="btn-group col" role="group" aria-label="Basic example">
        <button type="button" class="btn btn-outline-secondary">Left</button>
        <button type="button" class="btn btn-outline-secondary w-100">Middle</button>
        <button type="button" class="btn btn-outline-secondary">Right</button>
      </div>
    </div>

    0 讨论(0)
  • 2021-01-29 19:21

    Here I found a solution by comparing buttons in a button-group element. The simple solution is to get the one with the largest width and set the width to the other buttons. So they can have a equal width.

        function EqualizeButtons(parentElementId) {
    
        var longest = 0; 
        var element = $(parentElementId);
    
        element.find(".btn:not(.button-controlled)").each(function () {
            $(this).addClass('button-controlled');
            var width = $(this).width();
            if (longest < width) longest = width;
    
        }).promise().done(function () {
            $('.button-controlled').width(longest);
        });
    }
    

    It worked like a charm.

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