公歷每年的 3 月 12 日是一年一度的植樹(shù)節(jié)。旨在宣傳保護(hù)森林,并動(dòng)員群眾參加植樹(shù)造林活動(dòng)。作為程序員,也有自己獨(dú)特的種樹(shù)方式!下面隨編程獅以前端 HTML+CSS 為例,一步一步在網(wǎng)頁(yè)上“種出一棵樹(shù)”。
效果預(yù)覽
前期準(zhǔn)備:
1、首先你要具備一些基礎(chǔ)前端知識(shí),學(xué)個(gè)入門(mén)就夠了
2、其次要有一個(gè)編輯器,VScode、WebStrom、HbuilderX等,甚至系統(tǒng)自帶的“記事本、Notepad、文本編輯”就夠了
一、頁(yè)面框架搭建
新建一個(gè)index.html
的文件(注意是.html
格式),用你的編輯器打開(kāi)它:
- 使用
<!DOCTYPE html>
聲明文檔類型 - 設(shè)置中文字符集
<meta charset="UTF-8">
- 標(biāo)題使用
<title>
標(biāo)簽設(shè)置為"程序員的3.12植樹(shù)節(jié) | 編程獅(w3cschool.cn)
"
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>程序員的3.12植樹(shù)節(jié) | 編程獅(w3cschool.cn)</title>
二、CSS樣式設(shè)計(jì)
-
背景設(shè)計(jì):
body { background: #1a2f38; /* 深藍(lán)綠色背景 */ display: flex; /* 彈性布局 */ flex-direction: column; /* 垂直排列 */ align-items: center; /* 水平居中 */ }
-
代碼樹(shù)繪制技巧:
- 樹(shù)干使用矩形+圓角:
.trunk { width: 20px; height: 150px; border-radius: 5px; }
- 樹(shù)冠使用橢圓+內(nèi)陰影:
.canopy { border-radius: 50px; box-shadow: 0 0 10px #3c887e, /* 外發(fā)光 */ inset 0 0 20px #1d4037; /* 內(nèi)陰影 */ }
- 樹(shù)干使用矩形+圓角:
- 代碼裝飾實(shí)現(xiàn):
.code::before { content: "http:// "; /* 偽元素添加注釋符號(hào) */ color: #4d7069; }
三、詩(shī)歌排版技巧
再配上一首專屬詩(shī)歌更有意境
《森林的布爾值》
by 編程獅(w3cschool.cn)
鍵盤(pán)叩擊的間隙
一粒粒字符正在發(fā)芽
我調(diào)試著循環(huán)里的年輪
直到指針在樹(shù)冠間溢出月光
bug是誤入的甲蟲(chóng)
蜷縮在某個(gè)葉片的背面
而我的注釋如護(hù)林員
用括號(hào)加固每截脆弱的枝干
當(dāng)風(fēng)穿過(guò)if-else的縫隙
整片森林沙沙作響
光標(biāo)閃爍的鐵鍬下
新綠正以十六進(jìn)制生長(zhǎng)
最后一次commit時(shí)
露珠從枝頭滾落
整個(gè)春天突然return了
一行濕潤(rùn)的true
- 使用
pre
標(biāo)簽保留換行格式 - 左側(cè)裝飾線:
.poem { border-left: 3px solid #3c887e; padding-left: 20px; }
四、程序員元素融合
- 在樹(shù)冠周?chē)砑哟a片段注釋
- 使用等寬字體
font-family: 'Courier New'
- 顏色選擇藍(lán)綠色系,模擬代碼編輯器主題
- 詩(shī)中融入編程術(shù)語(yǔ):commit、return、十六進(jìn)制等
五、布局技巧
- 使用
position: absolute
進(jìn)行圖層疊加 -
通過(guò)transform實(shí)現(xiàn)水平居中:
transform: translateX(-50%);
- 彈性布局保持整體垂直排列
完整代碼展示
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>程序員的3.12植樹(shù)節(jié) | 編程獅(w3cschool.cn)</title>
<style>
/* 全局樣式 */
body {
background: #1a2f38;
display: flex;
flex-direction: column;
align-items: center;
font-family: 'Courier New', monospace;
}
/* 詩(shī)歌容器 */
.poem {
color: #c3d8df;
max-width: 600px;
margin: 40px;
line-height: 1.8;
border-left: 3px solid #3c887e;
padding-left: 20px;
}
/* 代碼樹(shù) */
.code-tree {
position: relative;
margin: 50px;
height: 300px;
}
/* 樹(shù)干 */
.trunk {
position: absolute;
width: 20px;
height: 150px;
background: #785549;
bottom: 0;
left: 50%;
transform: translateX(-50%);
border-radius: 5px;
}
/* 樹(shù)冠 */
.canopy {
position: absolute;
width: 120px;
height: 100px;
background: #2c5a47;
bottom: 140px;
left: 50%;
transform: translateX(-50%);
border-radius: 50px;
box-shadow:
0 0 10px #3c887e,
inset 0 0 20px #1d4037;
}
/* 代碼裝飾 */
.code {
position: absolute;
color: #6ba08a;
font-size: 12px;
white-space: nowrap;
}
.code::before {
content: "http:// ";
color: #4d7069;
}
</style>
</head>
<body>
<div class="code-tree">
<div class="trunk"></div>
<div class="canopy"></div>
<div class="code" style="top:30px; left:-40px;">if (tree.isGrowing()) {</div>
<div class="code" style="top:80px; right:-50px;">branch++;</div>
<div class="code" style="top:150px; left:10px;">// 年輪.add()</div>
</div>
<div class="poem">
<h2>《森林的布爾值》</h2>
<p>by 編程獅(w3cschool.cn)</p>
<pre>
鍵盤(pán)叩擊的間隙
一粒粒字符正在發(fā)芽
我調(diào)試著循環(huán)里的年輪
直到指針在樹(shù)冠間溢出月光
bug是誤入的甲蟲(chóng)
蜷縮在某個(gè)葉片的背面
而我的注釋如護(hù)林員
用括號(hào)加固每截脆弱的枝干
當(dāng)風(fēng)穿過(guò)if-else的縫隙
整片森林沙沙作響
光標(biāo)閃爍的鐵鍬下
新綠正以十六進(jìn)制生長(zhǎng)
最后一次commit時(shí)
露珠從枝頭滾落
整個(gè)春天突然return了
一行濕潤(rùn)的true
</pre>
</div>
</body>
</html>
保存你的index.html
文件,用瀏覽器打開(kāi)就能看到網(wǎng)頁(yè)效果啦??
編程獅的這個(gè)作品通過(guò)將編程元素與自然意象結(jié)合,用CSS繪制出獨(dú)特的"代碼之樹(shù)",詩(shī)歌排版采用類代碼的顯示風(fēng)格,展現(xiàn)了程序員在植樹(shù)節(jié)的特殊慶祝方式。你們也快來(lái)試試吧~