如何运用CSS完成渐变色动画边框的效果(附代码)
发表时间:2025-10-27 来源:浏览器大全整理相关软件相关文章人气:
网页的本质就是超级文本标记语言,通过结合使用其他的Web技术(如:脚本语言、公共网关接口、组件等),可以创造出功能强大的网页。因而,超级文本标记语言是万维网(Web)编程的基础,也就是说万维网是建立在超文本基础之上的。超级文本标记语言之所以称为超文本标记语言,是因为文本中包含了所谓“超级链接”点。
本篇文章给大家带来的内容是关于如何使用CSS实现渐变色动画边框的效果(附代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。效果预览

源代码下载
https://github.com/comehope/front-end-daily-challenges/tree/master/016-colorful-gradient-animated-border
代码解读
定义 dom,一个容器中包含一些文字:
<div class="box">
you are my<br>
FAVORITE
</div>居中显示:
html,
body,
.box {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}设置页面背景色:
body {
background: #222;
}设置容器和文字样式:
.box {
color: white;
font-size: 2.5em;
width: 10em;
height: 5em;
background: #111;
font-family: sans-serif;
line-height: 1.5em;
text-align: center;
border-radius: 0.2em;
}用伪元素增加一个背板:
.box {
position: relative;
}
.box::after {
content: '';
position: absolute;
width: 102%;
height: 104%;
background-color: orange;
z-index: -1;
border-radius: 0.2em;
}把背板设置为渐变色的:
.box::after {
/*background-color: orange;*/
background-image: linear-gradient(60deg, aquamarine, cornflowerblue, goldenrod, hotpink, salmon, lightgreen, sandybrown, violet);
}为背板设置动画效果:
.box::after {
background-size: 300%, 300%;
animation: animate_bg 5s ease infinite alternate;
}
@keyframes animate_bg {
0% {
background-position: 0%, 50%;
}
50% {
background-position: 100%, 50%;
}
100% {
background-position: 0%, 50%;
}
}最后,再为文字增加变色效果:
.box {
animation: animate_text 2s linear infinite alternate;
}
@keyframes animate_text {
from {
color: lime;
}
to {
color: yellow;
}
}大功告成!
相关推荐:
如何使用CSS实现一只鸭子头(附代码)
如何使用CSS和混色模式实现loader动画效果(附代码)
以上就是如何使用CSS实现渐变色动画边框的效果(附代码)的详细内容,更多请关注php中文网其它相关文章!
网站建设是一个广义的术语,涵盖了许多不同的技能和学科中所使用的生产和维护的网站。