【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>
相关推荐
姜太小白3 小时前
【前端】CSS Grid布局介绍及示例
前端·css
小杨同学yx8 小时前
前端三剑客之Css---day3
前端·css
cos14 小时前
FE Bits 前端周周谈 Vol.1|Hello World、TanStack DB 首个 Beta 版发布
前端·javascript·css
斯~内克17 小时前
前端CSS重绘与重排深度解析:从原理到优化实战
前端·css
琹箐18 小时前
CSS font-weight:500不生效
前端·css
Suppose18 小时前
[css]切角
css
典学长编程19 小时前
前端开发(HTML,CSS,VUE,JS)从入门到精通!第二天(CSS)
前端·javascript·css·html
Suppose19 小时前
[css]旋转流光效果
css
姜太小白21 小时前
【前端】CSS Flexbox布局示例介绍
前端·css
是你的小橘呀1 天前
【CSS】揭秘 CSS 浮动:让元素乖乖排队的 "魔法咒语"
css