萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML 腳本教學

HTML 文字環繞排版完全指南:文字繞圖、文字繞文字與背景色處理

HTML 文字繞排核心技術:align 屬性與嵌套表格布局

早期 CSS Float/Grid 未普及時,利用 `` 和嵌套 `

` 實現文字環繞圖片/文字,是經典排版技巧。

場景一:文字繞圖 (img align)

<!-- 問題:圖片高於文字,文字頂部留白 -->
<table cellpadding="15" width="200">
  <tr><td bgcolor="#EEEEFF">
    <img src="test.gif" height="60"> 文字內容...
  </td></tr>
</table>

<!-- 解決:添加 align="left" -->
<table cellpadding="15" width="200">
  <tr><td bgcolor="#EEEEFF">
    <img src="test.gif" hspace="1" align="LEFT" height="60"> 文字內容...
  </td></tr>
</table>

原理:`align="left/right"` 使圖片浮動,文字流環繞。`hspace/vspace` 控制間距 (已廢棄,現用 CSS margin)。

場景二:文字繞文字 (嵌套表格)

<!-- 基礎結構:外層表格限制寬度,內層表格承載大字 -->
<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>

關鍵點:內層 `

` 使大字浮動,外層 `
` 文字環繞。這是 CSS `float` 出現前的變通方案。

場景三:帶背景色的文字繞文字

<!-- 錯誤:td 直接包裹 font,背景不生效 -->
<table align="left" ...><td><table align="left" bgcolor="#C0C0C0"><font>在</font></table>...</td></table>

<!-- 正確:內層 table 必須有 tr/td 結構 -->
<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>

原因:`

` 直接包裹 `` 無效,必須有 `
` 結構才能渲染背景色。

現代 CSS 替代方案 (強烈推薦)

<!-- 文字繞圖 -->
<div class="text-wrap-image">
  <img src="test.gif" alt="示例" style="float:left; margin-right:10px; height:60px;">
  文字內容會自然環繞圖片...
</div>

<!-- 文字繞文字 (首字下沉/放大首字) -->
<div class="drop-cap">
  <span class="big-char">在</span>
  國內的VB網站中vbgood以每日更新...
</div>

<style>
.text-wrap-image { overflow: hidden; /* 清除浮動 */ }
.big-char {
  float: left;
  font-size: 4em;
  line-height: 1;
  padding: 0.1em 0.1em 0 0;
  color: red;
  font-weight: bold;
  /* 背景色方案 */
  background: #c0c0c0;
  padding: 0 0.1em;
  border-radius: 2px;
  margin-right: 0.05em;
}
</style>

進階:CSS Shapes 非矩形環繞

.shape-wrap {
  float: left;
  width: 200px;
  height: 200px;
  shape-outside: circle(50% at 50% 50%); /* 文字繞圓形 */
  /* shape-outside: polygon(...); 文字繞多邊形 */
  /* shape-outside: url(mask.png); 文字繞圖片透明區域 */
}

現代瀏覽器支持 `shape-outside` 實現真正的非矩形文字環繞,不再需要嵌套表格 Hack。

copyright © 萬盛學電腦網 all rights reserved