利用 CSS 的邊框與透明邊框技巧,可以不靠圖片就畫出六邊形。這種方法適合用於裝飾背景、蜂巢牆或特殊版面排列。
一個六邊形可由三個部分組成:上方三角形、中間矩形、下方三角形。透過寬度為 0 的元素搭配不同邊框顏色,就能做出上下尖角。
.hiveTop {
width: 0;
border: 30px solid #6C6;
border-top: none;
border-bottom: 30px solid #6C6;
border-left: 52px solid transparent;
border-right: 52px solid transparent;
}
.hiveCenter {
width: 104px;
height: 60px;
background-color: #6C6;
}
.hiveBottom {
width: 0;
border: 30px solid #6C6;
border-bottom: none;
border-top: 30px solid #6C6;
border-left: 52px solid transparent;
border-right: 52px solid transparent;
}
將多個六邊形放在同一列或下一列時,可透過 margin、float 與 clear 微調位置,讓它們像蜂巢一樣交錯排列。