讓文字像雜誌排版一樣環繞非矩形圖片(圓形、多邊形、不規則輪廓),是編輯級網頁設計的高階需求。本文對比三種技術路線:CSS Shapes 標準方案、CSS Multi-column 模擬方案、JavaScript 動態分塊方案,附完整代碼示例。
shape-outside 定義浮動區域幾何形狀,文字自動沿輪廓流動。
.shape-circle {
float: left;
width: 200px; height: 200px;
shape-outside: circle(50% at center); /* 或 ellipse()、polygon()、url(mask.png) */
shape-margin: 16px; /* 文字與形狀間距 */
clip-path: circle(50% at center); /* 同步剪裁圖片顯示區域 */
}
支援:Chrome 37+、Firefox 62+、Safari 10.1+、Edge 79+。IE/舊 Edge 不支援,需優雅降級(矩形浮動)。
進階:配合 shape-image-threshold 使用 PNG Alpha 通道定義複雜輪廓。
將容器分為兩欄,圖片置中跨欄,文字分欄流動形成「環繞假象」。
.magazine-layout {
column-count: 2;
column-gap: 40px; /* 預留圖片空間 */
}
.figure-float {
width: 100%; /* 跨欄 */
margin: 0 auto 20px;
/* 實際圖片置中,文字自動分欄流過兩側 */
}
缺陷:文字流向為欄內自上而下、再下一欄,非真正環繞;圖片高度固定時效果較好,動態內容難控制。
原理:將文字按行高切分為 4 區塊(上、左、右、下),絕對定位環繞圖片,JS 實時計算每區塊字數使高度匹配。
<div class="wrapper">
<div class="zone top"></div>
<div class="zone left"></div>
<div class="zone right"></div>
<div class="zone bottom"></div>
<div class="figure"></div>
</div>
const text = document.querySelector('.source-text').textContent;
const zones = ['top','left','right','bottom'].map(sel => document.querySelector(`.zone.${sel}`));
const figure = document.querySelector('.figure');
const lineHeight = 20; // px
const figureRect = figure.getBoundingClientRect();
function distribute() {
let remaining = text;
zones.forEach((zone, i) => {
const maxLines = Math.floor(zone.clientHeight / lineHeight);
zone.textContent = remaining.slice(0, maxLines * avgCharsPerLine);
remaining = remaining.slice(maxLines * avgCharsPerLine);
});
}
// 初始化 + resize 監聽重新分發

完整代碼見原文(含 jQuery 版實現,處理中文斷字、響應式重算)。
| 維度 | CSS Shapes | Multi-column | JS 分塊 |
|---|---|---|---|
| 語義/可維護性 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐ |
| 瀏覽器兼容 | 現代瀏覽器 | IE10+ | 全兼容 |
| 複雜輪廓支援 | ⭐⭐⭐⭐⭐ (polygon/alpha) | ⭐ (矩形のみ) | ⭐⭐⭐⭐ (任意形狀) |
| 動態內容適應 | ⭐⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐ (需重算) |
| 性能 | 原生極快 | 原生極快 | 需防抖優化 |
| 無障礙/SEO | 完美 | 完美 | 需注意內容重複/隱藏 |
@supports (shape-outside: circle()) 降級為矩形浮動shape-outside: url(mask.png)文字環繞圖片不再是排版難題,選對工具,讓網頁呈現雜誌級閱讀美感。