前端 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>
相关推荐
我叫张小白。6 分钟前
TypeScript对象类型与接口:构建复杂数据结构
前端·javascript·typescript
墨客希12 分钟前
如何快速掌握大型Vue项目
前端·javascript·vue.js
大福ya26 分钟前
AI开源项目改造NextChat(ChatGPT-Next-Web)实现前端SSR改造打造一个初始框架
前端·chatgpt·前端框架·开源·aigc·reactjs·ai编程
n***333538 分钟前
SpringBoot返回文件让前端下载的几种方式
前端·spring boot·后端
纯粹的热爱1 小时前
🌐 阿里云 Linux 服务器 Let's Encrypt 免费 SSL 证书完整部署指南
前端
北辰alk1 小时前
Vue3 自定义指令深度解析:从基础到高级应用的完整指南
前端·vue.js
重铸码农荣光1 小时前
从 0 到 1 实现 LocalStorage 待办清单:CSS 进阶 + 前端工程化思想实践
css·前端工程化
小熊哥7221 小时前
谈谈最进学习(低延迟)直播项目的坎坷与收获
前端
用户89225411829011 小时前
游戏框架文档
前端
Holin_浩霖1 小时前
mini-react 实现function 组件
前端