\n\n\n\n\nStyle .select2-style class in CSS with desired padding, and Chrome will display your chic dropdown.","image":"https://explain.codes/media/static/images/eightify-logo.svg","author":{"@type":"Person","name":"Anton Shumikhin","url":"https://explain.codes//author/anton-shumikhin"},"publisher":{"@type":"Organization","name":"Rational Expressions, Inc","logo":{"@type":"ImageObject","url":"https://explain.codes/landing/images/[email protected]"}},"datePublished":"2024-12-02T13:45:01.395Z","dateModified":"2024-12-02T13:45:03.363Z"}
Explain Codes LogoExplain Codes Logo

Select option padding not working in chrome

html
css-trickery
responsive-design
web-development
Anton ShumikhinbyAnton Shumikhin·Dec 2, 2024
TLDR

When padding falls flat in <option> elements in Chrome, jQuery select2 plugin comes to the rescue. It offers a dropdown with styles that bypass browser limitations:

<select class="select2-style"> <option value="1">Stylish Option 1</option> <option value="2">Stylish Option 2</option> </select> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script> <script> $(document).ready(function() { // Apply the magic $('.select2-style').select2(); }); </script>

Style .select2-style class in CSS with desired padding, and Chrome will display your chic dropdown.

When browsers play tough

Chrome, in particular, has a different manifesto to follow - it uses its own styles (a.k.a user agent stylesheet) over custom ones, curtailing customization of elements like option.

Even with notorious -webkit-appearance: none;, -moz-appearance: none;, or appearance: none; CSS trickery, the browser stands its ground, resisting padding on <option> elements.

Styling options for select elements

Dressing up the dropdown arrow

Chrome allows you to go wild with the styles of the dropdown arrow - just use -webkit-appearance: none; on <select> and put on your creativity hat:

.select-style { -webkit-appearance: none; /* Add your styling here. Sky's the limit! */ } .select-style + .custom-arrow { /* Go all Picasso on the arrow here */ }

The sneaky indent padding trick

Don't present. Pretend. Apply text-indent: 5px; on <select> for a faux padding effect:

.select-style { text-indent: 5px; /* Other styles to make it look fabulous */ }

Swapping <select> and <option> with ul and li

To obtain a uniform look across browsers, swap <select> and <option> with ul and li. Then put JavaScript to work for managing the selection states.

The font size twist

Give some space to <option> elements by tweaking the font-size. It simulates padding and leaves more room for interaction:

.option-style { font-size: 1.2em; /* Giant readable text. A treat to sore eyes */ }

Crafting pseudo-padding with height

Manipulating the height of <select> with calc offers the illusion of vertical padding:

.select-style { height: calc(2em + 10px); /* Sneak in a 10px pseudo-padding. Don't tell Chrome! */ }

Handling the quirks

Playing with font size

font-size-adjust: 0.3 combined with font-size: 2em keeps the visual size of <option> elements stable, ensuring cross-browser consistency.

Embracing Webkit limitations

Understand that Chrome and other Webkit browsers pose styling constraints on <option> tags. Primarily, you only get a free hand with font styles. Patience is key!