前端 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="initial-scale=1.0, user-scalable=no, width=device-width"
    />
    <title>document</title>
    <style>
      body {
        background: #000;
      }
      button {
        margin: 50px 40%;
        width: 200px;
        height: 80px;
        color: #fff;
        font-size: 24px;
        background: #000;
        border: none;
        outline: none;
        z-index: 1;
        border-radius: 10px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }

      button::before {
        content: "";
        width: 200px;
        height: 200px;
        position: absolute;
        background: orange;
        z-index: -2;
        left: 50%;
        top: 50%;
        transform-origin: left top;
        animation: rotating 2s linear infinite;
      }

      button::after {
        content: "";
        position: absolute;
        --g: 4px;
        width: calc(100% - var(--g) * 2);
        height: calc(100% - var(--g) * 2);
        background: #000;
        left: var(--g);
        top: var(--g);
        border-radius: inherit;
        z-index: -1;
      }

      @keyframes rotating {
        to {
          transform: rotate(360deg);
        }
      }
    </style>
  </head>
  <body>
    <button>按钮</button>
    <script></script>
  </body>
</html>
相关推荐
开开心心就好2 分钟前
手机悬屏翻译工具外语游戏漫画APP全覆盖
android·前端·javascript·python·游戏·pdf·html
支支დ8 分钟前
Phind 前端特定优势:AI 编程助手的体验革新
前端
DDXYcoder14 分钟前
万字解析动态内存从编译到运行
java·前端·算法
光影少年15 分钟前
react navite封装一个RN 通用按钮组件
前端·javascript·react native·react.js·前端框架
晴天1616 分钟前
CSS 预处理器深度解析-Day35
前端·css
恋猫de小郭18 分钟前
AI 时代,也许你的 Flutter 需要一套 Dartastic OpenTelemetry 监控
android·前端·flutter
wujiuhsu21 分钟前
前端实现二维码生成器:Canvas、SVG、纠错等级、扫码校验与批量
前端
计算机魔术师26 分钟前
从卖铲人到圈地:英伟达129亿拿下Hugging Face
前端
攻城狮-申30 分钟前
git本地分支对齐远程分支
前端·git
IT_陈寒37 分钟前
Vite热更新突然失效?可能是这个配置在捣鬼
前端·人工智能·后端