使用HTML+CSS代码可快速创建动态圣诞树,复制代码保存为.html文件后用浏览器打开即可查看闪烁效果。

想在网页上展示一棵漂亮的圣诞树?用HTML代码就能轻松实现。只要一段简单的HTML+CSS代码,配合浏览器打开,就能看到动态闪烁的圣诞树效果。下面教你如何运行HTML圣诞树代码,无需编程基础也能快速上手。
1. 获取HTML圣诞树代码
你可以从网上搜索“HTML Christmas Tree 代码”找到现成示例,或者直接使用以下简化版代码:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>圣诞树</title>
<style>
body {
background: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
overflow: hidden;
}
.tree {
position: relative;
width: 200px;
margin: 50px auto;
}
.tree::before {
content: "";
position: absolute;
top: -60px;
left: 90px;
width: 20px;
height: 60px;
background: #6D4C41;
}
.layer {
position: relative;
margin: 0 auto;
width: 0;
height: 0;
border-left: 100px solid transparent;
border-right: 100px solid transparent;
border-bottom: 60px solid green;
}
.layer:nth-child(1) { top: 0; border-bottom-color: #2E7D32; }
.layer:nth-child(2) { top: -40px; border-bottom-color: #388E3C; }
.layer:nth-child(3) { top: -80px; border-bottom-color: #43A047; }
.star {
position: absolute;
top: -120px;
left: 85px;
width: 30px;
height: 30px;
background: yellow;
clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
animation: blink 1s infinite;
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.light {
position: absolute;
width: 10px;
height: 10px;
background: red;
border-radius: 50%;
animation: flicker 0.8s infinite alternate;
}
.light:nth-child(4n+1) { background: red; animation-delay: 0.1s; }
.light:nth-child(4n+2) { background: blue; animation-delay: 0.3s; }
.light:nth-child(4n+3) { background: yellow; animation-delay: 0.5s; }
.light:nth-child(4n+4) { background: cyan; animation-delay: 0.7s; }
@keyframes flicker {
0% { opacity: 0.6; }
100% { opacity: 1; }
}
</style>
</head>
<body>
<div class="tree">
<div class="star"></div>
<div class="layer"></div>
<div class="layer"></div>
<div class="layer"></div>
<span class="light" style="top: -40px; left: 60px;"></span>
<span class="light" style="top: -60px; left: 120px;"></span>
<span class="light" style="top: -90px; left: 80px;"></span>
<span class="light" style="top: -110px; left: 110px;"></span>
</div>
</body>
</html>
2. 保存为HTML文件
将上面的代码复制到记事本(Notepad)或任意文本编辑器中,然后点击“另存为”,文件名输入 christmas_tree.html,保存类型选择“所有文件”,编码选 UTF-8,点击保存。
3. 在浏览器中运行
找到你保存的 christmas_tree.html 文件,双击它,系统会默认用浏览器打开。你会看到一棵绿色三角形组成的圣诞树,顶部有星星,周围有闪烁的小彩灯,非常应景。
立即学习“前端免费学习笔记(深入)”;
4. 常见问题与优化建议
- 打不开? 确保文件后缀是 .html,不是 .txt。如果保存成了 christmas_tree.html.txt,需开启“显示文件扩展名”并手动改为 .html。
- 想更炫? 可添加音乐、雪花动画或更多装饰元素,通过加入 JavaScript 实现交互效果。
- 分享给别人? 把这个HTML文件发给朋友,他们也能直接双击查看,无需联网。
基本上就这些。不复杂但容易忽略细节,比如文件格式和编码。只要按步骤来,几分钟内你就能拥有一个会闪灯的网页圣诞树。节日气氛瞬间拉满!










