css实现按钮边框旋转

先上效果图

本质:一个矩形在两个矩形互相重叠遮盖形成的缝隙中旋转形成,注意css属性z-index层级关系

直接上代码

html 复制代码
  <div class="bg">
    <div class="button">按钮</div>
  </div>
  
  <style>
	.bg {
	  width: 100%;
	  height: 100vh;
	  background-color: #000;
	  display: flex;
	  align-items: center;
	  justify-content: center;
	}


	@keyframes run {
	  0% {
	    transform: rotate(0);
	  }
	  100% {
	    transform: rotate(360deg);
	  }
	}

	.button {
	  width: 200px;
	  height: 100px;
	  display: flex;
	  align-items: center;
	  justify-content: center;
	  position: relative;
	  overflow: hidden;  /* 关键属性 */
	  color: #fff;
	  z-index: 1;
	  border-radius: 4px;
	}
	.button::before {
	  content: "";
	  width: 200%;
	  height: 200%;  /* 调整此处宽高控制旋转边框的长度 */
	  position: absolute;
	  background: linear-gradient(135deg, #ff00cc, #ffcc00, #00ffcc, #ff0066);
	  z-index: -2;
	  left: 50%;
	  top: 50%;
	  animation: run 1.5s linear infinite;
	  transform-origin: left top;
	}
	.button::after {
	  content: "";
	  position: absolute;
	  --w: 2px; /* 控制宽度 */
	  width: calc(100% - 2 * var(--w));
	  height: calc(100% - 2 * var(--w));
	  left: var(--w);
	  top: var(--w);
	  background: #000;
	  z-index: -1;
	  border-radius: inherit;
	}
  </style>
相关推荐
崔庆才丨静觅13 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
passerby606114 小时前
完成前端时间处理的另一块版图
前端·github·web components
掘了14 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅14 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅15 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅15 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment15 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅16 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊16 小时前
jwt介绍
前端
爱敲代码的小鱼16 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax