99

Select option padding not working in chrome

<style>
select option { padding:5px 0px; }
</style>    
<select>
<option>1</option>
<option>2</option>
<option>3</option> 
</select>
Piyush Marvaniya
  • 2,496
  • 2
  • 16
  • 28
  • 2
    What are you trying to achieve? Each of the options to be padded by 5px? Or just the main box? – DeeKayy90 Mar 27 '14 at 07:47
  • yes...I need padding for option value – Piyush Marvaniya Mar 27 '14 at 07:55
  • 1
    Even though it's not strictly padding, for left padding padding you could use   while this gets fixed: http://jsfiddle.net/chech/u5rcheqf/1/ – chech Dec 11 '14 at 14:57
  • Possible duplicate of [How to style a select tag's option element?](http://stackoverflow.com/questions/5887133/how-to-style-a-select-tags-option-element) – TylerH Nov 29 '15 at 23:40

16 Answers16

149

I just found a way to get padding applied to the select input in chrome

select{
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    padding: 5px;
}

Seems to work in the current chrome 39.0.2171.71 (64-bit) and safari (I only tested this on a mac).

This seems to remove the default styling added to the select input (it also removed the drop down arrow), but allows you to then use your own styling without chrome overriding it.

I stumbled across this fix while using code from here: http://fettblog.eu/style-select-elements/

rmmoul
  • 3,147
  • 3
  • 27
  • 35
  • 1
    **Another solution which works in all browser**, is adding ` ` to first Option in select list check [this](http://stackoverflow.com/questions/25752/how-do-i-put-a-space-character-before-option-text-in-a-html-select-element) and [this](http://stackoverflow.com/questions/5828697/how-to-retain-spaces-in-dropdownlist-asp-net-mvc-razor-views), Hope helps. – Shaiju T Oct 05 '16 at 08:31
  • 3
    @stom That might work for left and right padding, but I don't think it would work for top and bottom padding. – rmmoul Oct 05 '16 at 16:34
  • 1
    This is not working anymore, at least in my machine! :) – Nuno cruz Dec 07 '16 at 10:57
  • 1
    You could also use a plugin like [select2](https://select2.github.io/examples.html) or [chosen](https://harvesthq.github.io/chosen/) which convert your `` elements into fully customizable `
    ` elements. This way you could style all your dropdowns matching your other input fields or general UI.
    – Tad Wohlrapp May 22 '17 at 10:51
  • 3
    the arrows gets lost with this :( – Raul H Dec 22 '17 at 20:38
  • @RaulH I mentioned that in the solution. You'd need to add your own dropdown styling when using this method. – rmmoul Dec 22 '17 at 20:52
  • If option indentation is crucial to your UI, do not try to style it with CSS. You'll run into many cross-browsers issues. Write a script/use a plugin that simulates a select. – png Apr 23 '19 at 22:43
44

This simple hack will indent the text. Works well.

select {
  text-indent: 5px;
}
typeoneerror
  • 55,990
  • 32
  • 132
  • 223
Matt
  • 559
  • 4
  • 7
  • 20
    This adds the padding to the select itself, but not to the options. And applying a text-indent to an option doesn't work either in Chrome 64. – chris.ribal Apr 18 '18 at 12:16
  • Chrome 71 calls this an "unknown property." – Mark Feb 21 '19 at 20:20
  • Does not work well for the longest option (unless you add some spaces at the end, which will be clipped.) – horiatu Feb 10 '23 at 00:11
11

It seems that

Unfortunately, webkit browsers do not support styling of option tags yet.

you may find similar question here

  1. How to style a select tag's option element?
  2. Styling option value in select drop down html not work on Chrome & Safari

The most widely used cross browser solution is to use ul li

Hope it helps!

Community
  • 1
  • 1
Pravin W
  • 2,451
  • 1
  • 20
  • 26
  • 10
    Thanks for the answer. It is amazing to me that after so many years since websites, HTML, and CSS have been around, that there's still a lack of being able to style things like select options padding. Styling and positioning elements can be soo frustrating, yet in Android development, it is so much easier. I don't get why the web environment with HTML and CSS seem so "unfinished", a "work-in-progress" compared to desktop and native mobile environments. – Bryan Apr 01 '15 at 14:40
  • 2
    maybe in near future web would be more beautiful and developers will get helping hand of shadow dom - http://robdodson.me/shadow-dom-styles/ :) hopefully developers will get control over inner layer of form elements. RIP IE though. – Pravin W Apr 01 '15 at 17:18
  • 2
    @Bryan Web is an old technology, and as such it has some old ideas that haven't been improved. Because of browser support, new features have always been slow to reach anyone, and because of a lack of insenstive (there are existing work arounds, that people do, and the end user doesn't notice that these browsers are lacking) nothing changes. – csga5000 Jul 19 '15 at 02:16
6

I fixed it with this

select {
    max-height: calc(1.2em + 24px);
    height: calc(1.2em + 24px);
}


max-height: calc(your line height + (top + bottom padding));
height: calc(your line height + (top + bottom padding));
Community
  • 1
  • 1
Ali Sufyan
  • 75
  • 1
  • 8
  • This is by far the best solution i have seen so far. But it only worked for me when it's `height: calc(1.2em + 24px);` not `max-height: calc(1.2em + 24px);` – Julius Aug 06 '18 at 20:57
  • Julius I'm checked with all major browsers max-height working well. but using height attribute is also a good opportunity. – Ali Sufyan Aug 09 '18 at 15:42
4

Not padding but if your goal is to simply make it larger, you can increase the font-size. And using it with font-size-adjust reduces the font-size back to normal on select and not on options, so it ends up making the option larger.

Not sure if it works on all browsers, or will keep working in current.

Tested on Chrome 85 & Firefox 81.

screenshot (on Firefox)

select {
    font-size: 2em;
    font-size-adjust: 0.3;
}
<label>
  Select: <select>
            <option>Option 1</option>
            <option>Option 2</option>
            <option>Option 3</option>
          </select>
</label>
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
3

Very, VERY simple idea, but you can modify it accordingly. This isn't set up to look good, only to provide the idea. Hope it helps.

CSS:

ul {
    width: 50px;
    height: 20px;
    border: 1px solid black;
    display: block;

}

li {
    padding: 5px 0px;
    width: 50px;
    display: none;

}

HTML:

<ul id="customComboBox">
&nbsp
    <li>Test</li>
    <li>Test 2</li>
    <li>Test 3</li>
</ul>

Script:

$(document).ready(function(){
    $("#customComboBox").click(function(){
        $("li").toggle("slow");
    });
});

DEMO

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
DeeKayy90
  • 837
  • 1
  • 13
  • 27
  • 1
    @PiyushMarvaniya I understand, however as I remember it is not possible to modify single elements with the select option. That is why I offered an alternative. – DeeKayy90 Nov 28 '14 at 02:36
  • 1
    This example doesn't provide keyboard navigation (arrows or mac shortcuts), web accessibility tools. – Piotr Łużecki Sep 28 '15 at 16:36
  • Again, the question was not to create a custom dropdown, it was to add in padding. I have given a sinple base for the OP to build upon according to their requirements, I will not however create a custom control for them. =) – DeeKayy90 Sep 28 '15 at 22:38
  • 1
    If you are going to go with a UL/LI dropdown I would suggest just using Bootstrap's dropdown widget. Version 2 and 3 support role="menu" and role="navigation" to enable arrow keys. Makes sense to use something that has been vetted well IMO. – mbokil Jan 14 '16 at 15:59
  • @mbokil +1, definitely a good option rather than reinventing the wheel, use what's there. – DeeKayy90 Jan 15 '16 at 00:19
3

That's not work on optionentry because it's a "system" generated drop-down menu but you can set the padding of a select.

Just reset the box-sizing property to content-box in your CSS.

The default value of select is border-box.

select {
 box-sizing: content-box;
 padding: 5px 0;
}
user7286228
  • 95
  • 1
  • 1
3

Arbitrary Indentation of any Option

If you just want to indent random, arbitrary <option /> elements, you can use &nbsp;, which has the greatest cross-browser compatibility of the solutions posted here...

.optionGroup {
  font-weight: bold;
  font-style: italic;
}
<select>
    <option class="optionGroup" selected disabled>Choose one</option>
    <option value="sydney" class="optionChild">&nbsp;&nbsp;&nbsp;&nbsp;Sydney</option>
    <option value="melbourne" class="optionChild">&nbsp;&nbsp;&nbsp;&nbsp;Melbourne</option>
    <option value="cromwell" class="optionChild">&nbsp;&nbsp;&nbsp;&nbsp;Cromwell</option>
    <option value="queenstown" class="optionChild">&nbsp;&nbsp;&nbsp;&nbsp;Queenstown</option>
</select>

Ordered Indentation of Options

But if you have some sorted, ordered structure to your data, then it is recommended that you use the <optgroup/> syntax....

The HTML element creates a grouping of options within a element. (Source: MDN Web Docs: <optgroup>)

<select>
    <optgroup label="Australia" default selected>
        <option value="sydney">Sydney</option>
        <option value="melbourne">Melbourne</option>
    </optgroup>
    <optgroup label="United Kingdom">
        <option value="london">London</option>
        <option value="glasgow">Glasgow</option>
    </optgroup>
</select>

Unfortunately, only one <optgroup /> level is allowed and currently supported by browsers today. (Source: w3.org.) Personally, I would consider that part of the spec broken, but you can always extend to third, fourth, etc., levels of indentation with using the &nbsp; trick up above.

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
2

I have a little trick for your problem. But for that you must use javascript. If you detected that the browser is Chrome insert "dummy" options between every options. Give a new class for those "dummy" options and make them disabled. The height of "dummy" options you can define with font-size property.

CSS:

option.dummy-option-for-chrome {
  font-size:2px;
  color:transparent;
}

Script:

function prepareHtml5Selects() {

  var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
  if(!is_chrome) return;

  $('select > option').each(function() {
    $('<option class="dummy-option-for-chrome" disabled></option>')
     .insertBefore($(this));
  });
  $('<option class="dummy-option-for-chrome" disabled></option>')
    .insertAfter($('select > option:last-child'));
}
1

Hey guy the easy way to give option text padding from let just use   like check below

<option value="<?= $subc->gc_id ?>">&nbsp;&nbsp;&nbsp;<?php echo $subc->gc_title; ?> 
</option>

hope everyone enjoys this

abubakkar tahir
  • 737
  • 1
  • 11
  • 13
0

No Jquery - No Third party js file

function clickComboBox(){
  comboOpt = document.getElementsByClassName("opt");
  for (i = 0; i < comboOpt.length; i++) {
      if (comboOpt[i].style.display === "block") {
        comboOpt[i].style.display = "none";
      } else {
        comboOpt[i].style.display = "block";
      }
  }    
}
function clickOpt(value1,text1){
  document.getElementById("selectedValue").value=value1;
  document.getElementById("customComboBox").innerHTML=text1;
  clickComboBox();
}
#customComboBox{
width: 150px;
height: 20px;
border: 1px solid #CCCCCC;
display: block;
padding: 2px 5px;
}
.opt{
padding: 5px 0px;background-color:#CCCCCC;
width: 160px;
display: none;
}
<div id="customComboBox"  onClick="clickComboBox()">
-Select Value-
</div>
<input type="hidden" id="selectedValue">
  <div class="opt">
  <div onClick="clickOpt('01','Test1')"><img  src="https://pasteboard.co/J6940Vs.png" height="20">Test1</div>
  <div style="padding-left:10px;" onClick="clickOpt('02','Test2')">Test2</div>
  <div onClick="clickOpt('03','Test3')">Test3</div>
  </div>

smart code - use jquery

Waruna Manjula
  • 3,067
  • 1
  • 34
  • 33
0

Also it is possible to insert empty groups to get margins.

<select>
  <optgroup></optgroup>
  <option>Special</option>
  <optgroup></optgroup>
  <option selected>First</option>
  <option>Second</option>
  <option>Third</option>
  <optgroup></optgroup>
  <option>Extra</option>
</select>
0

For vertical padding you can use line-height. If you don't need border, you can specify border and make it the same color as the select element background, it will give you padding appearance.

box-shadow can be used to add stroke to the element if really needed.

select {
  background-color: #555;
  border: 10px solid #555;
  border-width: 15px 15px 15px 20px;
  border-radius: 30px;
  cursor: pointer;
  color: white;
}
<select>
  <option>Snake</option>
  <option>Squirrel</option>
  <option>Sheep</option>
  <option>Owl</option>
</select>
-1

Update 2022 - I know this answer is late, it works on different browsers, by manipulating in the margin, indent and padding.

Hope it works!

 <style>

    select {
        margin: 6px;
        font: 16px Arial;
    }
    select {
        width: 262px;
        height: 32px;
        padding: 4px;
        line-height: 32px;
        text-indent: 4px;
        cursor: pointer;
    }

    </style>    
    <select>
    <option>1</option>
    <option>2</option>
    <option>3</option> 
    </select>
Fadi
  • 585
  • 4
  • 22
-2

You can use font-size property for option if you need.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Rahul
  • 46
  • 4
  • This seems to work in chrome but not in Firefox (latest version) Firefox is still only targeting the style at the unselected box before the dropdown. –  Apr 05 '18 at 17:32
-10

You should be targeting select for your CSS instead of select option.

select { 
padding: 10px;
margin: 0;
-webkit-border-radius:4px;
-moz-border-radius:4px;
border-radius:4px;
}

View this article Styling Select Box with CSS3 for more styling options.

Rachelle
  • 16
  • 5