用 CSS 畫圖示是熟悉 border-radius、定位與盒模型的好方法。這個範例示範如何用最少的元素堆疊出瀏覽器 Logo 的樣子——三層同心圓加上兩個白色方塊,就足夠撐起整個圖形。
<!DOCTYPE html>
<html>
<head>
<title>CSS Logo</title>
<style type="text/css">
* { margin:0; padding:0 }
.main { width:800px; margin:50px auto 0 }
/* 外圈:淺藍底 + 深藍描邊 + 外陰影 */
.main_wrapper {
width:240px; height:240px;
border-radius:120px;
background:#B1E4FF;
border:2px solid #789CB6;
box-shadow:5px 5px 7px #999;
}
/* 內圈:飽和藍色圓 */
.main_wrap {
width:230px; height:230px;
border-radius:115px;
position:relative;
top:5px; left:5px;
background:#3B99E3;
}
/* 白色主體,右下角做成圓弧 */
.white_a {
width:150px; height:100px;
background:#FFF;
position:relative;
left:42px; top:70px;
border-radius:3px 20px 3px 3px;
}
/* 白色小塊,用超大藍色邊框擠出形狀 */
.white_b {
width:35px; height:45px;
background:#FFF;
position:relative;
left:33px; top:30px;
border:25px solid #3B99E3;
}
</style>
</head>
<body>
<div class="main">
<div class="main_wrapper">
<div class="main_wrap">
<div class="white_a">
<div class="white_b"></div>
</div>
</div>
</div>
</div>
</body>
</html>
用 border-radius 畫圓:寬高相等且半徑給到一半(240px 的方塊配 120px 半徑),就是正圓。
用 relative 做內縮:.main_wrap 比外層小 10px,再用 top/left 各偏移 5px,就能完美居中。這比負 margin 直覺得多。
用 border-radius 的四值語法做不對稱圓角:3px 20px 3px 3px 依序是左上、右上、右下、左下,只有右上角是明顯圓弧,其餘接近直角。
用厚邊框擠出造型:.white_b 讓 25px 的藍色邊框把白色內容擠到角落,這是純 CSS 畫圖裡相當常用的一招。
整份程式碼沒有任何圖片,改一改配色與尺寸就能挪去其他專案當作載入動畫的圖示。這類練習的價值在於:寫過幾次之後,你對「方塊如何堆疊成圖形」會有一種直覺。