这是我参与8月更文挑战的第26天,活动详情查看:8月更文挑战
作者:battleKing
仓库:Github、CodePen
博客:CSDN、掘金
反馈邮箱:myh19970701@foxmail.com
特别声明:原创不易,未经授权不得转载或抄袭,如需转载可联系笔者授权
背景
彩虹跑马灯边框效果:彩虹跑马灯边框是常用的一种 CSS 边框效果,相比于其他单色边框,主要起到 突出按钮 或者 某一特定区域内内容 ,吸引读者注意 的效果。接下来我们一起用 CSS 代码来实现这一效果。
最终效果
一、添加 HTML 文件
HTML部分非常简单,只要添加一个类名为gradient-border的div
<div class="gradient-border">css<br />is<br />handsome</div>
二、添加 CSS 文件
先初始化页面
- 设置
*为box-sizing: border-box - 设置
body来使整个项目居中
CSS 全局变量
- CSS 全局变量声明在
:root文档根元素中,语法为--* - CSS 全局变量使用语法为
var(--*)
- 声明
:root {
--border-width: 3px;
}
- 使用
.gradient-border{
border-radius: var(--border-width);
}
主要的 CSS 代码
.gradient-border {
--border-width: 3px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 300px;
height: 200px;
font-family: Lato, sans-serif;
font-size: 2.5rem;
text-transform: uppercase;
color: white;
background: #222;
border-radius: var(--border-width);
}
.gradient-border::after {
position: absolute;
content: "";
top: calc(-1 * var(--border-width));
left: calc(-1 * var(--border-width));
z-index: -1;
width: calc(100% + var(--border-width) * 2);
height: calc(100% + var(--border-width) * 2);
background: linear-gradient(60deg, #5f86f2, #a65ff2, #f25fd0, #f25f61, #f2cb5f, #abf25f, #5ff281, #5ff2f0);
background-size: 300% 300%;
background-position: 0 50%;
border-radius: calc(2 * var(--border-width));
animation: moveGradient 4s alternate infinite;
}
@keyframes moveGradient {
50% {
background-position: 100% 50%;
}
}
❤️ 感谢大家
如果本文对你有帮助,就点个赞支持下吧,你的「赞」是我创作的动力。
如果你喜欢这篇文章的话,可以「点赞」 + 「收藏」 + 「转发」 给更多朋友。