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:
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.
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.
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.
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.
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.
There are many ways to design a <select> dropdown menu using CSS. The Dropdown Menu is mainly used to select an element from the list of elements. Each menu option can be defined by an <option> element that can nested inside the <select> element.
THE PADDING: The padding is the space between the content of an element and the border of that same element, or we can simply say the space inside the border. This is similar to the spaces between your bed and the wall of your room.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With