bootstrap-selectpicker not changing box width

2019-07-16 11:30发布

问题:

I am trying to change the width of the selectpicker box that shows selected options, but no matter what I try I am unable to change it, nor am I able to change the color of the box.

Any ideas or suggestions are appreciated.

Code Snippet for what I have assuming should change the box width is as follows (as you can see I set it all the way down to 25 but the width is still quite wide):

.selectpicker {
width: 25px;

Code snippet you can run:

<script>
  $(function() {
    $('[data-toggle="tooltip"]').tooltip()
  }) 
</script>

<script type = "text/javascript">
  $(window).on('load', function() {
      $('.selectpicker').selectpicker({
      'selectedText': 'cat'
      });
      // $('.selectpicker').selectpicker('hide');  
  }); 
</script>
.text-muted {
  font-size: small;
}

.text-bold {
  font-weight: bold;
  font-size: small;
}

.button {
  background-color: #008CBA;
  /* Light Blue */
  border: none;
  color: white;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 11px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 4px;
  padding: 6px 10px;
}

.button2 {
  background-color: #008CBA;
  /* Light Blue */
  border: none;
  color: white;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 11px;
  margin: 8px 2px;
  cursor: pointer;
  border-radius: 4px;
  padding: 6px 10px;
}

.button2:hover {
  background-color: #008CBA;
  box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 30px 0 rgba(0, 0, 0, 0.19);
  padding: 6px 10px;
}


/* Blue */

.button3 {
  background-color: #f44336;
}


/* Red */

.button4 {
  background-color: #e7e7e7;
  color: black;
}


/* Gray */

.button5 {
  background-color: #555555;
}


/* Black */

.button6 {
  font-size: 12px;
  padding: 8px 8px;
  margin: 8px 0;
  box-sizing: border-box;
  border: none;
  background-color: #D35324;
  color: white;
  padding: 6px 10px;
}

.button6:hover {
  background-color: #037BA2;
  color: white;
  box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 30px 0 rgba(0, 0, 0, 0.19);
}

.button7 {
  font-size: 12px;
  padding: 8px 8px;
  margin: 8px 0;
  box-sizing: border-box;
  border: none;
  background-color: #008CBA;
  color: white;
}

.button7:hover {
  background-color: #008CBA;
  color: white;
  box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 30px 0 rgba(0, 0, 0, 0.19);
}

td {
  text-align: left;
  valign="middle";
}

i {
  border: solid white;
  border-width: 0 1px 1px 0;
  display: inline-block;
  padding: 2px;
}

.right {
  transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
}

.left {
  transform: rotate(135deg);
  -webkit-transform: rotate(135deg);
}

.up {
  transform: rotate(-135deg);
  -webkit-transform: rotate(-135deg);
}

.down {
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
}

.dropbtn {
  background-color: #008CBA;
  color: white;
  padding: 12px;
  font-size: 12px;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 120px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  color: #037BA2;
  padding: 6px 6px;
  text-align: center;
  text-decoration: none;
  display: block;
}

.dropdown-content a:hover {
  background-color: #037BA2;
  color: white;
}

.dropdown:hover .dropdown-content {
  display: block;
  padding-top: 3px;
  right: 0;
}

.dropdown:hover .dropbtn {
  background-color: #037BA2;
}

input[id=Search],
select {
  padding: 8px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

input[value=Apply] {
  font-size: 12px;
  padding: 8px 8px;
  margin: 8px 0;
  box-sizing: border-box;
  border: none;
  background-color: #D35324;
  color: white;
}

input[value=Reset] {
  font-size: 12px;
  padding: 8px 8px;
  margin: 8px 0;
  box-sizing: border-box;
  border: none;
  background-color: #D35324;
  color: white;
}

input[value=Apply]:hover {
  background-color: #C44E22;
}

input[value=Reset]:hover {
  background-color: #C44E22;
}

table.table1,
th.th1,
td.td1 {
  width: 100%;
  color: white;
  font-size: 16px;
  padding-left: 10px;
  background-color: #008CBA;
}

.tooltip {
  display: inline-block;
  border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 220px;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
  top: 150%;
  left: 50%;
  margin-left: -60px;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent transparent black transparent;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
}

.glyphicon.glyphicon-info-sign {
  font-size: 25px;
  top: 5px;
}

.selectpicker {
  width: 25px;
  height: 37px;
  background-color: #008CBA !important;
  border-style: solid;
  border-left-width: 3px;
  border-top: none;
  border-bottom: none;
  border-right: none;
  color: black;
  font-size: 14px;
  font-weight: 200;
  padding-left: 6px;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}
<head>


  <!-- Bootstrap core CSS -->
  <link href="dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>


  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>




  <!-- JQUERY -->
  <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>


  <!--  BOOTSTRAP SELECT -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.css" />




  <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
  <link href="assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

  <!-- Custom styles for this template -->
  <link href="dashboard.css" rel="stylesheet">

  <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
  <!--[if lt IE 9]><script src="assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
  <script src="assets/js/ie-emulation-modes-warning.js"></script>

  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->



</head>

<body>
  <h3>START OF TESTING</h3>
  <p><br><br></p>
  <select class="selectpicker" data-style="btn-info" multiple data-max-options="4" data-actions-box="true">

    <optgroup label="Select options below">
      <option>Active</option>
      <option>Inactive</option>
      <option>Leave</option>
      <option>No Status</option>
    </optgroup>
  </select>

  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>
  <h3>END OF TESTING</h3>
  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>






</body>

回答1:

Use data-content attribute in select option. For example:

<select class="selectpicker form-control" data-live-search="true" id="subject_teacher_drop_down">
    <option data-content="English">English</option>
    <option data-content="Methodology of social...">Methodology of social science with special reference to economics</option>
</select>


回答2:

Your code snippet example in question doesn't work at all. I did some cleanup and prepared an another one below, and also JSFiddle.

  1. To change width you should adjust .bootstrap-select class:
.bootstrap-select {
  width: 330px !important;
}
  1. It is not so easy to change the button's color properly. Button has different states, all programmed in bootstrap.css. I have take all entries for the btn-info class, renamed them to btn-custom and changed colors for the .btn-custom CSS entry only:
.btn-custom {
  color: #fff;
  background-color: #008CBA;
  border-color: #008CBA;
}

You should review all other btn-custom colors and adjust accordingly.

  1. Use your select as the following:
<select class="selectpicker" data-style="btn-custom" multiple data-max-options="4" data-actions-box="true">

The whole working example:

$(function() {
    $('[data-toggle="tooltip"]').tooltip()
  });

  $('.selectpicker').selectpicker({
    'selectedText': 'cat'
  });

  // $('.selectpicker').selectpicker('hide');
.text-muted {
  font-size: small;
}

.text-bold {
  font-weight: bold;
  font-size: small;
}

.button {
  background-color: #008CBA;
  /* Light Blue */
  border: none;
  color: white;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 11px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 4px;
  padding: 6px 10px;
}

.button2 {
  background-color: #008CBA;
  /* Light Blue */
  border: none;
  color: white;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 11px;
  margin: 8px 2px;
  cursor: pointer;
  border-radius: 4px;
  padding: 6px 10px;
}

.button2:hover {
  background-color: #008CBA;
  box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 30px 0 rgba(0, 0, 0, 0.19);
  padding: 6px 10px;
}


/* Blue */

.button3 {
  background-color: #f44336;
}


/* Red */

.button4 {
  background-color: #e7e7e7;
  color: black;
}


/* Gray */

.button5 {
  background-color: #555555;
}


/* Black */

.button6 {
  font-size: 12px;
  padding: 8px 8px;
  margin: 8px 0;
  box-sizing: border-box;
  border: none;
  background-color: #D35324;
  color: white;
  padding: 6px 10px;
}

.button6:hover {
  background-color: #037BA2;
  color: white;
  box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 30px 0 rgba(0, 0, 0, 0.19);
}

.button7 {
  font-size: 12px;
  padding: 8px 8px;
  margin: 8px 0;
  box-sizing: border-box;
  border: none;
  background-color: #008CBA;
  color: white;
}

.button7:hover {
  background-color: #008CBA;
  color: white;
  box-shadow: 0 12px 16px 0 rgba(0, 0, 0, 0.24), 0 17px 30px 0 rgba(0, 0, 0, 0.19);
}

td {
  text-align: left;
  valign="middle";
}

i {
  border: solid white;
  border-width: 0 1px 1px 0;
  display: inline-block;
  padding: 2px;
}

.right {
  transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
}

.left {
  transform: rotate(135deg);
  -webkit-transform: rotate(135deg);
}

.up {
  transform: rotate(-135deg);
  -webkit-transform: rotate(-135deg);
}

.down {
  transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
}

.dropbtn {
  background-color: #008CBA;
  color: white;
  padding: 12px;
  font-size: 12px;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 120px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  color: #037BA2;
  padding: 6px 6px;
  text-align: center;
  text-decoration: none;
  display: block;
}

.dropdown-content a:hover {
  background-color: #037BA2;
  color: white;
}

.dropdown:hover .dropdown-content {
  display: block;
  padding-top: 3px;
  right: 0;
}

.dropdown:hover .dropbtn {
  background-color: #037BA2;
}

input[id=Search],
select {
  padding: 8px 20px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
}

input[value=Apply] {
  font-size: 12px;
  padding: 8px 8px;
  margin: 8px 0;
  box-sizing: border-box;
  border: none;
  background-color: #D35324;
  color: white;
}

input[value=Reset] {
  font-size: 12px;
  padding: 8px 8px;
  margin: 8px 0;
  box-sizing: border-box;
  border: none;
  background-color: #D35324;
  color: white;
}

input[value=Apply]:hover {
  background-color: #C44E22;
}

input[value=Reset]:hover {
  background-color: #C44E22;
}

table.table1,
th.th1,
td.td1 {
  width: 100%;
  color: white;
  font-size: 16px;
  padding-left: 10px;
  background-color: #008CBA;
}

.tooltip {
  display: inline-block;
  border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 220px;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
  top: 150%;
  left: 50%;
  margin-left: -60px;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  bottom: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent transparent black transparent;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
}

.glyphicon.glyphicon-info-sign {
  font-size: 25px;
  top: 5px;
}

.selectpicker {
  width: 25px;
  height: 37px;
  background-color: #008CBA !important;
  border-style: solid;
  border-left-width: 3px;
  border-top: none;
  border-bottom: none;
  border-right: none;
  color: black;
  font-size: 14px;
  font-weight: 200;
  padding-left: 6px;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

.bootstrap-select {
  width: 330px !important;
}

.btn-custom {
  color: #fff;
  background-color: #008CBA;
  border-color: #008CBA;
}

.btn-custom:hover {
  color: #fff;
  background-color: #138496;
  border-color: #117a8b;
}

.btn-custom:focus, .btn-custom.focus {
  box-shadow: 0 0 0 0.2rem rgba(23, 162, 184, 0.5);
}

.btn-custom.disabled, .btn-custom:disabled {
  color: #fff;
  background-color: #17a2b8;
  border-color: #17a2b8;
}

.btn-custom:not(:disabled):not(.disabled):active, .btn-custom:not(:disabled):not(.disabled).active,
.show > .btn-custom.dropdown-toggle {
  color: #fff;
  background-color: #117a8b;
  border-color: #10707f;
}

.btn-custom:not(:disabled):not(.disabled):active:focus, .btn-custom:not(:disabled):not(.disabled).active:focus,
.show > .btn-custom.dropdown-toggle:focus {
  box-shadow: 0 0 0 0.2rem rgba(23, 162, 184, 0.5);
}

.bootstrap-select > .dropdown-toggle.bs-placeholder.btn-custom,
.bootstrap-select > .dropdown-toggle.bs-placeholder.btn-custom:hover,
.bootstrap-select > .dropdown-toggle.bs-placeholder.btn-custom:focus,
.bootstrap-select > .dropdown-toggle.bs-placeholder.btn-custom:active {
  color: rgba(255, 255, 255, 0.5);
}
<head>
  <!-- JQUERY -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <script src="https://unpkg.com/popper.js"></script>

  <!-- Bootstrap core CSS -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>

  <!--  BOOTSTRAP SELECT -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/css/bootstrap-select.min.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.1/js/bootstrap-select.min.js"></script>
</head>

<body>
  <h3>START OF TESTING</h3>
  <p><br><br></p>
  <select class="selectpicker" data-style="btn-custom" multiple data-max-options="4" data-actions-box="true">

    <optgroup label="Select options below">
      <option>Active</option>
      <option>Inactive</option>
      <option>Leave</option>
      <option>No Status</option>
    </optgroup>
  </select>

  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>
  <h3>END OF TESTING</h3>
  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>
  <p><br><br></p>
</body>