简体   繁体   中英

How to vertically align text when font is calculating height differently?

I apologize in beforehand if this is a duplicate but I can't seem to find an answer.

I made something that resembles a progress bar. It's a div with p elements inside. However I can't seem to center the p elements vertically.

All i did now was give the parent div some padding and logically the text should be in the middle but it seems like the font is counting the height differently. And so the solution to change the line-height won't work. If I change the font to Verdana the text is aligned but that is not a preferred solution.

Snippet:

 div { width: 90%; background-color: green; text-align: left; padding: 1.2%; border-radius: 5px; height: 20px; font-family: 'Hind Guntur', sans-serif; } p { font-size: 90%; display: inline; margin: 0; color: white; } 
 <link href="https://fonts.googleapis.com/css?family=Catamaran|Hind+Guntur" rel="stylesheet"> <div> <p>1</p> </div> 

在此处输入图片说明

check this

 div { width: 90%; background-color: green; text-align: left; padding: 1.2%; border-radius: 5px; height: 20px; font-family: 'Hind Guntur', sans-serif; } p { font-size: 90%; display: inline; margin: 0; color: white; vertical-align: middle; } 
 <link href="https://fonts.googleapis.com/css?family=Catamaran|Hind+Guntur" rel="stylesheet"> <div> <p>1</p> </div> 

This should be what you are looking for. Your problem was that you had a fixed height of 20px on the div . If you change that value to 28px , your text might vertical-align.

 div { width: 90%; background-color: green; text-align: left; padding: 1.2%; border-radius: 5px; height: 28px; font-family: 'Hind Guntur', sans-serif; } p { font-size: 90%; display: inline; margin: 0; color: white; } 
 <link href="https://fonts.googleapis.com/css?family=Catamaran|Hind+Guntur" rel="stylesheet"> <div> <p>1</p> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM