【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>
相关推荐
不会玩电脑的Xin.1 小时前
HTML + CSS
前端·css·html
悟能不能悟11 小时前
<style scoped>vue中怎么引用css文件
css·vue.js
粉末的沉淀19 小时前
css:制作带边框的气泡框
前端·javascript·css
西游音月20 小时前
(4)pytest+Selenium自动化测试-元素定位之CSS Selector定位
css·selenium·pytest
谢尔登1 天前
【CSS】样式隔离
前端·css
LQW_home1 天前
前端展示 接受springboot Flux数据demo
前端·css·css3
WebGirl1 天前
一个 CSS 属性aspect-ratio
css
xump1 天前
如何在DevTools选中调试一个实时交互才能显示的元素样式
前端·javascript·css
Lhuu(重开版2 天前
CSS:动效布局动画
前端·css
不羁的fang少年2 天前
前端常见问题(vue,css,html,js等)
前端·javascript·css