How to Style the Option of an HTML Select Element?

How to Style the Option of an HTML Select Element?

You can use the <style> tag with basic CSS to style the option of an HTML select element.  

In HTML form, <select> and <option> are their key parts, which also allow the user to pick an option from the dropdown list. Styling the <option> elements is a bit tough because many browsers don’t support styling directly with the CSS. We will discuss different methods to style the option in this blog.

Table of Contents:

Methods to Style the Option of an HTML Select Element

Browsers have strict default styles for <option> elements, making them difficult to customize. While the <select> element itself can be styled using CSS, <option> elements are often limited in terms of styling. However, there are various workarounds and techniques to achieve a customized appearance.

Method 1: Basic CSS for <select> Styling

The <option> elements can’t be styled much, so you can style the <select> option to make the dropdown look attractive.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Styled Select</title>
    <style>
        select {
            background-color: #f0f0f0;
            border: 2px solid #007bff;
            padding: 10px;
            border-radius: 5px;
            font-size: 16px;
        }
    </style>
</head>
<body>
    <select>
        <option>Option 1</option>
        <option>Option 2</option>
        <option>Option 3</option>
    </select>
</body>
</html>

Output:

Basic CSS for select Styling Output

Explanation: You can style the options dropdown with light grey with a blue border, and you can also apply the padding, rounding corners, and text with a large size. You will get three options to choose from.

Method 2: Using appearance: none for Custom Styling

You can customize the browser’s default size using appearance: none. So that you can use your own CSS properties for styling them.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Styled Select with Appearance None</title>
    <style>
        select {
            appearance: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            background-color: white;
            padding: 10px;
            border: 1px solid #333;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <select>
        <option>Option 1</option>
        <option>Option 2</option>
        <option>Option 3</option>
    </select>
</body>
</html>

Output:

Custom Styling Output

Explanation: You can customize the options dropdown with a white background, add padding, and create a pointer cursor. Customization can be done using the appearance: none to override the browser’s default style.

Method 3: Using Third-Party Libraries

You can make the dropdown menu look advanced by using external libraries like Select2, Choices.js, or Tom Select. You can fully customize your dropdown using these libraries.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Select2 Example</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
    <style>
        /* Increase the width of Select2 dropdown */
        .select2-container {
            width: 200px !important; 
        }
    </style>
</head>
<body>
    <select id="mySelect" style="width: 200px;">
        <option value="" disabled selected>Select an option</option>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
    </select>
    <script>
        $(document).ready(function() {
            $("#mySelect").select2({
                width: 'resolve' // Adjusts width to the parent element
            });
        });
    </script>
</body>
</html>

Output:

Using Third-Party Libraries

Explanation: You can use the libraries to style them and add some basic dropdown features. The library used in this code is Select2. You can also include some important links to select the CSS and JavaScript files. And when the document is ready, it starts the plugins.

Conclusion

You can find styling the <option> elements is challenging since the browser doesn’t support it directly. You can apply some basic CSS properties to make the option dropdown look stylish. Using appearance: none and libraries like Select2, Choices.js, and Tom Select are the advanced options to customize the <option> element. And these methods help you to create user-friendly dropdown menus.    

How to Style the Option of an HTML Select Element? – FAQs

1. Can I directly style the option elements using CSS?

No, many browsers limit the styling of the option element and CSS properties don’t have any effect on this element.

2. How can I style the select element itself?

You can apply the CSS properties to style the select element, such as its background color, padding, font size, etc.

3. What are some workarounds for styling option elements?

You can use the JavaScript to create a customized option dropdown.

4. Are there any libraries that help with styling dropdowns?

Yes, libraries such as Select2, Choices.js, or Tom Select are used to create custom-styled option dropdowns.

5. Can I use pseudo-elements to style the select element?

Yes, you can use::after and:: before pseudo-elements to create the customized dropdown. This is generally used to customize the arrow.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner