Text Padding in Select Boxes

HtmlCss

Html Problem Overview


I've just encountered an obscure issue with select boxes having additional space around text which doesn't seem part of the box model. Here are two pictures of what I mean:

Select Box Padding Test

Select Box Padding Test 2

Here is an example of it happening (on JSFiddle) or you can view the source to my example below:

select, input
{
	padding:0px;
	font-size:20px;
}
select.textIndent
{
	text-indent:-1.5px;
}
select.paddingOffset
{
	padding-left:2.5px;
}
input.paddingOffset
{
	padding-left:5px;
}

<h3>No Padding Test</h3>
<select>
	<option>Item 1</option>
	<option>Item 2</option>
	<option>Item 3</option>
</select><br />
<input type="text" value="Item 1" />
<h3>Negative Text Indent</h3>
<select class="textIndent">
	<option>Item 1</option>
	<option>Item 2</option>
	<option>Item 3</option>
</select><br />
<input type="text" value="Item 1" />
<h3>Padding Offset</h3>
<select class="paddingOffset">
	<option>Item 1</option>
	<option>Item 2</option>
	<option>Item 3</option>
</select><br />
<input class="paddingOffset" type="text" value="Item 1" />

I have already worked out two methods of trying to work around it, by either having a negative text-indent or using left padding to adjust the input and select boxes to match up.

Both of these methods aren't perfect. There is a difference between the negative text-indent for Chrome and Firefox resulting in a noticeable space if the select and input are lined up one after the other. Trying to offset the padding can make maintaining the styling of various input and select fields somewhat difficult where you are applying a percentage of padding.

I first noticed it on a client's website where part of the word was being cut off however if that empty space was not there, the word looks like it would fit.

Client example of the issue

While I can work around this particular issue for my client (eg. wider select box, less padding), I want to know whether there is a different solution for hiding/offsetting/removing the "other" padding on the left/right of the text. Is there another CSS property I should be looking at?

I have a feeling this might just be how select boxes work but would like the collective hive of SO to prove me wrong.

Edit 1

I am hoping that the solution would be pure-CSS. I know that if I use a drop-in replacement for a select box using a JS library, it would not have the same issue.

Edit 2

Just to better clarify what I am after, the select box has an additional padding outside the box model. The pictures below show 6px for the select box and ~3px for the input. I would like to either control this particular padding or remove it entirely. I have already tried with text-indent and using a padding offset to align the text in the input and select elements but both of these seem hackish. I think there is a better CSS way of doing this.

Example Clarified 1

Example Clarified 2

Html Solutions


Solution 1 - Html

Unfortunately, this extra whitespace is added by the browser's rendering engine. Form element rendering is inconsistent across browsers, and in this case is not determined by CSS.

Take a look at this article from Mozilla explaining some ways to mitigate select box incosistency, then you might read this article from Smashing Magazine about styling form elements (be sure to check out the comments and the problems people have had with selects).

Edit 2020: I stumbled across this article from Chris Coyer at CSS-Tricks that appears to show some styling that you can apply to select elements that may help some folks out. It appears overriding the browser's default styling allows you a little more freedom than I was aware of when I posted this answer several years go, according to Liliana Brissos on Team Treehouse.

Solution 2 - Html

The padding you are seeing is coming from the browser specific stylesheet. In chrome the attribute that is responsible for styling these dropdowns is -webkit-appearance, in firefox it is -moz-appearance

You can standardize the look of your select menus by doing the following:

select {
    -webkit-appearance: none;
    -moz-appearance : none;
    border:1px solid #ccc;
    border-radius:3px;
    padding:5px 3px;
}

Note: this will not produce good looking results, only give you standard padding/spacing in your select box. To achieve good looking, customizable select boxes that you can fully control the padding/size etc there are tons of tutorials out there. Here is one I found after a quick google search:

http://www.htmllion.com/default-select-dropdown-style-just-css.html

Solution 3 - Html

The left padding for:

  • OS X Chrome and Safari = 0px;
  • IE 10+ = 2px;
  • Edge 17 = 3px;
  • OS X FF, Android 4.4 = 4px;

These values might need more extensive cross-browser testing, I might update this later. I used a set of CSS hacks to assign different padding-left values per browser using calc with "base" value for left padding. I.e. if the base left padding is 5%, you could use the following SCSS (you can use CSS preprocessor to avoid duplication):

$select-base-gap-x: 5%;
$select-gap-x: calc(#{$select-base-gap-x} - 4px);
$select-gap-x_edge: calc(#{$select-base-gap-x} - 3px);
$select-gap-x_ie: calc(#{$select-base-gap-x} - 2px);

.select {
  -moz-appearance: none;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 0;
  margin: 0;

  padding-left: $select-gap-x;
  }

/* edge 12-18 */
@supports (-ms-ime-align:auto) {
  .select {
    padding-left: $select-gap-x_edge;
    }
  }

/* ie10-11 */
_:-ms-input-placeholder, :root .select {
  padding-left: $select-gap-x_ie;
  }

/* Chrome 37+ (and Opera 24+) */
@supports (-webkit-appearance:none) and (shape-outside:none) {
  .select {
    // Webkit doesn't need any correction for padding so it uses raw base value
    padding-left: $select-base-gap-x;
    }
  }

/* Safari 6.2,7.1+ */
_::-webkit-full-page-media, _:future, :root .select {
    // Webkit doesn't need any correction for padding so it uses raw base value
    padding-left: $select-base-gap-x;
  }

/* Firefox */
@-moz-document url-prefix() {
  .select {
    // Firefox recognizes hacks for webkit, so it needs a proper padding setting again
    padding-left: $select-gap-x;
    }
  }

Here is a demo link: https://codepen.io/andreyvolokitin/pen/wvvXZBm

Solution 4 - Html

A good option would be to replace select box with input datalist

Solution 5 - Html

To edit the select box you have to use jQuery (or javascript in general).

Anyway, you can solve the problem using this "trick":

http://jsfiddle.net/Clear/hwzd88o5/6/

Look at:

select.textIndent
{
    height: 80px;
    line-height: 80px;
    width: 300px;
    padding-left: 50px !important;
}

;)

Solution 6 - Html

What resolved this for me was overriding Chrome's "box-sizing" attribute on select boxes. It has "border-box" - and I overrode that and used "content-box"

Solution 7 - Html

Try this. It worked for me.

 <div id="div">
       <select size=1>
          <option value="0">Option 1</option>
	      <option value="1">Option 2</option>
       </select>
  </div>

CSS

 #div option {padding-left: 5px;}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTurnerjView Question on Stackoverflow
Solution 1 - HtmlJerreckView Answer on Stackoverflow
Solution 2 - HtmlJonathan CroweView Answer on Stackoverflow
Solution 3 - HtmlAndreyView Answer on Stackoverflow
Solution 4 - HtmlAnil JoshiView Answer on Stackoverflow
Solution 5 - HtmlDavide ScanuView Answer on Stackoverflow
Solution 6 - HtmlAdam WodonView Answer on Stackoverflow
Solution 7 - HtmlvandresView Answer on Stackoverflow