前端必知:8.字体的设置

当涉及到CSS字体设置时,有很多属性可以用来调整字体的外观和样式。以下是常用的CSS字体设置属性及其用法示例:

  1. font-family: 设置字体系列。
body {
  font-family: Arial, sans-serif;
}

  1. font-size: 设置字体大小。
h1 {
  font-size: 24px;
}

  1. font-weight: 设置字体粗细。
p {
  font-weight: bold;
}

  1. font-style: 设置字体样式(常用的有斜体)。
em {
  font-style: italic;
}

  1. text-decoration: 设置文本修饰线(如下划线)。
a {
  text-decoration: underline;
}

  1. text-transform: 设置文本转换(如大写、小写)。
h2 {
  text-transform: uppercase;
}

  1. letter-spacing: 设置字母间距。
h3 {
  letter-spacing: 2px;
}

  1. line-height: 设置行高。
p {
  line-height: 1.5;
}

  1. word-spacing: 设置单词间距。
p {
  word-spacing: 4px;
}

  1. font-variant: 设置字体变体(如小型大写字母)。
h4 {
  font-variant: small-caps;
}

  1. font-stretch: 设置字体拉伸(如压缩或扩展)。
p {
  font-stretch: condensed;
}

  1. font-size-adjust: 设置字体大小调整(用于优化字体的显示)。
body {
  font-size-adjust: 0.5;
}

  1. font-smooth: 设置字体平滑(用于控制字体在屏幕上的平滑程度)。
body {
  font-smooth: always;
}

  1. text-shadow: 设置文本阴影。
h1 {
  text-shadow: 2px 2px 4px #000000;
}

  1. text-align: 设置文本对齐方式。
p {
  text-align: center;
}

  1. white-space: 设置空白处理方式(如换行)。
pre {
  white-space: pre-wrap;
}

  1. text-overflow: 设置文本溢出处理方式。
div {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

  1. line-break: 设置换行规则。
p {
  line-break: strict;
}

  1. hyphens: 设置断词规则。
p {
  hyphens: auto;
}

  1. font-display: 设置字体加载行为。
@font-face {
  font-family: 'CustomFont';
  src: url('custom-font.woff2') format('woff2');
  font-display: swap;
}

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

这些是一些常用的CSS字体设置属性,可以帮助你自定义字体的外观和样式。你可以根据需求使用这些属性来创建所需的字体效果。

相关完整代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字体设置</title>
    <style>
        .txt1{
            font-style: normal;
            /* 设置字体不影响其他的设置 */
            color: aqua;
            font-weight: 400;
            font-size: 20px;
        }
        .txt2{
            font-style: italic;
            /* 加粗 */
            font-weight:400;
            font-size: 12px;
        }
        .txt3{
            color: #04be02;
            font-size: 40px;
            /* 加逗号,先使用前面的,前面的找不到再使用后面的 */
            font-family:'楷体','华文琥珀','serif';
        }
        .txt4{
            color: #04be02;
            font-size: 40px;
            /* 加逗号,先使用前面的,前面的找不到再使用后面的 */
            font-family:'serif','楷体';
        }
        .txt5{
            color: #04be02;
            font-size: 40px;
            font: normal bold 30px '华文琥珀','楷体';
            font: 40px '微软雅黑';
        }
    </style>
</head>
<body>
    <!-- 
     font-style  设置字体的样式
     值:
         normal  字体正常
         italic  设置字体为斜体
    
    font-weight  设置字体是否加粗
    值:  
          normal 默认
          bold 加粗
          bolder 相对bold加粗 
          他的值也可以是具体的整百数字,范围是100-900
          400 -> normal
          700 ->  bold
          900 -> bolder
    font-size  设置所修饰的字体的大小
          1.目前浏览器默认字体大小是16px
          2.pc浏览器能够接受的最小字体大小为12px
    font-family  设置文字的字体
           可以同时设置多个字体,形如:
           font-family:'a','b','c'....;
           含义是 在客户端的设备上匹配a字体,如果没有a字体,则继续匹配b字体
           依次进行匹配,直到匹配成功即可显示匹配到的字体。
        通用字体
           1.serif  有衬线(类似于锐化)
           2.sans-serif  无衬线

    font 也是一个复合型css属性 
           可以按照下面的顺序书写
           font:style  weight size family;
           注意:style和weight不是必须写入的内容,如果简写必须
           需要有 font-size 和font-family
           font:size family;
    
    
     -->
    <em class="txt1">驱除鞑虏,恢复中原</em>
    <p class="txt2">驱除鞑虏,恢复中原</p>
    <p class="txt3">西安事变</p>
    <p class="txt4">西安事变</p>
    <p class="txt5">百团大战</p>
</body>
</html>

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小姚学前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值