Word 中的文字環繞功能在網頁上並非原生單一屬性即可完成,但善用 HTML 元素與 CSS 浮動同樣能達到優雅的排版效果。本文說明「文字繞圖」與「文字繞文字」兩大常見場景的實作技巧。
<table cellpadding="15" width="200">
<tr>
<td bgcolor="#EEEEFF">
<img src="test.gif" height="60">
這裡是普通的排版,圖片比文字高,上方會出現難看空白。
</td>
</tr>
</table>
align="left" 或 align="right"<table cellpadding="15" width="200">
<tr>
<td bgcolor="#EEEEFF">
<img src="test.gif" hspace="1" align="left" height="60">
文字將自動環繞圖片左側流動,無留白。
</td>
</tr>
</table>
align="left":圖片靠左,文字繞右;align="right":圖片靠右,文字繞左。hspace / vspace:控制圖片與周圍文字的水平/垂直間距。align,改用 CSS float: left; margin-right: 10px;,語意更清晰、可維護性更高。利用巢狀表格或 CSS ::first-letter 實現首字放大、文字環繞:
<table align="left" cellspacing="0" cellpadding="0" width="200" border="1">
<tr>
<td>
<table align="left" cellspacing="0" cellpadding="0">
<font color="red" size="5"><b>在</b></font>
</table>
國內的 VB 網站中 vbgood 以每日更新...
</td>
</tr>
</table>
原理:外層表格限制寬度,內層表格包裹放大字元並 align="left",外層文字自動環繞內層表格。
<table align="left" cellspacing="0" cellpadding="0" width="200" border="1">
<tr>
<td>
<table align="left" cellspacing="0" cellpadding="0" bgcolor="#C0C0C0">
<tr><td><font color="red" size="5"><b>在</b></font></td></tr>
</table>
國內的 VB 網站中 vbgood 以每日更新...
</td>
</tr>
</table>
注意:內層表格必須包裹<tr><td>才能正確套用bgcolor。
.drop-cap::first-letter {
float: left;
font-size: 4em;
line-height: 1;
margin: 0.05em 0.1em 0 0;
color: #c0392b;
font-weight: bold;
/* 背景色需用 span 包裹首字 */
}
在國內的 VB 網站中...
或使用 initial-letter(支援度較新):
.drop-cap::first-letter {
initial-letter: 3; /* 佔 3 行高 */
color: #c0392b;
font-weight: bold;
}
align,配合 margin 控制間距。::first-letter + float 或 initial-letter,語意化、可維護、無巢狀表格。