Home  >  Article  >  Web Front-end  >  How many css font styles are there?

How many css font styles are there?

青灯夜游
青灯夜游Original
2022-08-29 18:55:595439browse

css font styles include: 1. "font-family", which specifies the font family of the text; 2. "font-size", which specifies the font size style of the text; 3. "font-style", which specifies the text The font style of Stretch or compress to make a font's characters wider or narrower.

How many css font styles are there?

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

Font setting is an important part of web design. Appropriate fonts will not only make the page more beautiful, but also improve the user experience. CSS provides a series of properties for setting text font styles, such as changing fonts, controlling font size and thickness, and so on.

1. font-family

The font-family attribute is used to set the font of the text within the element. Since there are thousands of fonts, and some are not free, it is almost impossible to have all the fonts on our computers. In order to ensure that the fonts we set can be displayed normally, you can define a list of several font names through the font-family attribute. Use commas to separate the font names. The browser will first try the first font in the list. , if not supported then try the next one, and so on.

Optional values ​​for the font-family attribute are as follows:

ValueDescription
family-name,
generic-family
family-name: font name, a font name represents a font, such as "Microsoft Yahei" is a font;
generic-family : Font family, that is, a certain type of font combination. A font family represents a type of font, which contains many similar but different fonts. For example, "sans-serif" is a sans-serif font, which contains many similar fonts.
Default value of font depends on browser settings
inheritInherits font settings from parent element

The following table lists some commonly used font families (generic-family):

Font familyDescriptionFont
serifA serif font, that is, adding special decorative lines or serifs at the end of the text strokes"Lucida Bright"," Lucida Fax", Palatino, "Palatino Linotype", Palladio, "URW Palladio", serif
sans-serif Sans serif font, that is, at the end of the text strokes Everything is smooth"Open Sans", "Fira Sans", "Lucida Sans", "Lucida Sans Unicode", "Trebuchet MS", "Liberation Sans", "Nimbus Sans L", sans-serif
monospaceMonospace font, that is, the width of each text is the same"Fira Mono", "DejaVu Sans Mono", Menlo, Consolas, "Liberation Mono", Monaco, "Lucida Console", monospace
cursive cursive font, this font has continuous strokes or special italic effects, It will give people a handwriting feeling"Brush Script MT", "Brush Script Std", "Lucida Calligraphy", "Lucida Handwriting", "Apple Chancery", cursive
fantasyFonts with special artistic effectsPapyrus, Herculanum, "Party LET", "Curlz MT", Harrington, fantasy

[Example] Use the font-family attribute to set the font style for HTML elements:




    CSS字体
    


    

font-family 属性

The running result is as shown below:

How many css font styles are there?

Note: If the font family or font name contains spaces or multiple words, they must be wrapped in quotation marks, such as "Times New Roman", "Courier New", "Segoe UI", etc., if it is in the element Single quotes must be used when used in the style attribute.

The most commonly used font families in web design are serif and sans-serif because they are suitable for reading. When displaying some program codes, fixed-width fonts are usually used, so that the program code looks neater.

2. font-style

The font-style attribute is used to set the font style, such as italic, slant, etc. The optional values ​​​​of this attribute are as follows:

ValueDescription
normalDefault value, the text is displayed in normal font
italicText is italic
obliqueText is italic
inheritInherit font style from parent element

【示例】使用 font-style 属性设置字体的样式:




    CSS字体
    


    

normal:显示一个标准的字体

italic:显示一个斜体的字体

oblique:显示一个倾斜的字体

inherit:从父元素继承字体样式

运行结果如下图所示:

How many css font styles are there?

乍看之下,您可能觉得 italic 和 oblique 的效果是一样的。其实不然,italic 显示的字体的斜体版本,而 oblique 则只是一个倾斜的普通字体。

3、font-weight

font-weight 属性能够设置字体的粗细,可选值如下:

描述
normal默认值,标准字体
bold粗体字体
bolder更粗的字体
lighter更细的字体
100、200、300、400、500、600、700、800、900由细到粗的设置字体粗细,100 为最细的字体,400 等同于 normal,700 等同于 bold
inherit从父元素继承字体的粗细

【示例】使用 font-weight 属性设置字体粗细:




    CSS字体
    


    

font-weight: 100;

font-weight: 200;

font-weight: normal;

font-weight: bold;

font-weight: bolder;

运行结果如下图所示:

How many css font styles are there?

4、font-size

font-size 属性用来设置字体的大小(字号),可选值如下:

描述
xx-small、x-small、small、medium、large、x-large、xx-large以关键字的形式把字体设置为不同的大小,从 xx-small 到 xx-large 依次变大,默认值为 medium
smaller为字体设置一个比父元素更小的尺寸
larger为字体设置一个比父元素更大的尺寸
length以数值加单位的形式把字体设置为一个固定尺寸,例如 18px、2em
%以百分比的形式为字体设置一个相对于父元素字体的大小
inherit从父元素继承字体的尺寸

【示例】使用 font-size 属性设置字体的大小:




    CSS字体
    


    

将字体大小设置为:xx-small

将字体大小设置为:x-small

将字体大小设置为:x-small

将字体大小设置为:medium

将字体大小设置为:large

将字体大小设置为:x-large

将字体大小设置为:xx-large

将字体大小设置为:smaller

将字体大小设置为:larger

将字体大小设置为 20 像素

运行结果如下图所示:

How many css font styles are there?

5、font-variant

font-variant 属性可以将文本中的小写英文字母转换为小型大写字母(转换后的大写字母与转换前小写字母的大小相仿,所以称之为小型大写字母)。font-variant 属性的可选值如下:

描述
normal默认值,浏览器会显示一个标准的字体
small-caps将文本中的小写英文字母转换为小型大写字母
inherit从父元素继承 font-variant 属性的值

【示例】使用 font-variant 属性设置小型大写字母:




    CSS字体
    


    

This is a paragraph

This is a paragraph

运行结果如下图所示:

How many css font styles are there?

6、font

font 属性与前面价绍的 background 属性的功能类似,通过 font 属性可以同时设置多个字体属性,不同的是,使用 font 属性需要遵循以下顺序:

font:[[font-style||font-variant||font-weight||font-stretch]?font-size[ /line-height]?font-family] | caption | icon | menu | message-box | small-caption | status-bar
描述
font-style规定字体样式。参阅:font-style 中可能的值。
font-variant规定字体异体。参阅:font-variant 中可能的值。
font-weight规定字体粗细。参阅:font-weight 中可能的值。
font-size/line-height规定字体尺寸和行高。参阅:font-sizeline-height 中可能的值。
font-family规定字体系列。参阅:font-family 中可能的值。
caption定义被标题控件(比如按钮、下拉列表等)使用的字体。
icon定义被图标标记使用的字体。
menu定义被下拉列表使用的字体。
message-box定义被对话框使用的字体。
small-captioncaption 字体的小型版本。
status-bar定义被窗口状态栏使用的字体。

在使用 font 属性时,有以下几点需要注意:

  • 使用 font 属性时必须按照如上所示的顺序,并且 font-size 和 font-family 两个属性不可忽略;

  • font 属性中的每个参数仅允许设置一个值,除 font-size 和 font-family 属性外,被忽略的属性将被设置为各自的默认值;

  • 若要定义 line-height 属性,则需要使用斜线/将 font-size 和 line-height 属性分开。

【示例】使用 font 属性同时定义多个字体效果:




    CSS字体
    


    

使用 font 属性需要遵循以下顺序:

font:[[font-style||font-variant||font-weight||font-stretch]?font-size[ /line-height]?font-family] | caption | icon | menu | message-box | small-caption | status-bar

运行结果如下图所示:

How many css font styles are there?

7、@font-face

以前在给网页文字设置一些好看的字体时,限于用户系统是否安装此字体,而只能使用三种方法解决,要么用通用字体,要么用图片替换文本,要么用Flash。而这几种方法却存在严重的缺陷。

现在好了,@font-face终于解决了这种使用网络字体的问题。

@font-face 用于从远程服务器或者用户本地加载指定的字体。

浏览器兼容性问题

How many css font styles are there?

其实,@font-face并不是CSS3才出来的属性,早在1998年它就在CSS2中使用了,但是在CSS2.1中又被除去了,CSS3又把它加了进来。

@font-face语法

@font-face {
    font-family: <字体名>;
    src: <字体路径> [<格式>][,<字体路径> [<格式>]]*;
    [font-weight: <粗细>];
    [font-style: <样式>];
}

取值说明:

  • 字体名:此值指的就是你自定义的字体名称,最好是使用你下载的默认字体,他将被引用到你的Web元素中的font-family。如“font-family:”字体名”;”

  • 字体路径:此值指的是你自定义的字体的存放路径,可以是相对路径也可以是绝路径;

  • 格式:此值指的是你自定义的字体的格式,主要用来帮助浏览器识别,其值主要有以下几种类型:truetype,opentype,truetype-aat,embedded-opentype,avg等;

  • weight和style:这两个值大家一定很熟悉,weight定义字体是否为粗体,style主要定义字体样式,如斜体。

属性描述
font-familyname必需。规定字体的名称。
srcURL必需。定义字体文件的 URL。
font-stretchnormal
condensed
ultra-condensed
extra-condensed
semi-condensed
expanded
semi-expanded
extra-expanded
ultra-expanded
可选。定义如何拉伸字体。默认是 “normal”。
font-stylenormal
italic
oblique
可选。定义字体的样式。默认是 “normal”。
font-weightnormal
bold
100
200
300
400
500
600
700
800
900
可选。定义字体的粗细。默认是 “normal”。
unicode-rangeunicode-range可选。定义字体支持的 UNICODE 字符范围。默认是 “U+0-10FFFF”。

@font-face小例子

Lecepin's Blog

效果:

How many css font styles are there?

代码块中,font-familysrc是必需的。src中的local()表是从本地系统查找字体,如果找不到,再从url()指定的查找。

format()指的是字体的格式,常用字体格式如下:

格式说明
TureTpe(.ttf)格式.ttf字体是Windows和Mac的最常见的字体,是一种RAW格式,因此他不为网站优化,支持这种字体的浏览器有【IE9+,Firefox3.5+,Chrome4+,Safari3+,Opera10+,iOS Mobile Safari4.2+】;
OpenType(.otf)格式.otf字体被认为是一种原始的字体格式,其内置在TureType的基础上,所以也提供了更多的功能,支持这种字体的浏览器有【Firefox3.5+,Chrome4.0+,Safari3.1+,Opera10.0+,iOS Mobile Safari4.2+】;
Web Open Font Format(.woff)格式.woff字体是Web字体中最佳格式,他是一个开放的TrueType/OpenType的压缩版本,同时也支持元数据包的分离,支持这种字体的浏览器有【IE9+,Firefox3.5+,Chrome6+,Safari3.6+,Opera11.1+】;
Embedded Open Type(.eot)格式.eot字体是IE专用字体,可以从TrueType创建此格式字体,支持这种字体的浏览器有【IE4+】;
SVG(.svg)格式.svg字体是基于SVG字体渲染的一种格式,支持这种字体的浏览器有【Chrome4+,Safari3.1+,Opera10.0+,iOS Mobile Safari3.2+】。
How many css font styles are there?

src format属性兼容写法

关于兼容各个浏览器的兼容写法,可以参考一下一个国外大神Paul Irish写的兼容代码:

   @font-face {
    font-family: '字体名';
    src: url('字体名.eot'); /* IE9 兼容模式 */
    src: url('字体名.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
             url('字体名.woff') format('woff'), /* 现代浏览器 */
             url('字体名.ttf')  format('truetype'), /* Safari, Android, iOS */
             url('字体名.svg#grablau') format('svg'); /* Legacy iOS */
   }

通常来说,有.woffeot这两种就够了。

关于这个兼容写法,请参考:Bulletproof @font-face Syntax

字体格式转换工具

当你只有一种字体格式文件的时候,可以使用如下在线格式转换工具,生成其它格式字体文件:

Font Squirrel:https://www.fontsquirrel.com/tools/webfont-generator

@font-face generator:http://fontface.codeandmore.com/

Google Fonts:https://fonts.google.com/

8、font-size-adjust

CSS 中的 font-size-adjust 属性允许开发者基于小写字母的高度指定 font-size ,这可以有效地提高网页文字的可读性。

在这里,你不仅能了解到 font-size-adjust 属性的重要性,并且还能学会如何在你的项目中使用它。

font-size-adjust 的重要性

你访问的网站大多都是由文本组成的,由于书面文字是网站的重要组成部分,因此就很值得把注意力放到你用来显示信息的字体上面。选对正确的字体能带给用户愉快的阅读体验,然而,使用不恰当的字体则会使网站变得难以阅读。当你决定将要使用什么字体后,一般你就会再给这个字体选择一个合适的大小。

font-size 属性会设置网页中所有 font-family 下你想使用的字体的大小,然而在大多数情况下,浏览器一般都是使用 font-family 下声明的第一种字体。只有当第一种字体因为某些原因不可用时,浏览器才会使用候选字体继续渲染页面。

举个例子,看下面的代码:

body {
  font-family: 'Lato', Verdana, sans-serif;
}

如果你的浏览器从 Google Fonts 下载的 ‘Lato’ 字体不可用时,在这种情况下,Verdana 字体就会被使用。但是,脑海里 font-size 的值好像是针对 ‘Lato’ 字体设定的,而不是 Verdana。

什么是字体的纵横比?

字体的外观尺寸及其可读性可能会因为 font-size 的值而产生很大的变化,特别像是对拉丁文这种文字会导致其在大小写之间差别巨大。在这种情况下,小写字母与对应的大写字母的高度比例是决定一种字体易读性的重要因素,这个比值通常被叫做一种字体的纵横比

正如我之前说的,一旦你设置了 font-size 的值,这个值将会对所有的字体起作用。如果候选字体的纵横比跟首选字体的纵横比相差太大,这可能影响候选字体的易读性。

font-size-adjust 属性在这种情形下则扮演着一个尤为重要的角色,因为它允许你设置所有字体的 x 轴高度 为统一大小,以便提高文字的易读性。

给 font-size-adjust 属性选择合适的值

现在你知道使用 font-size-adjust 属性的重要性了吧,是时候把它用到你的网站上了。这个属性的语法如下:

font-size-adjust: none | 

none 是默认值,这个值意味着不调整字体的大小。

你也可以设置属性的值为一个数字,这个数字将用来计算一张网页上所有字体的 x 轴高度,x 轴高度等于这个数字乘以 font-size 的值。 这可以提高小尺寸字体的可读性。以下是一个使用 font-size-adjust 属性的例子:

font-size: 20px;font-size-adjust: 0.6;

所有字体的 x 轴高度现在是 20px * 0.6 = 12px,一种字体的实际大小现在可以被修改以确保 x 轴高度总是等于 12px。调整后 font-size 的值可以通过以下公式计算

c = ( a / a' ) s.

这里, c 指调整后的 font-sizes 指原先指定的 font-size,a 是 font-size-adjust 属性指定的纵横比,a' 指实际字体的纵横比。

你不能设置 font-size-adjust 的值为负数,设置为 0 则会致使文字没有高度,换句话说,就是文字会被隐藏。在旧的浏览器中,例如 Firefox 40,如果设置其属性值为 0 则相当于设置为 none

大多数情况下,开发者一般会尝试不同的 font-size 取值以确定哪个值对给定的字体最好看。这意味着在理想情况下,他们希望所有字体的 x 轴高度与首选字体的 x 轴高度相等。换句话说,最合适的 font-size-adjust 取值就是你首选字体的纵横比。

如何计算一种字体的纵横比

要确定一种字体合适的纵横比,你可以凭实际经验就是调整后的字体大小应该跟原来声明的字体大小一样。这就是说上面公式中的 a 应该跟 a' 相等。

计算纵横比的第一步是先创建 2 个 元素,每个 元素将会包含一个字母和一个包围着字母的边框(因为我们要进行比较,所以每个 中的字母都必须相同)。同时,每个元素的 font-size 属性值都应该相同,但只有一个元素会使用 font-size-adjust 属性。当 font-size-adjust 的值等于给定字体的纵横比时,每个 下的字母都是一样的大小。

在下面的 demo 中,我创建了一个边框围绕着字母 ‘t’ 和 ‘b’ 并且对每组字母应用了不同的 font-size-adjust 属性值。

以下是相关代码:

.adjusted-a {
  font-size-adjust: 0.4;
}

.adjusted-b {
  font-size-adjust: 0.495;
}

.adjusted-c {
  font-size-adjust: 0.6;
}

正如下面 demo 所示,font-size-adjust 的值越大则字母会显得越大,反之则越小,当该值等于纵横比时,每组字母的尺寸都相等。

演示地址:https://codepen.io/SitePoint/pen/YxxbMp

How many css font styles are there?

在网站上使用 font-size-adjust

以下 demo 使用的 font-size-adjust 取值于上一个 CodePen demo 中为 ‘Lato’ 字体设置的值,现在将会用来调整 ‘Verdana’ 这个候选字体。会有一个按钮控制修改是否发生,所以你可以看出修改前后的变化:

演示地址:https://codepen.io/SitePoint/pen/KvvLOr

How many css font styles are there?

当你处理大量文字时效果会更加引人注目,然而上面的例子应该足够让你认识到这个属性的有用之处。

浏览器支持

目前,只有 Firefox 默认支持 font-size-adjust 属性。Chrome 和 Opera 分别从 43 和 30 版本开始作为试验特性予以支持,开发者需前往 chrome://flags 中开启 “Experimental Web Platform Features” 选项。Edge 和 Safari 不支持这个属性。

如果你决定使用这个属性,低版本浏览器的支持将不成问题,这个属性被设计时就已经考虑到向后兼容性,不支持的浏览器会正常的显示文本,支持的浏览器则会基于该属性的值调整字体大小。

9、font-stretch

font-stretch属性用来将字体在水平方向上进行拉伸或压缩,让一种字体的字符更宽或更窄。如果水平压缩,则字体变窄,如果水平拉伸,则字体变宽。

语法格式:

font-stretch: wider|narrower|ultra-condensed|extra-condensed|condensed|semi-condensed|normal|semi-expanded|expanded|extra-expanded|ultra-expanded|inherit;
说明
wider使得文本更宽
narrower使得文本窄
ultra-condensed使文本窄得不能再窄
extra-condensed指定紧缩程度第二大的字体
condensed指定略微紧缩程度第二大的字体
semi-condensed指定略微紧缩的字体
normal指明字体既不紧缩也不加宽
semi-expanded指定略微加宽的字体
expanded指定加宽的字体
extra-expanded指定加宽程度第二大的字体
ultra-expanded指定加宽程度最大的字体
inherit指定该属性与元素父项的属性采用相同的计算值

就像 font-size 属性的预定义关键字(如xx-large)一样,该属性也有一系列预定义关键字,这些关键字可以是normal、或condensed、或expanded,默认值为 normal,表示不进行拉伸或压缩。

示例:

/* Keyword values */
font-stretch: ultra-condensed;
font-stretch: extra-condensed;
font-stretch: condensed;
font-stretch: semi-condensed;
font-stretch: normal;
font-stretch: semi-expanded;
font-stretch: expanded;
font-stretch: extra-expanded;
font-stretch: ultra-expanded;

/* Global values */
font-stretch: inherit;
font-stretch: initial;
font-stretch: unset;

How many css font styles are there?

该属性不会通过伸缩缩小任意字体的几何形状。像font-variant。如果它提供了其中的几个,这仅仅是一种选择最合适的字体的方式, 。

注意:如果字体提供了多个面,font-stretch则选择与该属性值最匹配的那个面。例如,在OS X上,除了更为常见的Bold,Regular,Italic和BoldItalic外,“Helvetica Neue”字体还提供了第二组缩放的缩略字体:缩写。浏览器支持font-stretch将使用压缩的值ultra-condensed,以semi-condensed及用于其它正常表面(normal和所有展开的值)。

How many css font styles are there?

如果字体没有浓缩或扩展,如Mac OS上的默认“Times New Roman”,font-stretch则不会有任何可见的效果,因为在所有情况下都将使用唯一合适的。

How many css font styles are there?

(学习视频分享:web前端入门

The above is the detailed content of How many css font styles are there?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn