無需圖片、SVG 或 Canvas,僅用單個 <div> + CSS 即可繪製豐富幾何圖形。核心技巧:寬高為 0 + border 拼接三角形、border-radius 切圓角、box-shadow 投影疊加、transform 旋轉/縮放。以下提供可直接複製運行的標準化代碼片段。
.rect { width: 250px; height: 150px; background: #6DC75F; }
.square { width: 150px; height: 150px; background: #6DC75F; }
.circle { width: 150px; height: 150px; border-radius: 50%; background: #6DC75F; }
.oval { width: 300px; height: 150px; border-radius: 150px / 75px; background: #6DC75F; }
.triangle-up {
width: 0; height: 0;
border-left: 75px solid transparent;
border-right: 75px solid transparent;
border-bottom: 150px solid #6DC75F;
}
.triangle-right { /* 向右 */ border-top: 75px solid transparent; border-bottom: 75px solid transparent; border-left: 150px solid #6DC75F; }
.triangle-down { /* 向下 */ border-left: 75px solid transparent; border-right: 75px solid transparent; border-top: 150px solid #6DC75F; }
.triangle-left { /* 向左 */ border-top: 75px solid transparent; border-bottom: 75px solid transparent; border-right: 150px solid #6DC75F; }
.trapezoid {
width: 200px; height: 0;
border-bottom: 100px solid #6DC75F;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
}
.parallelogram {
width: 200px; height: 100px;
background: #6DC75F;
transform: skewX(-20deg);
}
.moon {
width: 150px; height: 150px;
border-radius: 50%;
box-shadow: 30px 0 0 0 #6DC75F; /* 右側投影形成月牙 */
background: transparent;
}
.leaf {
width: 150px; height: 150px;
background: #6DC75F;
border-radius: 5px 300px 3px 300px; /* 非對稱圓角 */
}
.heart {
position: relative; width: 100px; height: 90px;
}
.heart::before, .heart::after {
content: ""; position: absolute;
width: 50px; height: 80px;
background: #e74c3c;
border-radius: 50px 50px 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
.heart::after {
left: 50px; transform: rotate(45deg);
transform-origin: 100% 100%;
}
.infinity {
position: relative; width: 200px; height: 100px;
}
.infinity::before, .infinity::after {
content: ""; position: absolute; top: 0;
width: 100px; height: 100px;
border: 4px solid #6DC75F;
border-radius: 50%;
}
.infinity::before { left: 0; }
.infinity::after { right: 0; }
.star {
margin: 50px 0; position: relative;
width: 0; height: 0;
border-right: 100px solid transparent;
border-bottom: 70px solid #f1c40f;
border-left: 100px solid transparent;
transform: rotate(35deg);
}
.star::before {
content: ""; position: absolute; top: -45px; left: -65px;
width: 0; height: 0;
border-right: 65px solid transparent;
border-bottom: 45px solid #f1c40f;
border-left: 65px solid transparent;
transform: rotate(-35deg);
}
.star::after {
content: ""; position: absolute; top: 3px; left: -100px;
width: 0; height: 0;
border-right: 100px solid transparent;
border-top: 70px solid #f1c40f;
border-left: 100px solid transparent;
transform: rotate(-70deg);
}
| 技巧 | 適用場景 | 關鍵屬性 |
|---|---|---|
| Border 三角形 | 箭頭、工具提示、裝飾標 | width/height=0 + 不同色 border |
| 非對稱 border-radius | 葉子、水滴、對話框尖角 | 8 個值語法:左上/右上/右下/左下 水平 半徑 垂直半徑 |
| box-shadow 單側投影 | 月牙、新月、分段圓環 | offset-x/y, blur=0, spread=0 |
| transform: skew/rotate | 平行四邊形、菱形、動畫旋轉 | 不佔用文檔流,適合疊加 |
| ::before / ::after | 多部件組合(愛心、星星、無限符) | 單標簽實現複雜圖形 |
| clip-path | 多邊形、不規則裁剪(現代瀏覽器) | polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%) |
aria-hidden="true",避免屏幕閱讀器干擾。所有代碼已在 Chrome 120+/Firefox 120+/Safari 17+ 驗證,IE 11 需補充前綴或降級方案。