tags:

views:

1654

answers:

4

I want to add padding to the options of html "select" tag.

I try to add it in the style property of "select" and "option" and it doesn't work in IE.

What can I do?

A: 

Css:

option{ padding: 0.5em; }

It doesn't work in IE.
Billy
The only browser in which this works is Firefox. Safari, Chrome, and Opera all take no notice of it.
Steve Harrison
+3  A: 

It is difficult to style a drop down box cross browser. To get any sort of control, you'll need to code a replacement one in JavaScript. Be warned, however:

  • Remember that the user may have difficulty using your drop down - take usabilty into account
  • Replace a select element with JS - this is an accessibility issue (and also allows users without JS support to use the form input)
alex
A: 

The following works, in FF3+ and Midori (and presumably other Webkit browsers):

select
  {width: 14em;
     margin: 0 1em;
     text-indent: 1em;
     line-height: 2em;}

select *    {width: 14em;
     padding: 0 1em;
     text-indent: 0;
     line-height: 2em;}

The width is to allow enough room in the displayed select box when not active, the margin does what it always does, text-indent is used to feign padding between the left boundary of the select box and the inner-text. line-height is just to allow vertical spacing.

I can't comment on its use in IE, I'm afraid, so if anyone could feed back -or adapt- to suit that'd be good.

It's worth noting that the drop-down part of the select (select *) isn't affected outside of FF3 for me, for whatever reason, so if that was the question you wanted answering, I apologise for offering nothing new.

Demo at: http://davidrhysthomas.co.uk/so/select-styling.html

David Thomas
+4  A: 

OK, I hate to sound all 1990s, but would simply pre- and app-ending a space to the option text be acceptable for your desired goals?

DVK
+1 vote: This is the best solution given that the OP wants to just add some space to the left and right, and if he does not want to spend a couple of hours implementing custom dropdown.
Jan Zich