Skip to content

实现小于 12px 的字体

浏览器默认的最小字号是 12px,就算你设置了 10 px,它依然会按 12px 进行显示。那么有哪些方式可以实现呢?

效果图如下

通过 zoom 方式实现

默认相对该元素左上角进行缩放,不可为负数值。该元素所占据的空间会发生变化。

html
<!DOCTYPE html>
<html lang="en">
    <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>Document</title>
        <style>
            .text1 {
                font-size: 12px;
            }

            .text2 {
                font-size: 12px;
                zoom: 0.833333;
            }
        </style>
    </head>
    <body>
        <div class="text1">我是 12 号字体</div>
        <div class="text2">我是 10 号字体</div>
    </body>
</html>
<!DOCTYPE html>
<html lang="en">
    <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>Document</title>
        <style>
            .text1 {
                font-size: 12px;
            }

            .text2 {
                font-size: 12px;
                zoom: 0.833333;
            }
        </style>
    </head>
    <body>
        <div class="text1">我是 12 号字体</div>
        <div class="text2">我是 10 号字体</div>
    </body>
</html>

通过 transform scale 方式实现

默认相对该元素中间部分进行缩放,可通过 transform-origin 来改变。可为负数值。该元素所占据的空间不会发生变化。该方式更适合用来实现动画。

html
<!DOCTYPE html>
<html lang="en">
    <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>Document</title>
        <style>
            .text1 {
                font-size: 12px;
            }

            .text2 {
                font-size: 12px;
                transform: scale(0.833333);
                transform-origin: left;
            }
        </style>
    </head>
    <body>
        <div class="text1">我是 12 号字体</div>
        <div class="text2">我是 10 号字体</div>
    </body>
</html>
<!DOCTYPE html>
<html lang="en">
    <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>Document</title>
        <style>
            .text1 {
                font-size: 12px;
            }

            .text2 {
                font-size: 12px;
                transform: scale(0.833333);
                transform-origin: left;
            }
        </style>
    </head>
    <body>
        <div class="text1">我是 12 号字体</div>
        <div class="text2">我是 10 号字体</div>
    </body>
</html>

更新时间: