I am trying to do some quite precise styling on some form elements, and this issue is causing me a lot of grief.
If I try to remove padding, margin, border and outline from an <input> (with display: block) so that the size of the field is purely determined by the text, the input field ends up having a few pixels extra padding than any other block level element styled exactly the same way. Here’s an example:
http://jsfiddle.net/nottrobin/b9zfa/
<input class="normalised testSubject" value="hello" />
<div class="normalised testSubject">hello</div>
Rendering:

In that example, the <div> gets a computed height of 16px while the <input> gets a computed height of 19px.
I get the same behaviour in Chrome 16, Firefox 9 and Opera 11 so it’s clearly rendering engine independent.
I can fix the issue by manually adding a height, but I don’t want to do that because I want the design to remain responsive.
Can anyone help me understand what’s going on here, and how I can reliably make sure that the <input> will be the same height as any block level element that follows it?
The
<input>has a minimumline-heightbased on font size. Setting both elements to a largerline-heightvalue works, as does removingline-heightaltogether. But that still doesn’t allow you to have smaller heights than the minimum. The fix for that is using thefirst-linepseudo-element and setting it todisplay: inline-block;.Demo: http://jsfiddle.net/ThinkingStiff/B7cmQ/
CSS:
But this doesn’t explain why the
<input>is acting differently than the<div>. Even-webkit-appearance: none;didn’t fix it. It would seem there is some invisible voodoo on inputs that treats its contents asinline.inlineelements have minimunline-heightbased on font size, which is the behavior we’re seeing here. That’s whyfirst-linefixes it. It seems to be styling the “child” element of the<input>.Here’s a demo that shows the minimum
line-heightoninlineelements. The<div>element honorsline-height: 7px;. The<span>, even though its computed value is showing7px;, is not honoring it visually.Demo: http://jsfiddle.net/ThinkingStiff/zhReb/
Output:
HTML:
CSS: