简体   繁体   English

字体大小可以设置为页面宽度的百分比吗?

[英]Can font-size be set to a percentage of page-width?

我正在制作一个移动网站,并尝试这样做,因此标题h1标签将调整为屏幕尺寸的80%。

According to CSS Values and Units Module Level 3 (Candidate Recommendation), you can use the vh unit, which is equal to 1% of the width of the initial containing block. 根据CSS值和单位模块级别3 (候选推荐),您可以使用vh单位,该单位等于初始包含块的宽度的1%。 So for example, font-size: 5vh would set font size to 5% of the body width. 例如, font-size: 5vh会将字体大小设置为正文宽度的5%。 Browser support is fairly good in newest versions, but this excludes eg IE 8. This more or less answers the question in the title. 浏览器支持在最新版本中相当不错,但这不包括例如IE 8.这或多或少回答了标题中的问题。

The description in the body of the question is something rather different. 问题正文中的描述有些不同。 The font size is the height of the font and does not have any defined relationship with the widths of letters (which vary), so there is really no way in CSS to make some text occupy 80% of some width. 字体大小是字体的高度,并且与字母的宽度(有所不同)没有任何已定义的关系,因此CSS中没有办法使某些文本占据某个宽度的80%。 You can set the width of an h1 element of course, but this is very different from setting the width of its text content. 您当然可以设置h1元素的宽度,但这与设置文本内容的宽度非常不同。 You would need JavaScript to set the text width. 您需要JavaScript来设置文本宽度。

Font size can't be use as percentage (%) you can use em and px extensions for font size for example font-size:2 em; 字体大小不能用作百分比(%),你可以使用em和px扩展名的字体大小,例如font-size:2 em;

or font-size:10px; 或font-size:10px;

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

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