I'm trying to style a text input with a value, text input with a placeholder, and a span, identically in Chrome. Specifically, I would like to control the line-height independently of the font size.
However, there seems to be some sort of minimum line-height (or something causing a similar affect) on the input value, that seems to push the text down somehow that prevents the identical styling.
Example HTML:
<div>
<input type="text" value="Text">
<input type="text" placeholder="Text">
<span>Text</span>
</div>
CSS:
div {
line-height: 50px;
font-family: Arial;
}
input,
span {
font-size: 50px;
line-height: 50px;
height: 50px;
width: 100px;
padding: 0;
min-height: 0;
display: inline-block;
font-family: inherit;
border: 2px solid red;
overflow: hidden;
vertical-align: top;
}
And the results can be seen at
http://plnkr.co/edit/oHbhKDSPTha8ShWVOC7N?p=preview and in the following screen shot from Chrome 44.0.2403.155 (64-bit) on Linux:
Strangely, the placeholder seems to be styled with the desired line-height, while the text value of the input is positioned differently. I'm not concerned with the colour of the placeholder at this point.
How can I style all 3 elements so the text is in the same position, where I'm using a custom line-height?
I understand I can just set the line-height to normal
or 1.2
, or reduce the font size, to make the elements appear identically, but they would not have the visual appearance I'm looking for.
Try like this
As per the link:Firefox line-height issue with input fields
so reduce the font-size:50px to 45px it will look fine.
Code Below
Why is this happening?
This misalignment is being caused by the caret and as such I don't think you will find a way to align the text if the
font-size
andline-height
are the same.Why is the caret at fault?
The caret has a
height
greater than the text which is causing the alignment to be skewed.There are a few things which support this:
1. You can see the size of the caret
input
and hold the left mouse button. Drag up and down and you will see that the text will moveheight: 50px;
frominput, span
. The size of theinput
will now increase to theheight
of the caret2. The placeholder text is correctly aligned
input
the alignment is thrown offThe result of the caret having a greater
height
is that theline-height
is being artificially increased causing the text to be out of line.This can be proven by:
line-height
to58px
. The alignment of the placeholder text andspan
will be the same as theinput
font-size
to45px
. The caret will now fit in the50px
height
What can be done?
As there is no way to style the caret itself (to make it smaller) the most efficient way of ensuring the text is aligned would be to use a
font-size
which is smaller than theline-height
. This will in turn make the caret smaller and stop it from artificially increasing theline-height
of theinput
.Alternatively you could remove
height
and just specify aline-height
equal to theheight
of the caret: