Markdown使用-4 表格

Markdown本身有表格语法,它也支持HTML表格


目录

  1. Markdown表格语法
  2. HTML表格语法
  3. 在线编辑

Markdown表格语法

参考:Tables

Markdown的表格语法如下:

| Syntax      | Description | Description1 |
| ----------- | ----------- | ------------ |
| Header      | Title       | Title1       |
| Paragraph   | Text        | Text1        |

To add a table, use three or more hyphens (---) to create each column’s header, and use pipes (|) to separate each column. You can optionally add pipes on either end of the table.
SyntaxDescriptionDescription1
HeaderTitleTitle1
ParagraphTextText1

使用3个或更多的连字符(-)来创建每列的头,使用竖线(|)来分离每列

对齐

| Syntax      | Description | Test Text     |
| :---        |    :----:   |          ---: |
| Header      | Title       | Here's this   |
| Paragraph   | Text        | And more      |

You can align text in the columns to the left, right, or center by adding a colon (:) to the left, right, or on both side of the hyphens within the header row.
SyntaxDescriptionTest Text
HeaderTitleHere’s this
ParagraphTextAnd more

如果想要对齐每列(左,中,右),可以在连字符上加上分号(:),加在左边表示向左对齐,加在两侧表示居中对齐,加在右边表示向右对齐。


HTML表格语法

参考:HTML 表格

同样的,在Markdown上也可以使用HTML表格语法,相比之下,使用HTML语法可以实现更多的样式。

<table>
    <tr>
        <td>1行1列</td>
        <td>1行2列</td>
    </tr>
    <tr>
        <td>2行1列</td>
        <td>2行2列</td>
    </tr>
</table>
1行1列1行2列
2行1列2行2列

标签<table>表示表格,标签<tr>表示行,标签<td>表示列

边界

如果想要显示边界,在<table>属性中设置border

<table border="1">
    <tr>
        <td>1行1列</td>
        <td>1行2列</td>
    </tr>
    <tr>
        <td>2行1列</td>
        <td>2行2列</td>
    </tr>
</table>
1行1列1行2列
2行1列2行2列

表头

使用标签<th>表示表头

<table border="1">
    <tr>
        <th>表头</a>
        <th>第二个表头</a>
    </tr>
    <tr>
        <td>1行1列</td>
        <td>1行2列</td>
    </tr>
    <tr>
        <td>2行1列</td>
        <td>2行2列</td>
    </tr>
</table>
表头 第二个表头
1行1列1行2列
2行1列2行2列

表头<th>即可用于行,也可表示列

<table border="1">
    <tr>
        <th>姓名</th>
        <td>Bill Gates</td>
    </tr>
    <tr>
        <th>电话</th>
        <td>555 77 854</td>
    </tr>
    <tr>
        <th>电话</th>
        <td>555 77 855</td>
    </tr>
</table>
姓名Bill Gates
电话555 77 854
电话555 77 855

标题

使用标签<caption>设置表格标题

<table border="1">
    <caption>电话簿</caption>
    <tr>
        <th>姓名</th>
        <td>Bill Gates</td>
    </tr>
    <tr>
        <th>电话1</th>
        <td>555 77 854</td>
    </tr>
    <tr>
        <th>电话2</th>
        <td>555 77 855</td>
    </tr>
</table>
电话簿
姓名Bill Gates
电话1555 77 854
电话2555 77 855

跨越多行或多列

在标签<td>或者<th>上设置属性colspan表示跨越多列,设置属性rowspan表示跨越多行

<table border="1">
    <caption>电话簿</caption>
    <tr align="center">
        <th>姓名</th>
        <td>Bill Gates</td>
        <td colspan="2">hehe</td>
    </tr>
    <tr>
        <th>电话</th>
        <td>555 77 854</td>
        <td>555 77 855</td>
        <td>555 77 856</td>
    </tr>
</table>
电话簿
姓名Bill Gateshehe
电话555 77 854555 77 855555 77 856
<table border="1">
    <caption>电话簿</caption>
    <tr align="center">
        <th>姓名</th>
        <th>电话</th>
    </tr>
    <tr>
        <td>Bill Gates</td>
        <td>555 77 854</td>
    </tr>
    <tr>
        <td rowspan="2" align="center">hehe</td>
        <td>555 77 855</td>
    </tr>
    <tr>
        <td>555 77 856</td>
    </tr>
</table>
电话簿
姓名电话
Bill Gates555 77 854
hehe555 77 855
555 77 856

对齐

使用属性值align设置对齐方式,它可以应用于每一种标签:

  • 在标签<table>中设置,表示表格居中
  • 在标签<tr>中设置,表示行文字对齐
  • 在标签<td>中设置,表示列文字对齐

在线编辑

参考:Tables Generator – 在线生成 LaTeX、HTML、Markdown 表格

有一个在线表格生成器:Tables Generator,可用于生成Markdown表格和HTML表格

还有一个W3C的在线编辑器:html_tables

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值