前端 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>
相关推荐
Juchecar21 分钟前
分析:将现代开源浏览器的JavaScript引擎更换为Python的可行性与操作
前端·javascript·python
极客小俊28 分钟前
Font Awesome 一个基于CSS和LESS的免费图标库工具包
前端
yinuo1 小时前
CSS基础动画keyframes
前端
一条上岸小咸鱼2 小时前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
前端小巷子2 小时前
Vue 事件绑定机制
前端·vue.js·面试
uhakadotcom2 小时前
开源:subdomainpy快速高效的 Python 子域名检测工具
前端·后端·面试
爱加班的猫2 小时前
Node.js 中 require 函数的原理深度解析
前端·node.js
用户8165111263972 小时前
WWDC 2025 Build a SwiftUI app with the new design
前端
伍哥的传说2 小时前
Vue 3.5重磅更新:响应式Props解构,让组件开发更简洁高效
前端·javascript·vue.js·defineprops·vue 3.5·响应式props解构·vue.js新特性