使用 :hover 伪类和 font-weight 属性可实现文字悬停加粗,1. 默认设 font-weight: normal;2. .text:hover 中设 font-weight: bold;3. 可添加 transition 实现平滑过渡,注意字体需支持对应字重。

要实现CSS元素在悬停时文字加粗,可以通过 :hover 伪类配合 font-weight 属性来完成。默认情况下文字为正常粗细,鼠标移入时变为加粗,移出后恢复,整个过程无需JavaScript,纯CSS即可实现。
使用 :hover 选择器定义鼠标悬停状态下的样式,通过设置 font-weight 的值来控制文字粗细变化。
常见 font-weight 取值:以下是一个简单的按钮文字悬停加粗效果:
<style>
.text {
font-weight: normal;
transition: font-weight 0.3s ease; /* 可选:添加平滑过渡 */
}
<p>.text:hover {
font-weight: bold;
}
</style></p><p><p class="text">将鼠标放上来,文字会变粗</p></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1267">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680158098827.jpg" alt="ImagetoCartoon">
</a>
<div class="aritcle_card_info">
<a href="/ai/1267">ImagetoCartoon</a>
<p>一款在线AI漫画家,可以将人脸转换成卡通或动漫风格的图像。</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="ImagetoCartoon">
<span>106</span>
</div>
</div>
<a href="/ai/1267" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="ImagetoCartoon">
</a>
</div>
直接切换 font-weight 可能显得生硬,加入 transition 可使加粗过程更流畅。
推荐写法:某些字体可能不支持所有 font-weight 数值,导致加粗无效。建议使用系统常用字体或确保自定义字体包含对应字重文件。
如果字体本身只有 normal 和 bold 两种字重,中间值如 500 将不会生效或被自动映射。
基本上就这些,不复杂但容易忽略细节。
以上就是css元素在悬停时文字加粗如何实现_使用:hover和font-weight调整的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号