CSS笔记:CSS 字体属性

1、CSS字体属性

CSS Fonts (字体)属性用于定义字体系列、大小、粗细、和文字样式(如斜体)。

1.1字体:font-family
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS字体属性</title>
    <style>
        h2 {
            font-family: "Microsoft YaHei";
        }
        
        p {
            font-family: "Microsoft YaHei";
        }
    </style>
</head>

<body>
    <h2>断章</h2>
    <p>你站在桥上看风景,</p>
    <p>看风景人在楼上看你。</p>
    <p>明月装饰了你的窗子,</p>
    <p>你装饰了别人的梦。</p>
</body>
  • 各种字体之间必须使用英文状态下的逗号隔开
  • 一般情况下,如果有空格隔开的多个单词组成的字体,加引号.
  • 尽量使用系统默认自带字体,保证在任何用户的浏览器中都能正确显示
  • 最常见的几个字体:body {font-family: ‘Microsoft YaHei’,tahoma,arial,‘Hiragino Sans GB’; }
1.2字体大小:font-size
p { 
 font-size: 20px; 
}
1.3字体粗细:font-weight

font-weight的属性值:normal(默认值不加粗)、bold(粗体)、100-900(400等同于normal,700等同于bold)

p { 
 font-weight: bold; 
}

例子:

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字体粗细</title>
    <style>
        /* font-weight的属性值:normal(默认值不加粗)、bold(粗体)、100-900(400等同于normal,700等同于bold) */
        
        h2 {
            font-weight: bold;
        }
        
        .wei {
            font-weight: normal;
        }
        
        .hei {
            font-weight: 700;
        }
    </style>
</head>

<body>
    <h2>断章</h2>
    <p class="wei">你站在桥上看风景,</p>
    <p class="hei">看风景人在楼上看你。</p>
    <p>明月装饰了你的窗子,</p>
    <p>你装饰了别人的梦。</p>
</body>
1.4、文字样式:font-style

语法:
normal为正常格式,italic为倾斜字体格式

p { 
 font-style: normal;
}

例子:

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字体样式</title>
    <style>
        /* normal为正常格式,italic为倾斜字体格式 */
        
        h4 {
            font-style: normal;
        }
        
        p {
            font-style: italic;
        }
    </style>
</head>

<body>
    <h4>乏了</h4>
    <p>困了</p>
</body>
1.5、复合样式
body { 
 font: font-style font-weight font-size/line-height font-family;
}
  • 使用 font 属性时,必须按上面语法格式中的顺序书写,不能更换顺序,并且各个属性间以空格隔开
  • 不需要设置的属性可以省略(取默认值),但必须保留 font-size 和 font-family 属性,否则 font 属性将不起作用

例子

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字体复合属性</title>
    <style>
        /* font: font-style font-weight font-size/line-height font-family; */
        
        p {
            font: italic 700 30px "Microsoft YaHei";
        }
    </style>
</head>

<body>
    <p>困了</p>
</body>

2、字体属性总结

属性含义注意点
font-size字号需要加上单位px
font-family字体按实际要求写
font-weigth字体粗细normal、bold、100-900(400normal,700bold)
font-style字体样式倾斜:italic 正常:normal
font复合连写按照顺序写:font-style font-weight font-size/line-height font-family;字号和字体(font-size 和 font-family 属性)必须都要有
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值