遇到雜誌式不規則文字環繞(如圓形、斜切、多邊形),傳統做法用圖片熱區或 Flash,但對 SEO、無障礙、載入速度皆有害。DIV+CSS 結合 float、shape-outside、position 可純前端實現。
<h1>、<p>、<span>),承載真實文字內容。margin、padding、position、z-index 精準推拉位置。shape-outside 讓文字真正沿非矩形邊緣流動。.irregular-wrap {
position: relative;
width: 600px; height: 400px;
background: url(shape-bg.png) no-repeat;
}
.irregular-wrap h1 {
position: absolute; top: 20px; left: 40px;
width: 200px; margin: 0;
}
.irregular-wrap p {
position: absolute; top: 80px; left: 40px;
width: 300px; line-height: 1.6;
}
.float-shape {
float: left;
width: 200px; height: 300px;
shape-outside: polygon(0 0, 200px 0, 150px 300px, 0 300px); /* 梯形 */
shape-margin: 1rem;
}
文字會沿多邊形邊緒環繞,無需額外容器定位。
| 方案 | SEO | 無障礙 | 維護性 | 相容性 |
|---|---|---|---|---|
| 圖片熱區 | 差(無文字) | 差 | 差(改文改圖) | 全 |
| Flash | 差 | 差 | 差(需專業工具) | 已棄用 |
| 定位容器 | 佳 | 佳 | 中(座標微調繁瑣) | 全 |
| shape-outside | 佳 | 佳 | 佳(聲明式) | 現代瀏覽器 |
shape-outside。