前端 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>
相关推荐
用户61581396951614 小时前
Elpis: 基于vue3+webpack5+nodejs搭建一个完整项目
前端
90后的晨仔14 小时前
S C:\WINDOWS\system32> pnpm i -g openclaw@latest pnpm : 无法加载文件 C:\xx\A
前端
蜡台15 小时前
Node 版本管理器NVM 安装配置和使用
前端·javascript·vue.js·node·nvm
狂奔蜗牛飙车15 小时前
Day3:HTML5 基础标签:h1-h6、p、hr、br、a、img
前端·html·html5·html常用标签的使用方法
木斯佳15 小时前
前端八股文面经大全:腾讯前端暑期提前批一、二、三面面经(下)(2026-03-04)·面经深度解析
前端
bluceli15 小时前
前端国际化(i18n)实战指南:构建多语言应用的完整方案
前端
hh随便起个名15 小时前
React组件通信
前端·react.js·前端框架
前端 贾公子15 小时前
vite-plugin-eruda-pro 在vite中使用eruda
前端
Jackson__16 小时前
Agent Skill 和 Rules 有什么区别?
前端·agent·ai编程
不要卷鸿蒙啊16 小时前
【鸿蒙开发】HMRouter一款和好用的管理路由三方工具
前端·harmonyos