font-style - CSS(层叠样式表) 编辑
font-style
CSS 属性允许你选择 font-family
字体下的 italic
或 oblique
样式。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Italic 样式一般是指书写体,相比无样式的字体,通常会占用较少的高度,而 oblique 字形一般只是常规字形的倾斜版本。斜体(italic) 和倾斜体(oblique)都是通过人工倾斜常规字体的字形来模拟的(使用 font-synthesis
对此进行控制)。
Italic font faces are generally cursive in nature, usually using less horizontal space than their unstyled counterparts, while oblique faces are usually just sloped versions of the regular face. When the specified style is not available, both italic and oblique faces are simulated by artificially sloping the glyphs of the regular face (use font-synthesis
to control this behavior).
语法
font-style: normal;
font-style: italic;
font-style: oblique;
font-style: oblique 10deg;
/* Global values */
font-style: inherit;
font-style: initial;
font-style: unset;
font-style
属性被指定为从下面的取值列表中的单独一个关键字,如果关键字是 oblique
,则可附加一个可选的角度。
值
normal
- 选择
font-family
的常规字体。 italic
- 选择斜体,如果当前字体没有可用的斜体版本,会选用倾斜体(
oblique
)替代。 oblique
- 选择倾斜体,如果当前字体没有可用的倾斜体版本,会选用斜体(
italic
)替代。 If one or more oblique faces are available in the chosen font family, the one that most closely matches the specified angle is chosen. If no oblique faces are available, the browser will synthesize an oblique version of the font by slanting a normal face by the specified amount. Valid values are degree values of-90deg
to90deg
inclusive. If an angle is not specified, an angle of 14 degrees is used. Positive values are slanted to the end of the line, while negative values are slanted towards the beginning.
In general, for a requested angle of 14 degrees or greater, larger angles are prefered; otherwise, smaller angles are preferred (see the spec's font matching section for the precise algorithm).
可变字体
可变字体(Variable font)可以精确控制 oblique 字体的倾斜程度。You can select this using the <angle>
modifier for the oblique
keyword.
For TrueType or OpenType variable fonts, the "slnt"
variation is used to implement varying slant angles for oblique, and the "ital"
variation with a value of 1 is used to implement italic values. See font-variation-settings
.
For the example below to work, you'll need a browser that supports the CSS Fonts Level 4 syntax in which font-style: oblique
can accept an <angle>
.
HTML
<header>
<input type="range" id="slant" name="slant" min="-90" max="90" />
<label for="slant">Slant</label>
</header>
<div class="container">
<p class="sample">...it would not be wonderful to meet a Megalosaurus, forty feet long or so, waddling like an elephantine lizard up Holborn Hill.</p>
</div>
CSS
/*
AmstelvarAlpha-VF 由 David Berlow 制作:https://github.com/TypeNetwork/Amstelvar
在此使用时,遵循此开源协议:
https://github.com/TypeNetwork/Amstelvar/blob/master/OFL.txt
*/
@font-face {
src: url('https://mdn.mozillademos.org/files/16044/AmstelvarAlpha-VF.ttf');
font-family:'AmstelvarAlpha';
font-style: normal;
}
label {
font: 1rem monospace;
}
.container {
max-height: 150px;
overflow: scroll;
}
.sample {
font: 2rem 'AmstelvarAlpha', sans-serif;
}
html, body {
max-height: 100vh;
max-width: 100vw;
overflow: hidden;
}
body {
display: flex;
flex-direction: column;
}
header {
margin-bottom: 1.5rem;
}
.container {
flex-grow: 1;
}
.container > p {
margin-top: 0;
margin-bottom: 0;
}
JavaScript
let slantLabel = document.querySelector('label[for="slant"]');
let slantInput = document.querySelector('#slant');
let sampleText = document.querySelector('.sample');
function update() {
let slant = `oblique ${slantInput.value}deg`;
slantLabel.textContent = `font-style: ${slant};`;
sampleText.style.fontStyle = slant;
}
slantInput.addEventListener('input', update);
update();
形式化语法
normal | italic | oblique <angle>?
示例
示例展示了不同的 font-style
值。CSS 看起来像这样:
<p class="normal">This paragraph is normal.</p>
<p class="italic">This paragraph is italic.</p>
<p class="oblique">This paragraph is oblique.</p>
.normal {
font-style: normal;
}
.italic {
font-style: italic;
}
.oblique {
font-style: oblique;
}
请注意,不是所有的字体都有确切的 oblique
和 italic
字形,即便如此,浏览器也会通过使用现有的字形来模拟所缺少的字形。下面是一个使用这两种字形渲染字体的示例:
可访问性问题
Large sections of text set with a font-style
value of italic
may be difficult for people with cognitive concerns such as Dyslexia to read.
规范
规范 | 状态 | 注释 |
---|---|---|
CSS Fonts Module Level 4 font-style | Working Draft | Adds the ability to specify an angle after oblique |
CSS Fonts Module Level 3 font-style | Candidate Recommendation | 没有变化 |
CSS Level 2 (Revision 1) font-style | Recommendation | 没有变化 |
CSS Level 1 font-style | Recommendation | 初始定义 |
初始值 | normal |
---|---|
适用元素 | all elements. It also applies to ::first-letter and ::first-line . |
是否是继承属性 | yes |
计算值 | as specified |
Animation type | discrete |
浏览器兼容性
BCD tables only load in the browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.[1] 在 Firefox 44 之前,Gecko 并不对 oblique
和 italic
字形做出区分。在那之后,如果有可用的字形,它将使用正确的字形。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论