简体   繁体   English

当字体不同地计算高度时,如何垂直对齐文本?

[英]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. 这是一个内部包含p个元素的div。 However I can't seem to center the p elements vertically. 但是我似乎无法将p元素垂直居中。

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. 我现在所做的只是给父div填充了一些内容,逻辑上文本应该在中间,但似乎字体在计算高度时却有所不同。 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. 如果我将字体更改为Verdana,则文本会对齐,但这不是首选的解决方案。

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 . 您的问题是div的固定height20px If you change that value to 28px , your text might vertical-align. 如果将该值更改为28px ,则文本可能会垂直对齐。

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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