CSS单位CH用法

前提

身为一只小白,只会学校学的那一点点css的内容,今天想在自己的网站加上一个类似打字的动画,第一次用到ch单位。

按道理

  • 1ch = 1个英文 = 1个数字
  • 2ch = 1个中文

以下代码,在显示英文数字的时候1ch就是等于一个英文一个数字,是没有问题的。可是在显示中文ch长度。以下的动画就效果就出现问题,因为2ch不等于一个中文。

放个代码

<div class="middleBox-title">
  <p>一只抱枕枕</p>
</div>
.middleBox-title p {
    font-size: 1.2 rem;
    border-right: .1em solid;
    width: 10ch;
    white-space: nowrap;
    overflow: hidden;
    animation: typing 2s steps(5, end), blink-caret .3s step-end infinite alternate;
    -webkit-animation: typing 2s steps(5, end), blink-caret .3s step-end infinite alternate;
    margin: -30px 0 0 480px;
    transition: all 0.3s;
}

解决方案

1.修改字符集,中易宋体(Simsun)是好用的。
借鉴https://www.zhihu.com/question/36455024问题的答案。
放个css代码。

* {
    font-family: 'Simsun';
}

2.可是我不喜欢simsun的字体样子嗷!怎么办?改为em。建议使用em
放个代码

* {
    /* font-family: 'Simsun'; */
    font-family: 'Microsoft YaHei';/*微软雅黑 没有商用*/
}
.middleBox-title p {
    font-size: 1.2rem;
    border-right: .1em solid;
    width: 5em;/*12ch;*/
    white-space: nowrap;
    overflow: hidden;
    animation: typing 2s steps(5, end), blink-caret .3s step-end infinite alternate;
    -webkit-animation: typing 2s steps(5, end), blink-caret .3s step-end infinite alternate;
    margin: -30px 0 0 480px;
    transition: all 0.3s;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值