在本章中,我们将讨论字体。
CSS 字体属性用于定义文本的大小、字体系列、粗体和样式。
此外,它将元素的字体设置为系统字体。
CSS font 属性被认为是速记属性。
所以,在这里我们将解释以下属性:
- 字体系列
- 字体样式
- 字体大小
- 字体粗细
- 字体变体
当我们在网页上写一些信息、一些文字或者链接时,我们必须给它们样式。
我们将上面提到的属性用于文本样式。
让我们更多地了解这些属性。
字体样式
我们主要将 font-style 属性用于要写斜体的文本。
最重要的是,我们使用 font-style 属性的以下值:
- normal,正常显示文本,
- italic ,以斜体显示文本,
- oblique,“倾斜”文本。
font-style 属性示例:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <p style="font-style:normal">This is paragraph with font-style property with normal value.</p> <p style="font-style:italic">This is paragraph with font-style property with italic value.</p> <p style="font-style:oblique">This is paragraph with font-style property with oblique value.</p> </body> </html>
默认情况下,我们的文本将是 font-style: normal,即使不写这个值。
字体粗细
现在我们知道,当我们有一些文本,并且想要将文本设为斜体时,我们使用 font-style 属性。
现在让我们了解我们可以做些什么来使我们的文本更粗,以防它不是标题,因为默认情况下标题是粗体的。
为此,我们使用 font-weight 属性。
大多数情况下,我们使用 font-weight proparty 的以下值:
- normal
- bold
该值也可以由数字给出。
以 font-weight 属性为例:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <p style="font-weight:900">Some paragraph with the font-weight property with value bold.</p> <p style="font-weight:normal">Some paragraph with the font-weight property with value normal.</p> <p style="font-weight:bold">Some paragraph with the font-weight property with value bold.</p> </body> </html>
正如我们所记得的,我们说过我们可以用 100-900 的数字来赋予价值。
值 100 是正常的,而 900 是更粗的。
随着数字在 100 到 900 之间增长,字体变得更粗。
字体变体
font-variant 属性允许将文本设置为字体系列中的普通或者小型大写字母。
small-caps 值使小写字母变为大写字母,但这些转换后的字母会比普通的大写字母显示得小一点。
font-variant 属性示例:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> .smallcaps { font-variant: small-caps; } </style> </head> <body> <h2>Font-variant 属性示例</h2> <p>Here we used a normal text as you can see.</p> <p class="smallcaps">But We Used A Text With Small-Caps Here.</p> </body> </html>
字体系列
对于文本的字体系列,我们使用 font-family 属性。
这是我们要在网页中使用的字体名称。
p { font-family: Arial, sans-serif; }
如我们所见,我们已赋予 <p> 标签 font-family 属性。
我们区分两种类型的家庭:
- 通用系列 - 一组外观相似的字体系列(如“Serif”或者“Sans-serif”)
- 字体系列 - 特定的字体系列(如“Times New Roman”或者“Arial”)
当我们使用由多个单词组成的特定字体系列时,我们会将名称放在引号中(例如“Times New Roman”)。
同时,我们必须编写多个字体系列,因为万一浏览器不支持第一个字体系列,我们可以拥有其他几个类似的字体系列。
字体大小
对于文本的大小,我们使用 font-size 属性。
我们需要为我们的财产赋予价值。
这些值由像素、pt、em、rem 等给出。
我们可以在 font-size 部分找到完整列表。
你需要知道1em=16px。
大多数情况下,我们使用像素(px)。
字体大小属性的示例:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <h3 style="font-size:40px;"> Some heading with font-size property.</h3> <p style="font-size:25px;"> Some paragraph with font-size property.</p> <a style="font-size:2em;"> Some link with font-size property.</a> </body> </html>
你也需要自己检查一下。
我们可以使用我们的 html 在线编辑器点击这里。