HTML&CSS:这个动态删除按钮打几分?

这段HTML和CSS代码创建了一个可交互的按钮,该按钮在鼠标悬停时会展开显示一个删除图标和文字标签

演示效果

HTML&CSS

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>前端Hardy</title>
    <style>
        body {
            background: #e8e8e8;
            padding: 0;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }

        .button {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: rgb(20, 20, 20);
            border: none;
            font-weight: 600;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.164);
            cursor: pointer;
            transition-duration: .3s;
            overflow: hidden;
            position: relative;
        }

        .svgIcon {
            width: 12px;
            transition-duration: .3s;
        }

        .svgIcon path {
            fill: white;
        }

        .button:hover {
            width: 140px;
            border-radius: 50px;
            transition-duration: .3s;
            background-color: rgb(255, 69, 69);
            align-items: center;
        }

        .button:hover .svgIcon {
            width: 50px;
            transition-duration: .3s;
            transform: translateY(60%);
        }

        .button::before {
            position: absolute;
            top: -20px;
            content: "Delete";
            color: white;
            transition-duration: .3s;
            font-size: 2px;
        }

        .button:hover::before {
            font-size: 13px;
            opacity: 1;
            transform: translateY(30px);
            transition-duration: .3s;
        }
    </style>
</head>

<body>
    <button class="button">
        <svg viewBox="0 0 448 512" class="svgIcon">
            <path
                d="M135.2 17.7L128 32H32C14.3 32 0 46.3 0 64S14.3 96 32 96H416c17.7 0 32-14.3 32-32s-14.3-32-32-32H320l-7.2-14.3C307.4 6.8 296.3 0 284.2 0H163.8c-12.1 0-23.2 6.8-28.6 17.7zM416 128H32L53.2 467c1.6 25.3 22.6 45 47.9 45H346.9c25.3 0 46.3-19.7 47.9-45L416 128z">
            </path>
        </svg>
    </button>
</body>
</html>

HTML结构

  • button:这是一个按钮元素,用于触发点击事件。
  • svgIcon:这是一个SVG图标,用于在按钮中显示一个图标。viewBox属性定义了SVG画布的尺寸和位置,class="svgIcon"用于应用SVG相关的样式。

CSS样式

  • .button:定义了按钮的基本样式,包括尺寸、边框半径、背景颜色、边框、字体权重、显示方式、对齐方式、盒子阴影、光标样式、过渡效果、溢出隐藏和定位。
  • .svgIcon:定义了SVG图标的基本样式,包括宽度和过渡效果。
  • .svgIcon path:定义了SVG图标路径的填充颜色。
  • .button:hover:定义了鼠标悬停在按钮上时的样式,包括宽度、边框半径、背景颜色、对齐方式和过渡效果。
  • .button:hover .svgIcon:定义了鼠标悬停在按钮上时SVG图标的样式,包括宽度、过渡效果和Y轴变换。
  • .button::before:使用了伪元素::before来在按钮上添加一个隐藏的文本标签,包括位置、内容、颜色、过渡效果和字体大小。
  • .button:hover::before:定义了鼠标悬停在按钮上时伪元素::before的样式,包括字体大小、透明度、Y轴变换和过渡效果。
相关推荐
夏幻灵23 分钟前
HTML5里最常用的十大标签
前端·html·html5
冰暮流星23 分钟前
javascript之二重循环练习
开发语言·javascript·数据库
Mr Xu_36 分钟前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝40 分钟前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions1 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发1 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_1 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞051 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、1 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao1 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架