I’m creating a css theme engine and one issue I came across was vertical height issue for different fonts. I read many articles online and all addressed solution for a specific font family. But in my theme engine users can use many fonts as a selection. This vertical height issue is really painful when aligning icons and labels. So I want to know whether there is a way to achieve this.
I created a sample code in jsFiddle please check it out using this URL jsfiddle
// html
<button id="arial">
arial
</button>
<button id="tahoma">
helvetica
</button>
// css
#arial {
font-family: airal;
font-size: 1em;
padding: 5px 10px;
}
#tahoma {
font-family: Tahoma;
font-size: 1em;
padding: 5px 10px;
}
so as you can see I used arial
and tahoma
(please don’t mind the button text). you can see how text positioning and space from top and bottom has changed. pixel values are not web pixel values I just zoomed image and got the dimension in photoshop just to show the difference.
5
Answers
Well if i understand correctly you are currently using padding/margin to align an element(in your case text) vertically, so if height changes the position also changes as you were calculating the distances so you need a solution where it centers automatically itself by calculating the height. That’s what i understand.
Well i guess you need something like
top : calc(50% - 10px);
Here’s a JSFiddle https://jsfiddle.net/xnjq1t22/
And This link contains more hacks to align items vertically center http://jsfiddle.net/techsin/FAwku/1/
Try using the
line-height
attribute.I set the
body
font-size
to16px
to make sure both font families have the same size – depending on the browser1em
might be different – and removed the vertical padding from the buttons and used a line-height of +10px the body font size to get the same result.Bonus tip (edited):
line-height
cannotshould not be used withinput
elements, just use height there instead – details after the fiddle.https://jsfiddle.net/5j4L2tpj/
The reason is because in some browsers – Safari for iOS for example – the vertical alignment of text is messed up. Simple
height
property works as expected, perfectly aligning the text vertically – verticalpadding
not required in this case. To sum up, just useheight
on inputs, withoutline-height
or verticalpadding
and get the job done as expected an all browsers/devices.PS You had a typo for the
Arial
font family.Here’s a solution:
Check the updated fiddle: https://jsfiddle.net/upb7cL5g/4/
That is not Arial.
You have (in the fiddle too) indicated a font-family of airal, which does not exist, and is replaced by what is probably Times Roman. The default sizes (at least on my Firefox 57) seem different to me. I’d try to reproduce the issue once you’re sure of the actual fonts involved.
How about using flex?
Check the cross-browser prefixes for
flex