jquery - CSS - 我的 Bootstrap 选择 jQuery 插件的宽度和对齐问题

标签 jquery css twitter-bootstrap bootstrap-select

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

我正在使用 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. 我也无法减小“月份”输入的大小!

请帮忙!谢谢!

最佳答案

bootstrap-select 的 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

关于jquery - CSS - 我的 Bootstrap 选择 jQuery 插件的宽度和对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37369616/

相关文章:

javascript获取字符前的字符串

javascript - jQuery 幻灯片 + 单击放大(LightBox 和 ColorBox)

css - 仅适用于 IE10+ 不适用于其他浏览器的 css 样式

css - 如何在 LESS 中设置 Arial Black 字体系列

twitter-bootstrap - Bootstrap 多选的 knockout 数据绑定(bind)不选择初始数据

html - 使分区图像响应

jquery - 开始和停止 div 在另外两个 div 之间滚动

javascript - 无法使用 window.location.href 更改为不同的窗口

javascript - 用JS打开浏览器安全信息?

jquery - 如何使用 Bootstrap 3 在 div 中垂直居中 div