【CSS技巧】给按钮添加下划线动画

按钮或菜单项添加 hover 时下划线动画,可以给界面增色不少。

一种巧妙的方式是通过after伪元素来实现(在线示例):

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>HTML + CSS</title>
    <link rel="stylesheet" href="styles.css" />
    <style>
      .c-text-button {
        position: relative;
        display: inline-flex;
        align-items: center;
        padding: 2px 4px;
        cursor: pointer;
        font-size: 14px;
        transition: all 0.3s ease;
      }
      .c-text-button::after {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 0;
        height: 1px;
        background-color: currentcolor;
        content: "";
        transition: width 0.3s ease;
      }
      .c-text-button:hover {
        opacity: 1;
        transform: translateY(-1px);
      }
      .c-text-button:hover::after {
        width: 100%;
      }

    </style>
  </head>
  <body>
    <div class="c-text-button">button</div>
  </body>
</html>
相关推荐
treasure14013 小时前
【复古】浅尝Django前后端不分离
前端·css·python·django·bootstrap·html·web
ZhengEnCi21 小时前
前端项目为什么需要一份统一的 theme.css:用 CSS 变量管住全局样式与主题切换
前端·css·前端工程化
牧艺2 天前
弹层不用再手算坐标了:CSS Anchor Positioning 从原理到可上线
前端·javascript·css
程序员大阳2 天前
HTML+CSS+JS对话式实战(7)-学生端活动报名功能开发
javascript·css·html·活动报名
程序员大阳3 天前
HTML+CSS+JS对话式实战(5)-活动管理页之查看报名名单的实现
javascript·css·html·查看报名名单
曦樂~3 天前
【Web Development】02 CSS
前端·css
东鹏特饮3 天前
经典打砖块(Breakout)游戏
前端·css·css3
程序员大阳4 天前
HTML+CSS+JS对话式实战(9)-项目总结与问题分析
javascript·css·html·实战·项目总结
程序员大阳4 天前
HTML+CSS+JS对话式实战(6)-学生端查看活动功能开发
javascript·css·html·模块·查看学生
程序员大阳7 天前
HTML+CSS+JS对话式实战(3)-活动管理页实现查询、新增、删除API后端接口
javascript·css·html·实战·删除·查询·新增