Home > front end >  How can I give Bootstrap select a normal height if there is nothing selected?
How can I give Bootstrap select a normal height if there is nothing selected?

Time:02-02

I am using enter image description here

And this would be the desired result:

enter image description here

I have used this attribute to make the select look empty: data-none-selected-text="" and it actually looks empty but too flat.

If I remove that attribute, I'll get this Nothing selected placeholder which I don't want:

enter image description here

How can I make the design consistent and keep the same size whether something is selected or not?

body {
  background-color: lightblue !important;
}
<head>
  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  <!-- Bootstrap Select CSS -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap-select.min.css">
</head>

<body>
  <div >
    <div >
      <div >
        Project Manager <span style="color:red;">*</span>
      </div>

      <div >
        <select  data-none-selected-text="" multiple data-max-options="2" aria-label="Project Manager" required>
          <option value="Michal Jackson">Michael Jackson</option>
          <option value="Michael Jordan">Michael Jordan</option>
          <option value="Luke Skywalker">Luke Skywalker</option>
          <option value="Rafael Nadal">Rafael Nadal</option>
          <option value="Cristiano Ronaldo">Cristiano Ronaldo</option>
          <option value="Albert Einstein">Albert Einstein</option>
        </select>
      </div>
    </div>
  </div>

  <!-- Popper.js 5.0.2 JS -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.9.2/umd/popper.min.js"></script>
  <!-- Bootstrap 5.0.2 JS -->
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
  <!-- jQuery CDN -->
  <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
  <!-- Bootstrap Select JS BETA VERSION, UPDATE TO A STABLE VERSION COMPATIBLE WITH BS5 ONCE IT IS RELEASED-->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.14.0-beta2/js/bootstrap-select.min.js"></script>
</body>

CodePudding user response:

You can overwrite default bootstrap class and make an exception for this scenario.

.dropdown-toggle {
   height: 50px;
 }

CodePudding user response:

If you really don't want to use placeholder text, just use a blank HTML space (&nbsp;) as the data-none-selected-text or placeholder attribute value. Either approach results in the select box holding a standard height with no visible text in it.

body {
  background-color: lightblue !important;
}
<head>
  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
  <!-- Bootstrap Select CSS -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap-select.min.css">
</head>

<body>
  <div >
    <div >
      <div >
        Project Manager <span style="color:red;">*</span>
      </div>

      <div >
        <select  placeholder="&nbsp;" multiple data-max-options="2" aria-label="Project Manager" required>
          <option value="Michal Jackson">Michael Jackson</option>
          <option value="Michael Jordan">Michael Jordan</option>
          <option value="Luke Skywalker">Luke Skywalker</option>
          <option value="Rafael Nadal">Rafael Nadal</option>
          <option value="Cristiano Ronaldo">Cristiano Ronaldo</option>
          <option value="Albert Einstein">Albert Einstein</option>
        </select>
      </div>
    </div>
  </div>

  <!-- Popper.js 5.0.2 JS -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.9.2/umd/popper.min.js"></script>
  <!-- Bootstrap 5.0.2 JS -->
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
  <!-- jQuery CDN -->
  <script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
  <!-- Bootstrap Select JS BETA VERSION, UPDATE TO A STABLE VERSION COMPATIBLE WITH BS5 ONCE IT IS RELEASED-->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.14.0-beta2/js/bootstrap-select.min.js"></script>
</body>

CodePudding user response:

This is not the best answer, I think. So, if you found that this way is not efficient, do comment!

enter image description here

Still using the data-none-selected-text="" in the select tag. But, we need to add the hidden-placeholder class for it.

Then, we write some javascript code. (Read the comment in the code)

<script>
  const placeholder = document.querySelector('.hidden-placeholder');

  document.addEventListener('DOMContentLoaded', () => {
    const { nextElementSibling, dataset } = placeholder;
    const target = document.querySelector('.filter-option-inner-inner');

    // Set the default color to transparent
    target.style.color = 'transparent';

    placeholder.addEventListener('change', () => {
      target.style.color = 'inherit';

      //   Change the color of the placeholder to normal
      nextElementSibling.title === dataset.noneSelectedText
        ? (target.style.color = 'transparent')
        : (target.style.color = 'inherit');
    });
  });
</script>

Then, you should see the result like this:

enter image description here


Example Result

const placeholder = document.querySelector('.hidden-placeholder');

document.addEventListener('DOMContentLoaded', () => {
  const { nextElementSibling, dataset } = placeholder;
  const target = document.querySelector('.filter-option-inner-inner');

  // Set the default color to transparent
  target.style.color = 'transparent';

  placeholder.addEventListener('change', () => {
    target.style.color = 'inherit';

    //   Change the color of the placeholder to normal
    nextElementSibling.title === dataset.noneSelectedText
      ? (target.style.color = 'transparent')
      : (target.style.color = 'inherit');
  });
});
body {
  background-color: lightblue;
}
<!-- Bootstrap CSS -->
<link
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
  rel="stylesheet"
  integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
  crossorigin="anonymous"
/>
<!-- Bootstrap Select CSS -->
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap-select.min.css"
/>

<!-- Popper.js 5.0.2 JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.9.2/umd/popper.min.js"></script>
<!-- Bootstrap 5.0.2 JS -->
<script
  src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
  integrity="sha384-MrcW6ZMFYlzcLA8Nl NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP JcXn/tWtIaxVXM"
  crossorigin="anonymous"
></script>
<!-- jQuery CDN -->
<script
  src="https://code.jquery.com/jquery-3.6.0.js"
  integrity="sha256-H K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
  crossorigin="anonymous"
></script>
<!-- Bootstrap Select JS BETA VERSION, UPDATE TO A STABLE VERSION COMPATIBLE WITH BS5 ONCE IT IS RELEASED-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.14.0-beta2/js/bootstrap-select.min.js"></script>

<head>
  <style>
    body {
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div >
    <div >Project Manager <span style="color: red">*</span></div>
    <div >
      <select
        
        data-none-selected-text="This item shouldn't be displayed!"
        multiple
        data-max-options="2"
        aria-label="Project Manager"
        required
      >
        <option value="Michal Jackson">Michael Jackson</option>
        <option value="Michael Jordan">Michael Jordan</option>
        <option value="Luke Skywalker">Luke Skywalker</option>
        <option value="Rafael Nadal">Rafael Nadal</option>
        <option value="Cristiano Ronaldo">Cristiano Ronaldo</option>
        <option value="Albert Einstein">Albert Einstein</option>
      </select>
    </div>
  </div>
</body>

  •  Tags:  
  • Related