The Wayback Machine - https://web.archive.org/web/20240617163157/https://www.geeksforgeeks.org/how-to-use-bootstrap-select-for-dropdown/
Open In App

How to use bootstrap-select for dropdown ?

Last Updated : 11 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Bootstrap Select is a form control that shows a collapsible list of different values that can be selected. This can be used for displaying forms or menus to the user. This article shows the methods by which a <select> element can be styled in Bootstrap, using both custom styles and bootstrap-select.

Using the default custom styles: Bootstrap has custom styles that can be applied to some form elements. Custom select menus require only one custom class, that is, .custom-select to trigger the custom styles. However, custom styles are restricted to the select’s initial appearance and cannot alter the option’s due to browser limitation. The example below represents how the default <select> element can be styled using .custom-select in Bootstrap.

Example:

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href=
        crossorigin="anonymous">
    </script>
 
    <!-- Optional JavaScript -->
    </script>
    <script src=
    </script>
    <script src=
    </script>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
 
    <!-- Use the custom-select class -->
    <select class="custom-select"
            style="width:150px;">
        <option>Pizzas</option>
        <option>Burger</option>
        <option>Ice Cream</option>
        <option>Fried Potatoes</option>
    </select>
</body>
 
</html>


Output:

There are only some style properties that can be applied to the <option> component. This is because this sort of component is a case of a “replaced component”. They are OS-dependent and are not the portion of the HTML/browser. It cannot be styled through CSS. Except for background-color and color, the style settings applied through the style object for the <option> component are disregarded.

The select option is styled by the Operating System, not by HTML. Hence the CSS style does not have any impact.

option { 
    background-color: color; 
    border-radius: value; 
    font-size: value 
}

In general, the above values will work. However, we can not customize the padding, margin, and other properties.

Bootstrap-select: To solve the above problems, bootstrap-select can be used to style <option> and <select> tags. 

Note: By default, bootstrap-select naturally recognizes the version of Bootstrap that is being used. However, there are a few occurrences where the version detection may not work.

The below example shows how bootstrap-select can be included in the page and initialized. The selectpicker class is used in the select components to auto-initialize bootstrap-select as explained in the example:

Example:

HTML




<!DOCTYPE html>
<html>
 
<head>
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
 
    <!-- CDN link used below is compatible with this example -->
    <link rel="stylesheet" href=
    <script src=
    </script>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <select class="selectpicker"
        data-style="btn-success">
        <option>Pizzas</option>
        <option>Burger</option>
        <option>Ice Cream</option>
        <option>Fried Potatoes</option>
    </select>
</body>
 
</html>


Output:

Below are some attributes that can be used to style the <select> tag:

  • data-live-search: It allows us to add a search input.
  • data-tokens: It allows us to add keywords to options to improve their search ability.
  • data-max-options: It allows us to specify the limit the number of options that can be selected. It also works for option groups.
  • title: This attribute allows us to set the default placeholder text when nothing is selected.
  • data-style: This attribute helps us to style the button classes.
  • show-tick: This attribute helps us to show the checkmark icon on standard select boxes.
  • data-width: This attribute helps us to set the width of the select.

Below are some attributes that can be used to style the <option> tag:

  • data-icon: It is used to add an icon to an <option> or <optgroup>.
  • data-content: It is used to insert custom HTML into the <option>.
  • data-subtext: It is used to add a subtext to an <option> or <optgroup> element.

Example:

HTML




<!DOCTYPE html>
<html>
 
<head>
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
    <script src=
    </script>
 
    <!-- CDN link used below is compatible with this example -->
    <link rel="stylesheet" href=
    <script src=
    </script>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
 
    <!-- Using the attributes to style the
        <select> and <option> tag -->
    <select class="selectpicker">
        <option data-content=
"<span class='badge badge-danger'>Pizzas</span>">
            Pizzas
        </option>
        <option data-content=
"<span class='badge badge-success'>Burger</span>">
            Burger
        </option>
        <option data-content=
"<span class='badge badge-primary'>Ice Cream</span>">
            Ice Cream
        </option>
        <option data-content=
"<span class='badge badge-warning'>Fried Potatoes</span>">
            Fried Potatoes
        </option>
    </select>
</body>
 
</html>


Output:



Next Article

Similar Reads

How to use Multi-Select Dropdown in React-Bootstrap ?
In ReactJS applications, we always need to add the UI component that allows us to select multiple options from the DropDown list. So in Bootstrap, the Multi-Select Dropdown is a UI component that allows us to select multiple different options from the list of dropdown menus. Additionally, we can do the filtering, tagging, and other options to make
4 min read
React Suite Dropdown Dropdown with Icon
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. The dropdown component allows the user to provide navigation that uses a select picker if you want to select a value. Dropdown with Icon will help us to add an image as per the dropdown item. This will add more
3 min read
How to detect if a dropdown list is a multi-select using jQuery?
In this article, we will learn how to detect if a dropdown list or an HTML select element is a multi-select one or not using jQuery. There is one main approach with which this can be implemented. Approach: Using the prop() method in the jQuery library. There is one select element with an id of dropdown defined in the following example which also ha
3 min read
How to change a select's options based on another dropdown using React?
React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It’s ‘V’ in MVC. ReactJS is an open-source, component-based front end library responsible only for the view layer of the application. It is maintained by Facebook. Select in HTML allows us to choose among multiple values using the dropdown. Here, we are
3 min read
Auto select option from Dropdown in ReactJS
Auto Select option from Dropdown means without forcing the user to click on any options, the option value is auto-populated. Material UI for React has this component available for us and it is very easy to integrate. We can auto-select the options from Dropdown using the following approach. Creating React Application And Installing Module: Step 1:
2 min read
How to select/deselect multiple options in dropdown using jQuery ?
To select and deselect multiple options in a drop-down menu, we will use the HTML and CSS codes. HTML code helps to define the basic structure of the webpage and CSS will benefit in designing the web page. Some essential properties used in the code are as follows- &lt;div&gt; - This is a division tag that helps us to define a particular section of
4 min read
How to make a multi-select dropdown using Angular 11/10 ?
In this article, we will learn to build the multiple selection drop-down menu in Angular. To accomplish this task, we require Angular 10 or the Angular 11 version. Sometimes we need to display dynamically fetched multi-selected data in a drop-down menu, for this, we will use the npm @ng-select/ng-select package as a multi-select drop-down menu. The
3 min read
How to select multiple options at once in dropdown list in HTML5?
Dropdown lists are one of the most flexible elements in HTML. It is similar to that of the radio input, that is, only one item can be selected from a group of items by default. However, when the multiple attribute is used with the &lt;select&gt; element, we can enable the selection of multiple options from the list. The multiple attribute is a bool
3 min read
Create Searchable Dropdown Select With jQuery
Create a searchable dropdown select with jQuery by enhancing a standard HTML &lt;select&gt; element. This improves user experience by allowing quick and efficient selection from large lists. PrerequisitesHTMLCSSJavaScriptJQueryApproachThe HTML document includes the standard structure with "&lt;!DOCTYPE html&gt;", "&lt;html&gt;", "&lt;head&gt;", and
3 min read
How to remove all the options of a select box and then add one option and select it using JQuery ?
Given a select box with a fixed number of options, the task is to remove all the options and then add one option and immediately select that particular option using jQuery. There are two approaches that can be taken to achieve this: Approach 1: Using find(), remove(), end() and append() Methods: In the following example, a select box with some opti
3 min read
three90RightbarBannerImg