Setting the width of a dropdown list in Bootstrap 3.0

后端 未结 4 824
执笔经年
执笔经年 2020-12-05 07:04

How can I set the width of a dropdown triggering button in bootstrap 3.0 to be equal to the width of the dropdown list? Similar to what is achieved when using bootstrap-sele

相关标签:
4条回答
  • 2020-12-05 07:23

    If I understand correctly you want to style the menu width according to the larger option you have to override the min-width property of the .dropdown-menu list like:

    .dropdown-menu {
        min-width: 0px !important;
    }
    

    Demo: http://jsfiddle.net/faxyz/4/

    0 讨论(0)
  • 2020-12-05 07:29

    You can set the width of .dropdown-toggle. I recommend to use text-overflow ellipsis when the dropdown becomes too small. It's not necessary to style .dropdown-menu. You can set the width of the dropdown to any other value if you want to use it in .input-group or not in the grid.

    .dropdown-toggle {   
        overflow: hidden;
        padding-right: 24px /* Optional for caret */;
        text-align: left;
        text-overflow: ellipsis;    
        width: 100%;
    }
    
    /* Optional for caret */
    .dropdown-toggle .caret {
        position: absolute;
        right: 12px;
        top: calc(50% - 2px);
    }
    
    0 讨论(0)
  • 2020-12-05 07:30

    I found the solution by setting the dropdown-menu and button width=100%.

    .dropdown-menu {
      width: 100%; 
    }
    
    .btn{
     width: 100%;
    }
    

    Now both the button and dropdown list have the same width, controlled by the column width.

    0 讨论(0)
  • 2020-12-05 07:36

    I came here looking for a solution to a similar problem, although in my case the dropdown wasn't within a col div. I wanted the dropdown button to match the width of the dropdown list, dependant on the content width of whichever was wider, similar to a select dropdown. If anyone is looking for a similar solution here it is:

    .dropdown-container {
      float:right;
      height:60px;
    }
    .position-dropdown-controller {
      position: absolute;
      right:0;
    }
    
    .dropdown-toggle,
    .dropdown-menu {
      width: 100%;
      min-width: 0;
    }
    
    .dropdown-toggle {
      text-align: right;
    }
    
    .dropdown-menu {
      height: 0;
      text-align: center;
      display: block !important;
      visibility: hidden;
      position: relative;
      float: none;
    }
    
    .open .dropdown-menu {
      display: block !important;
      visibility: visible;
      height: auto;
    }
    
    <div class="dropdown-container">
      <div class="position-dropdown-controller">
        <div class="dropdown">
          <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">Dropdown<span class="caret"></span></button>
          <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
            ...
          </ul>
        </div>
      </div>
    </div>
    

    In this example I wanted the dropdown floated to the right, but it can just as easily be tweaked to work however you like.

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