六邊形(蜂巢)排版是近幾年很流行的一種卡片配置,比傳統的方格更有活潑感,也特別適合人物、作品或景點的展示。這個範例以純 CSS3 實作,滑鼠移入時圖片會有對應的回饋效果。
核心手法是 clip-path: polygon():直接以六個頂點座標告訴瀏覽器要裁出什麼形狀,一行就能切出正六邊形。搭配外層容器的負向 margin 讓相鄰的六邊形彼此嵌套咬合,就能拼出蜂巢般的緊密排列;內層再透過 transform: scale() 在 hover 時微微放大,形成層次。
與早期的做法相比,這比用偽元素堆疊三角形或旋轉正方形要直觀得多,也能更精準地控制每一個角度。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS3 實現六邊形網格布局</title>
<link rel="stylesheet" type="text/css"
href="https://hovertree.com/texiao/css3/30/css/hexagons.css">
</head>
<body>
<ul id="hexGrid">
<li class="hex">
<a class="hexIn" href="https://hovertree.com/">
<img src="https://hovertree.com/texiao/css3/30/img/1.jpg" alt="" />
<h1>何問起</h1>
<p>嗨,我是 1 號,我叫何問起,我萌吧,歡迎點我。</p>
</a>
</li>
<li class="hex">
<a class="hexIn" href="https://hovertree.com/h/bjaf/kqud99m6.htm">
<img src="https://hovertree.com/texiao/css3/30/img/2.jpg" alt="" />
<h1>二號</h1>
<p>我是二號,歡迎點我。</p>
</a>
</li>
<li class="hex">
<a class="hexIn" href="https://hovertree.com/hvtart/bjae/m8wrrmrw.htm">
<img src="https://hovertree.com/texiao/css3/30/img/3.jpg" alt="" />
<h1>冷冰冰</h1>
<p>我的外表冷冰冰,但是我很活潑的,進來看看就知道了。</p>
</a>
</li>
<li class="hex">
<a class="hexIn" href="https://hovertree.com/h/bjaf/html5zixueji.htm">
<img src="https://hovertree.com/texiao/css3/30/img/4.jpg" alt="" />
<h1>美女</h1>
<p>別問我是誰,相逢何必曾相識。</p>
</a>
</li>
<li class="hex">
<a class="hexIn" href="https://hovertree.com/h/bjaf/vvxsmuh4.htm">
<img src="https://hovertree.com/texiao/css3/30/img/5.jpg" alt="" />
<h1>This is a title</h1>
<p>Some sample text about the article this hexagon leads to</p>
</a>
</li>
<li class="hex">
<a class="hexIn" href="https://hovertree.com/texiao/css3/20/">
<img src="https://hovertree.com/texiao/css3/30/img/6.jpg" alt="" />
<h1>This is a title</h1>
<p>Some sample text about the article this hexagon leads to</p>
</a>
</li>
<li class="hex">
<a class="hexIn" href="#">
<img src="https://hovertree.com/texiao/css3/30/img/7.jpg" alt="" />
<h1>HT 主播</h1>
<p>如果你有絕活,歡迎加入我們的團隊。</p>
</a>
</li>
<li class="hex">
<a class="hexIn" href="https://tool.hovertree.com/a/base64/">
<img src="https://hovertree.com/texiao/css3/30/img/8.jpg" alt="" />
<h1>This is a title</h1>
<p>Some sample text about the article this hexagon leads to</p>
</a>
</li>
<!-- 後續項目結構與上述完全相同,可依需求無限擴充 -->
</ul>
<div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';">
<p>適用瀏覽器:FireFox、Chrome、Safari、Opera、Edge、IE11。不支援 IE8 及以下瀏覽器。</p>
<p>來源:<a href="https://hovertree.com/" target="_blank">何問起</a>
<a href="https://hovertree.com/menu/texiao/">網頁特效</a>
<a href="https://hovertree.com/h/bjaf/pned60nr.htm">代碼說明</a></p>
</div>
</body>
</html>
HTML 結構非常單純:一個 #hexGrid 清單,每個 <li class="hex"> 裡面放一個 <a class="hexIn"> 包住圖片與文字,剩下的排版全交給 hexagons.css。要擴充卡片數量時,直接複製貼上新的 li 區塊即可,網格會自動補位。
需要留意的是,六邊形排列在窄螢幕上容易出現大片空白,建議加上 media query,在小螢幕切換成兩欄或單欄的長方形卡片,閱讀體驗會好很多。此外,被 clip-path 裁掉的區域仍然可以接收點擊事件,若要避免這個問題,可將 pointer-events 相關設定一併處理。