如何将Codepen动画边框发光效果整合到HTML/CSS图片动画中
整合Codepen边框发光动画到现有项目的问题解决
问题描述
我在Codepen上找到一个酷炫的边框发光动画效果,想把它加到自己的HTML/CSS图片动画里。我试着把Codepen代码里的.card类换成我代码里的.container类,但没达到预期效果。我是编程新手,相关代码如下:
Codepen CSS代码
@property --rotate { syntax: "<angle>"; initial-value: 132deg; inherits: false; } :root { --card-height: 65vh; --card-width: calc(var(--card-height) / 1.5); } body { min-height: 100vh; background: #212534; display: flex; align-items: center; flex-direction: column; padding-top: 2rem; padding-bottom: 2rem; box-sizing: border-box; } .card { background: #191c29; width: var(--card-width); height: var(--card-height); padding: 3px; position: relative; border-radius: 6px; justify-content: center; align-items: center; text-align: center; display: flex; font-size: 1.5em; color: rgb(88 199 250 / 0%); cursor: pointer; font-family: cursive; } .card:hover { color: rgb(88 199 250 / 100%); transition: color 1s; } .card:hover:before, .card:hover:after { animation: none; opacity: 0; } .card::before { content: ""; width: 104%; height: 102%; border-radius: 8px; background-image: linear-gradient(var(--rotate), #5ddcff, #3c67e3 43%, #4e00c2); position: absolute; z-index: -1; top: -1%; left: -2%; animation: spin 2.5s linear infinite; } .card::after { position: absolute; content: ""; top: calc(var(--card-height) / 6); left: 0; right: 0; z-index: -1; height: 100%; width: 100%; margin: 0 auto; transform: scale(0.8); filter: blur(calc(var(--card-height) / 6)); background-image: linear-gradient(var(--rotate), #5ddcff, #3c67e3 43%, #4e00c2); opacity: 1; transition: opacity 0.5s; animation: spin 2.5s linear infinite; } @keyframes spin { 0% { --rotate: 0deg; } 100% { --rotate: 360deg; } } a { color: #212534; text-decoration: none; font-family: sans-serif; font-weight: bold; margin-top: 2rem; }
我的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>Creative Menu Effect</title> <link rel="stylesheet" href="2ndpage.css" /> </head> <body> <header> <div class="logoWrapper"> <img class="logo" src="Images/Untitled.png" alt="logo"> <a class="mtap"> Mount <br> Apo </a> </div> <nav> <ul class="nav_links"> <li><a href="#">Services</a></li> <li><a href="#">Projects</a></li> <li><a href="#">About</a></li> </ul> </nav> <a class="cta" href="#"><button>Contact</button></a> </header> <section class="container"> <div class="clip clip1"></div> <div class="clip clip2"></div> <div class="clip clip3"></div> </section> </body> </html>
我的CSS代码
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap'); * { box-sizing: border-box; margin: 0; padding: 0; background-color: #24252A; justify-content: center; align-items: center; } li, a, button { font-family: "Montserrat", sans-serif; font-weight: 500; font-size: 16px; color: #edf0f1; text-decoration: none; } header { display: flex; justify-content: space-between; align-items: center; padding: 30px 10%; } .logo { width: 100px; } .logoWrapper { display: flex; gap: 10px; align-items: center; } .mtap { font-family: "Montserrat", sans-serif; font-weight: 500; font-size: 16px; color: #edf0f1; text-decoration: none; } .mtap:hover { color: #0088a9; } .nav_links { list-style: none; } .nav_links li { display: inline-block; padding: 0px 20px; } .nav_links li a { transition: all 0.5s ease 0s; } .nav_links li a:hover { color: #0088a9; } button { padding: 9px 25px; background-color: rgba(0, 136, 169, 1); border: none; border-radius: 50px; cursor: pointer; transition: all 0.5s ease 0s; } button:hover { background-color: rgba(0, 136, 169, 0.8); } li { list-style: none; } ul { display: flex; } ul li a { color: #fff; text-decoration: none; padding: 0.4rem 1rem; margin: 0 10px; position: relative; transition: all 0.5s; text-transform: uppercase; } .nav_links li a:before { content: ""; position: relative; bottom: 12px; left: 12px; width: 12px; height: 12px; border: 3px solid white; border-width: 0 0 3px 3px; opacity: 0; transition: all 0.6s; } .nav_links li a:hover:before { opacity: 1; bottom: -8px; left: -8px; } .nav_links li a:after { content: ""; position: relative; top: 0; right: 0; width: 12px; height: 12px; border: 3px solid }
问题原因及修复方案
核心问题
- 全局样式冲突:
*选择器给所有元素(包括伪元素)设置了background-color: #24252A,覆盖了发光伪元素的渐变背景,导致效果无法显示。 - 尺寸变量不匹配:Codepen的
--card-height/width是针对卡片的固定尺寸,.container需要适配自身布局,不能直接复用。 - 定位缺失:
.container未设置position: relative,导致伪元素无法正确定位。
修改后的整合代码
在原有CSS末尾添加以下代码(替换Codepen的.card相关样式):
/* 发光动画核心定义 */ @property --rotate { syntax: "<angle>"; initial-value: 132deg; inherits: false; } :root { /* 为container自定义尺寸,可按需调整 */ --container-height: 50vh; --container-width: 80vw; } .container { background: #191c29; width: var(--container-width); height: var(--container-height); padding: 3px; position: relative; border-radius: 6px; margin: 2rem auto; display: flex; justify-content: center; align-items: center; cursor: pointer; } /* 清除伪元素的全局背景覆盖 */ .container::before, .container::after { background-color: transparent; } .container::before { content: ""; width: 104%; height: 102%; border-radius: 8px; background-image: linear-gradient(var(--rotate), #5ddcff, #3c67e3 43%, #4e00c2); position: absolute; z-index: -1; top: -1%; left: -2%; animation: spin 2.5s linear infinite; } .container::after { content: ""; position: absolute; top: calc(var(--container-height) / 6); left: 0; right: 0; z-index: -1; height: 100%; width: 100%; margin: 0 auto; transform: scale(0.8); filter: blur(calc(var(--container-height) / 6)); background-image: linear-gradient(var(--rotate), #5ddcff, #3c67e3 43%, #4e00c2); opacity: 1; transition: opacity 0.5s; animation: spin 2.5s linear infinite; } .container:hover::before, .container:hover::after { animation: none; opacity: 0; } @keyframes spin { 0% { --rotate: 0deg; } 100% { --rotate: 360deg; } }
额外优化建议
把*选择器中的background-color移到body上,避免影响其他元素:
* { box-sizing: border-box; margin: 0; padding: 0; /* 移除该行 */ justify-content: center; align-items: center; } body { background-color: #24252A; /* 移到此处 */ }
修改后,.container就能正常显示旋转发光的边框效果,同时保留原有导航栏样式。
内容的提问来源于stack exchange,提问作者C9SMIC69
相关产品推荐
相关产品推荐

