代码之家  ›  专栏  ›  技术社区  ›  RGO

CSS-我对Bootstrap select jQuery插件的宽度和对齐的问题

  •  1
  • RGO  · 技术社区  · 9 年前

    这可能是一个微不足道的练习,但我似乎无法处理。

    我使用Twitter Bootstrap 3.3.6和jQuery 1.12.0,还使用“Bootstrap select”jQuery plugin :

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    
      <!-- Latest compiled and minified CSS -->
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css">
    
      <!-- Latest compiled and minified JavaScript -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>
    </head>
    
    <body>
      <br>
      <div>
        <div class="panel panel-primary">
          <div class="panel-heading ">
            <center>
              <select class="selectpicker" id="headingSelect">
                <option>Item 1</option>
                <option selected>Item 2</option>
                <option>Item 3</option>
              </select>
            </center>
          </div>
          <div class="panel-body">
            <center>
              <div class="form-inline">
                <div class="form-group">
                  <label class="control-label" for="age">Age:</label>
                  <input type="text" class="form-control" id="age" style="width:45px;">
                </div>
                <div class="form-group">
                  <label class="control-label" for="Month">Month:</label>
                  <select id="Month" class="selectpicker">
                    <option>Month1</option>
                    <option selected>Month2</option>
                    <option>Month3</option>
                  </select>
                </div>
                <div class="form-group">
                  <label for="Year">Year:</label>
                  <input type="text" class="form-control" id="Year" style="width:60px;">
                </div>
              </div>
            </center>
          </div>
        </div>
      </div>
      </div>
    </body>
    
    </html>
    

    我有两个问题:

    1. 我希望选择选项居中对齐,但我做不到!

    2. 我也不能减少“月”输入的大小!

    请帮忙!谢谢

    1 回复  |  直到 9 年前
        1
  •  4
  •   ryantdecker    9 年前

    引导选择的CSS看起来相当繁重,因此您必须使用相当高的特定性进行覆盖…因此,最简单的方法是向包含月份选择的div添加一个ID,以便您可以专门针对它。(假设您不希望项目的大小选择与月div上的自定义大小相同)。

    如果您擅长添加ID,以下是您需要的附加CSS:

    .dropdown-menu>li>a {
      text-align:center;
    }
    #month-select .bootstrap-select.btn-group {width:auto;}
    
    #month-select.bootstrap-select.btn-group .dropdown-toggle,
    #month-select.bootstrap-select.btn-group .dropdown-menu {
      max-width:100px; /* or whatever width you want the select and dropdown to be */
      min-width:100px; /* or whatever width you want the select and dropdown to be */
    }    
    
    /* if you want to center the label in the dropdown button, not just the options
     in the list, uncomment this:
    
    .bootstrap-select.btn-group .dropdown-toggle .filter-option {
      text-align:center;
      padding-left:12px
    }
    */ 
    

    代码笔: http://codepen.io/anon/pen/KzOpGJ