前端 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>
相关推荐
533_14 小时前
[vxe-table] 合并单元格
前端
浩星14 小时前
electron系列9:调用原生能力,剪贴板、通知、开机自启
前端·javascript·electron
Mapmost14 小时前
【Mapmost渲染指北】灯光+后处理,一招切出立体感
前端
J船长14 小时前
Kotlin 协程:从入门到深度理解
前端
Hilaku15 小时前
做中后台业务,为什么我不建议你用 Tailwind CSS?
前端·css·代码规范
有意义15 小时前
【面试复盘】前端底层原理与 React 核心机制深度梳理
前端·react.js·面试
二十一_15 小时前
LangChain 教程 05|模型配置:AI 的大脑与推理引擎
前端·langchain
kerli15 小时前
Compose 组件:BoxWithConstraints作用及其原理
android·前端
LovroMance15 小时前
消息总线 + 可插拔的消息插件管理系统
前端
Lee川15 小时前
React Router 实战指南:构建现代化前端路由系统
前端·react.js·架构