边框按钮(纯CSS)

边框按钮(纯CSS)

  • [1. 思路](#1. 思路)
  • [2. 实现代码](#2. 实现代码)
  • [3. 拓展](#3. 拓展)
    • [3.1 双边滑动按钮](#3.1 双边滑动按钮)
    • [3.2 双边不滑动按钮](#3.2 双边不滑动按钮)

1. 思路

(1)主体部分

(2)伪元素before设置在-2层

(3)伪元素after设置在-1层

(4)去掉按钮的边框,并且隐藏超出部分

(5)添加动画,旋转伪元素 before

2. 实现代码

index.html:

html 复制代码
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./index.css" />
  </head>
  <body>
    <button>边框按钮</button>
  </body>
</html>

index.css:

css 复制代码
body{
  background-color: #000;
}
button{
  width: 160px;
  height: 80px;
  color: #0ebeff;
  font-size: 24px;
  background: #000;
  border:none;
  z-index: 1;
  cursor: pointer;
  border-radius: 10px;
  position: relative;
  overflow: hidden;
}

button::before{
  content: '';
  position: absolute;
  --ratio: 200%;
  width: var(--ratio);
  height: var(--ratio);
  background: red;
  z-index: -2;
  left: 50%;
  top: 50%;
  transform-origin: left top;
  animation: rotation 2s linear infinite;
}

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;
}

@keyframes rotation {
  to {
    transform: rotate(1turn);
  }  
}

3. 拓展

3.1 双边滑动按钮

其实思路也很简单,将伪元素 bofore 调整为矩形,并且中心点和旋转点重合即可(这里看着卡顿,只是因为 gif 压缩后导致的,实际很顺滑)。

index.css:

css 复制代码
body{
  background-color: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}
button{
  margin-top: 200px;
  --button-width: 160px;
  --button-height: 80px;
  width: var(--button-width);
  height: var(--button-height);
  color: #0ebeff;
  font-size: 24px;
  background: #000;
  border:none;
  z-index: 1;
  cursor: pointer;
  border-radius: 10px;
  position: relative;
  overflow: hidden;
  /* outline: 4px solid #fff; */
}

button::before{
  content: '';
  position: absolute;
  --w: 320px;
  --h: 30px;
  width: var(--w);
  height: var(--h);
  background: red;
  z-index: -2;
  left: calc(50% - var(--w) / 2);
  top: calc(50% - var(--h) / 2);
  animation: rotation 15s linear infinite;
}

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;
}

@keyframes rotation {
  to {
    transform: rotate(1turn);
  }  
}

3.2 双边不滑动按钮

基于 3.1 双边滑动按钮 取消动画,修改伪元素before旋转角度即可。

index.css:

css 复制代码
body{
  background-color: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}
button{
  margin-top: 200px;
  --button-width: 160px;
  --button-height: 80px;
  width: var(--button-width);
  height: var(--button-height);
  color: #0ebeff;
  font-size: 24px;
  background: #000;
  border:none;
  z-index: 1;
  cursor: pointer;
  border-radius: 10px;
  position: relative;
  overflow: hidden;
  /* outline: 4px solid #fff; */
}

button::before{
  content: '';
  position: absolute;
  --w: 320px;
  --h: 30px;
  width: var(--w);
  height: var(--h);
  background: red;
  z-index: -2;
  left: calc(50% - var(--w) / 2);
  top: calc(50% - var(--h) / 2);
  transform: rotate(25deg);
  /* animation: rotation 15s linear infinite; */
}

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;
}

/* @keyframes rotation {
  to {
    transform: rotate(1turn);
  }  
} */
相关推荐
Csvn15 小时前
OpenSpec 详细使用教程
前端
王莎莎-MinerU15 小时前
MinerU 深度技术解析:从架构原理到生产部署的全面指南
css·人工智能·自然语言处理·架构·ocr·个人开发
之歆16 小时前
Day19_LESS 完全指南——从入门到工程实践
前端·css·less
云水一下16 小时前
HTML5 从入门到精通:实战收官——从零搭建完整静态网站,综合运用所有知识
前端·html5
不总是16 小时前
Windows 系统 Node.js 免安装版(zip)安装与配置教程(2026 最新)
前端·windows·node.js
冬奇Lab17 小时前
每日一个开源项目(第105篇):Twenty - 跳出 Salesforce 的圈套,定义现代开源 CRM
前端·后端·开源
zhangyao94033017 小时前
开发pc端时,表格的高度怎么设置才能铺满页面
前端·javascript·elementui
kjs--18 小时前
浏览器书签执行脚本
前端
之歆18 小时前
Day16_JavaScript 轮播图与事件工程实战(下篇)
服务器·开发语言·前端·javascript·网络·性能优化
沄媪18 小时前
CSRF 跨站请求伪造
前端·ctf·csrf