利用伪元素::before和::after制作旋转边框的按钮
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
html,
body {
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.btn {
/* outline: 4px solid #000; */
width: 80px;
height: 30px;
border-radius: 3px;
display: flex;
justify-content: center;
align-items: center;
position: relative;
overflow: hidden;
}
.btn::before {
content: '';
background: rgb(188, 27, 27);
position: absolute;
height: 200%;
width: 200%;
border-radius: 3px;
animation: border-line 3s infinite linear;
z-index: -2;
transform-origin: 0 0;
top: 50%;
left: 50%;
}
@keyframes border-line {
to {
transform: rotate(1turn);
}
}
.btn::after {
content: '';
background: #fff;
height: calc(100% - 4px);
width: calc(100% - 4px);
position: absolute;
z-index: -1;
}
</style>
</head>
<body>
<div class="btn">按钮</div>
</body>
</html>
这篇博客展示了如何利用CSS伪元素`::before`和`::after`创建一个带有旋转边框效果的按钮。通过设置动画和定位,使得边框在按钮周围动态旋转,实现了视觉上的独特效果。示例代码详细解释了每个样式属性的作用,帮助读者理解并应用到自己的项目中。
726

被折叠的 条评论
为什么被折叠?



