How to solve the problem that the font stretching attribute cannot be started
P粉676821490
2023-08-26 14:42:23
<p>font-stretch属性根本不起作用。这是我的代码:</p>
<pre class="brush:php;toolbar:false;"><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>font-stretch</title>
<style>
p {
font-size: 5em;
font-family: 'Myriad Pro';
}
</style>
</head>
<body>
<p>
<span style="font-stretch: ultra-condensed">P</span>
<span style="font-stretch: extra-condensed">P</span>
<span style="font-stretch: condensed">P</span>
<span style="font-stretch: semi-condensed">P</span>
<span style="font-stretch: normal">P</span>
<span style="font-stretch: semi-expanded">P</span>
<span style="font-stretch: expanded">P</span>
<span style="font-stretch: extra-expanded">P</span>
<span style="font-stretch: ultra-expanded">P</span>
</p>
</body>
</html></pre>
<p>我已经尝试了很多其他字体,问题仍然存在。请帮忙解决</p>
You can use
font-stretch
to select a stretched or stretched font among these fonts. This property has no effect if the font you are using does not provide a compressed or expanded font.Google Fonts' user interface currently still prefers static/single-weight CSS output.
But you can manually force the API to output the
@font-face
rules for variable fonts:It is important to use '..' as the range separator - otherwise you will end up with a CSS containing multiple static
woff2
file URLs.Additionally, Google's API uses some user-agent detection (also known as browser sniffing) to provide backwards compatibility (for browsers that do not support variable fonts). This makes sense... Unfortunately, this doesn't work very well: some browsers like Opera (which supports VF perfectly) also receive static fonts. (This may also work for other Chromium/Blink based browsers)
As a workaround, I recommend opening the above CSS URL in Firefox. The result should look similar to:
Note that
font-weight
andfont-stretch
attribute values contain 2 values, indicating the range of weight/width. This is a good indicator that you've acquired the correct (variable) rules.Example: Inconsolata variable font