[英]How do I vertically fill a container with uppercase text using JS/CSS?
I have a text that is uppercase, eg ABC
. 我有一个大写的文本,例如ABC
。
As it is uppercase, all characters have the same height. 由于它是大写的,所有字符都具有相同的高度。
I also have a container ( div
) with fixed height, eg 100px
. 我还有一个固定高度的容器( div
),例如100px
。
How do I make this text fill it vertically, so each letter is exactly 100 pixels high? 如何使此文本垂直填充,因此每个字母正好是100像素高?
I tried font-size: 100px
, but it does not fill the container (there are gaps above and below). 我尝试使用font-size: 100px
,但它没有填充容器(上面和下面有间隙)。
See http://jsfiddle.net/6z8un/1/ for an example. 有关示例,请参见http://jsfiddle.net/6z8un/1/ 。
UPDATE 1: 更新1:
Let's assume all characters actually have the same height (difference either does not exist or is negligible). 假设所有字符实际上具有相同的高度(差异不存在或可忽略不计)。 Otherwise the question does not make much sense. 否则问题没有多大意义。
UPDATE 2: 更新2:
I am pretty sure it can be solved using https://stackoverflow.com/a/9847841/39068 , but so far I had no perfect solution with it. 我很确定它可以使用https://stackoverflow.com/a/9847841/39068解决,但到目前为止我没有完美的解决方案。 I think ascent and descent are not enough, I would need something else for the top space. 我认为上升和下降是不够的,我需要顶级空间的其他东西。
line-height
http://jsfiddle.net/6z8un/2/ will not solve the problem because this will not remove the whitespaces. line-height
http://jsfiddle.net/6z8un/2/不会解决问题,因为这不会删除空格。 You could apply the size by hardcoding (for me it fits with font-size of 126px) But this is different to every user (sans-serif can be configured by user/system/browser) 您可以通过硬编码应用大小(对我而言,它适合126px的字体大小)但这对每个用户都不同(sans-serif可以由用户/系统/浏览器配置)
Windows default sans-serif font MS sans serif
is different to Droid sans serif
on Android or DejaVu Sans
on Ubuntu. Windows默认的sans-serif字体MS sans serif
与Android上的Droid sans serif
或Ubuntu上的DejaVu Sans
不同。
To solve this problem, you could set a font to default, like Times New Roman
, but not every system does have this font by default. 要解决此问题,您可以将字体设置为默认值,例如Times New Roman
,但默认情况下并非每个系统都具有此字体。
To solve this, you could use a custom font imported from a server like htttp://google.com/fonts but not every browser does support custom fonts. 要解决此问题,您可以使用从httpsp://google.com/fonts等服务器导入的自定义字体,但不是每个浏览器都支持自定义字体。
I think the only way to solve this is to use an image. 我认为解决这个问题的唯一方法是使用图像。
But custom fonts should do their job on modern browsers too :) (eg: http://jsfiddle.net/6z8un/5/ ) 但是自定义字体也应该在现代浏览器上完成它们的工作:)(例如: http : //jsfiddle.net/6z8un/5/ )
Use this code. 使用此代码。 I hope this can help you. 我希望这可以帮到你。
<div class="outer"><div class="inner">ABC</span></div>
.outer {
margin: 0;
padding: 0;
height: 75px;
overflow-y: hidden;
}
.inner {
font-size: 100px;
background-color: #ccc;
font-family: sans-serif;
margin-top: -18px;
}
Note: As I know whenever we use font-size the upper and lower gap is also the part of height. 注意:据我所知,每当我们使用font-size时,上下间隙也是高度的一部分。 I mean font-size = upper gap + actual height of font + lower gap. 我的意思是font-size =上部差距+字体的实际高度+下部差距。 So if we want 100px div then use font-size larger than 100. 因此,如果我们想要100px div,那么使用大于100的font-size。
Is this ok? 这个可以吗?
http://jsfiddle.net/6z8un/4/ http://jsfiddle.net/6z8un/4/
HTML: HTML:
<div><span>ABC</span></div>
CSS: CSS:
div {
height: 100px;
background-color: #ddd;
font-family: sans-serif;
}
span {
font-size:136px;
margin-top:-25px;
display:inline-block;
};
So far I made a small script that measures letter heights using canvas (would be a good thing to put on GitHub I suppose). 到目前为止,我制作了一个小脚本,使用canvas来测量字母高度(我想在GitHub上放一个好东西)。 It is currently slightly unprecise, mostly because of caching. 它目前略显不足,主要是因为缓存。
I have published it as a library on GitHub, see here: https://github.com/ashmind/textmetrics . 我已将它作为GitHub上的库发布,请参见: https : //github.com/ashmind/textmetrics 。
Unfortunately I did not have time to make demo work as a GitHub page yet, so I can't link to it. 不幸的是我没有时间将演示工作作为GitHub页面,所以我无法链接到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.