一、 概述
在數據排版時,表格是很常見的一種展示方式。然而,在表格中存在合併單元格的需求,以使表格更加清晰易讀。本文主要介紹markdown表格中如何實現單元格合併的方法。
二、 HTML 實體化
看過一些markdown教程的朋友們可能知道,markdown里沒有直接的單元格合併語法。在markdown中使用HTML語法是最快的解決方案。不過需要注意的是需要實體化HTML代碼,不然會出現markdown解析錯誤的情況。具體實體化方式如下:
<-- <td colspan="2"></td> -->
上述代碼中,span屬性指定了該單元格需要合併的列數,這裡合併了2列,其它屬性不再贅述。
三、用 : 調整列寬併合並單元格
首先,我們來看一下這樣一個表格:
| text | text | text | | ---- | ---- | ---- | | text | text | text | | text | text | text |
其中| -|- |-| 行表示此表格的列樣式。我們可以通過在第二個和第三個 | 之間使用 :(冒號)來調整每一列的寬度,如下:
| text | text | text | | :--: | :-: | --: | | text | text | text | | text | text | text |
上述代碼中,:- 表示左對齊, -: 表示右對齊, :-: 表示居中對齊。這樣,我們可以任意調整表格的列寬度。
現在,我們來看一下如何合併單元格。
| | text | text | | -| :-: | -: | | text | text | test | | | text | text |
通過在第一列的第二行和第三行保持空白,就可以將第二列的兩個單元格合併為一個。
四、使用 HTML 表格標記
Markdown是衍生自HTML的一個工具,因此,markdown支持使用HTML語法,如果Markdown用起來不太好使,我們可以試試HTML增強markdown表格的功能。下面以一個例子來說明:
<table> <tr> <th rowspan="2">tr:0 td:0</th> <th>tr:0 td:1</th> <th>tr:0 td:2</th> </tr> <tr> <td colspan="2">td:1 ~td:2</td> </tr> <tr> <td>tr:2 td:0</td> <td>tr:2 td:1</td> <td>tr:2 td:2</td> </tr> </table>
通過上訴代碼,我們可以實現以下這個表格:
tr:0 td:0 | tr:0 td:1 | tr:0 td:2 |
---|---|---|
td:1 ~td:2 | ||
tr:2 td:0 | tr:2 td:1 | tr:2 td:2 |
其中,屬性 colspan 和 rowspan 同樣是對合併單元格進行設置,可以更好的細化單元格的化單元格的合併。
五、使用擴展 markdown 語法
我們還可以使用 GitHub Flavored Markdown (GFM),即GitHub特有的Markdown語法。GFM的表格語法增加了符號|後的「 – 」和「 : 」符號,用來控制表格的對齊方式和寬度,同時,GFM還提供了表格的合併單元格語法。具體代碼如下:
| Function | Description | | ------- | ----------- | | `help()` | Display help window | | `destroy()` | Destroy the window | | `quit()` | Quit the application | | `align_*()` | Align the window | | `resize()` | Resize the window | | `move()` | Move the window | | `hide()` | Hide the window | | `show()` | Display the window | | `maximize()` | Maximize the window | | `minimize()` | Minimize the window | | `restore()` | Restore the window | | `raise()` | Raise the window | | `lower()` | Lower the window | | `title(title_text)` | Set the title of the window | | `menu(menu)` | Set the menu | | `colour(colour)` | Set the colour | | `background(colour)` | Set the background colour | | `gradient(colour)` | Set the gradient colour | | `icon(icon)` | Set the window icon |
通過使用 GFM 的表格語法,表格會更加清晰易讀。其中寬度調整方式和前面的方法相似,這裡不再贅述。在這裡我們着重介紹 `colspan` 和 `rowspan`的特性,如下:
| First Header | Second Header | | ------------- | ------------- | | Content Cell | Content Cell | | Content Cell | Content Cell | | New Section | | | col 1 | col width two | | col width one, | col 2 | | col width one across two cols | copied |
上述例子中,我們在第6行的第一個單元格使用了 `colspan` 屬性,從而強制讓其寬度跨越兩列。在第5行的第二個單元格我們同樣使用了` row span` 屬性使其高度跨越兩行,代碼寫法如下:
表格代碼: | First Header | Second Header | | ------------- | ------------- | | Content Cell | Content Cell | | Content Cell | Content Cell | | New Section | | | col 1 | col width two | | col width one, | col 2 | | col width one across two cols | copied | 翻譯結果: | 第一列 | 第二列 | | ----------- | ----------- | | Cell 1 | Cell 2 | | Cell 3 | Cell 4 | | 新的一列 | | | 跨越兩列的第1列 | 跨越兩行 | | 跨越兩列的第2列 | 被複制 |
六、總結
本文介紹了幾種在markdown表格中實現單元格合併的方法,包括HTML實體化、用:調整列寬併合並單元格、使用HTML表格標記和使用GFM語法。讀者還可以自己嘗試不同的方法,看哪種方式更符合自己的需求。
原創文章,作者:SLWXD,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/361937.html