• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • font-family

    font-family 允许您通过给定一个有先后顺序的,由字体名或者字体族名组成的列表来为选定的元素设置字体。

    示例

    /* 一个字体族名和一个通用字体族名 */
    font-family: gill sans extrabold, sans-serif;
    font-family: "goudy bookletter 1911", sans-serif;
    
    /* 仅有一个通用字体族名 */
    font-family: serif;
    font-family: sans-serif;
    font-family: monospace;
    font-family: cursive;
    font-family: fantasy;
    font-family: system-ui;
    font-family: emoji;
    font-family: math;
    font-family: fangsong;
    
    /* 全局值 */
    font-family: inherit;
    font-family: initial;
    font-family: unset;
    

    浏览器支持

    IE浏览器火狐浏览器opera浏览器chrome浏览器safari浏览器
    浏览器都支持font-family

    语法:

    font-family:family-name| generic-family
    • family-name:字体名称,按优先顺序排列,以逗号隔开,如果字体名称包含空格或中文,则应使用引号括起。一般字体引用可以不加引号,如果字体名包含了空格、数字或者符号(如连接符)则需加上引号,避免引发错误。
    • generic-family:字体序列名称。序列可包含嵌入字体。请参阅@font-face规则。useragent会遍历定义的字体序列,直到匹配到某个字体为止。例如:body{font-family: helvetica, verdana, sans-serif;}。如上字体定义,假设你机器上没有helvetica字体,但有verdana,这时将会以verdana显示你的文字。
    • 对应的脚本特性为fontFamily

    取值:

    • family-name:一个字体族的名字。例如"Times"和"Helvetica"都是字体族名。字体族名可以包含空格,但包含空格时应该用引号。
    • generic-family:通用字体族名是一种备选机制,用于在指定的字体不可用时给出较好的字体。通用字体族名都是关键字,所以不可以加引号。在列表的末尾应该至少有一个通用字体族名。以下是该属性可能的取值以及他们的定义。
      • serif:带衬线字体,笔画结尾有特殊的装饰线或衬线。例如:Lucida Bright,Lucida Fax,Palatino,"Palatino Linotype",Palladio,"URW Palladio",serif.
      • sans-serif:无衬线字体,即笔画结尾是平滑的字体。例如,"Open Sans","Fira Sans","Lucida Sans","Lucida Sans Unicode","Trebuchet MS","Liberation Sans","Nimbus Sans L",sans-serif.
      • monospace:等宽字体,即字体中每个字宽度相同。例如,"Fira Mono","DejaVu Sans Mono",Menlo,Consolas,"Liberation Mono",Monaco,"Lucida Console",monospace.
      • cursive:草书字体。这种字体有的有连笔,有的还有特殊的斜体效果。因为一般这种字体都有一点连笔效果,所以会给人一种手写的感觉。例如,"Brush Script MT","Brush Script Std","Lucida Calligraphy","Lucida Handwriting","Apple Chancery",cursive.
      • fantasy:主要是那些具有特殊艺术效果的字体。例如,Papyrus,Herculanum,Party LET,Curlz MT,Harrington,fantasy.
      • system-ui:从浏览器所处平台处获取的默认用户界面字体。由于世界各地的排版习惯之间有很大的差异,这个通用选项 is provided for typefaces that don't map cleanly into the other generics.
      • math:针对显示数学相关字符的特殊样式问题而设计的字体:支持上标和下标、跨行括号、嵌套表达式和具有不同含义的 double struck glyph。
      • emoji:专门用于呈现 Emoji 表情符号的字体。
      • fangsong:一种汉字字体,介于宋体和楷体之间。这种字体常用于某些政府文件。

    有效的字体族名

    字体族名或者是引号包括的字符串,或者是不含引号的一个或多个合法标识串构成的。这意味着在没有带引号的字体族名的开头是不能使用标点符号字符和数字字符的。

    例如,以下的声明是有效的:

    font-family: gill sans extrabold, sans-serif;
    font-family: "goudy bookletter 1911", sans-serif;

    以下的声明是无效的

    font-family: goudy bookletter 1911, sans-serif;
    font-family: red/black, sans-serif;
    font-family: "lucida" grande, sans-serif;
    font-family: ahem!, sans-serif;
    font-family: test@foo, sans-serif;
    font-family: #pound, sans-serif;
    font-family: hawaii 5-0, sans-serif;
    默认值由useragent决定
    适用于所有元素
    继承性
    动画性
    计算值指定值

    例子

    一些通用字体

    .serif {
      font-family: times, times new roman, georgia, serif;
    }
    
    .sansserif {
      font-family: verdana, arial, helvetica, sans-serif;
    }
    
    .monospace {
      font-family: lucida console, courier, monospace;
    }
    
    .cursive {
      font-family: cursive;
    }
    
    .fantasy {
      font-family: fantasy;
    }
    
    .emoji {
      font-family: emoji;
    }
    
    .math {
      font-family: math;
    }
    
    .fangsong {
      font-family: fangsong;
    }
    
    <div class="serif">
      this is an example of a serif font.
    </div>
    
    <div class="sansserif">
      this is an example of a sans-serif font.
    </div>
    
    <div class="monospace">
      this is an example of a monospace font.
    </div>
    
    <div class="cursive">
      this is an example of a cursive font.
    </div>
    
    <div class="fantasy">
      this is an example of a fantasy font.
    </div>
    
    <div class="math">
      this is an example of a math font.
    </div>
    
    <div class="emoji">
      this is an example of an emoji font.
    </div>
    
    <div class="fangsong">
      this is an example of a fangsong font.
    </div>

    上篇:font-size

    下篇:font-stretch