【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>
相关推荐
球球pick小樱花8 小时前
游戏官网前端工具库:海内外案例解析
前端·javascript·css
AAA阿giao1 天前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
掘金安东尼2 天前
用 CSS 打造完美的饼图
前端·css
掘金安东尼2 天前
纯 CSS 实现弹性文字效果
前端·css
前端Hardy2 天前
HTML&CSS&JS:打造丝滑的3D彩纸飘落特效
前端·javascript·css
前端Hardy2 天前
HTML&CSS&JS:丝滑无卡顿的明暗主题切换
javascript·css·html
parade岁月3 天前
Tailwind CSS v4 — 当框架猜不透你的心思
前端·css
前端Hardy3 天前
HTML&CSS&JS:基于定位的实时天气卡片
javascript·css·html
程序员阿耶4 天前
5 个让 CSS 起飞的新特性,设计师看了直呼内行
css
前端Hardy4 天前
HTML&CSS:纯CSS实现随机转盘抽奖机——无JS,全靠现代CSS黑科技!
css·html