在Markdown文件中,是否可以将表格居中显示?

67

我有一张表:

    | This | Is | A | Table |
    | :--- | -- | - | ----: |
    | foo  | ba | r | elbaT |

我希望表格能够居中显示在我的Markdown文件中,而不是左对齐。 我不是想要对齐文本,而是整个表格本身。 我需要使用HTML / CSS才能实现我想要的效果吗?

这是为Apiary.io项目准备的。


有关MkDocs材料的内容:https://github.com/squidfunk/mkdocs-material/issues/3430#issuecomment-1005973474 - phoenix
6个回答

83

如果您使用标准文档,请像这样使用<center>标签。

蓝图

FORMAT: 1A
HOST: http://www.google.com

# Tables
Notes API is a *short texts saving* service similar to its physical paper presence on your table.

<center>

| Tables   |      Are      |  Cool |
|----------|:-------------:|------:|
| col 1 is |  left-aligned | $1600 |
| col 2 is |    centered   |   $12 |
| col 3 is | right-aligned |    $1 |

</center>

# Group Notes

(...)

预览


如果您使用“新文档”,则不可能将表格居中显示(因为表格占用了整个列的宽度)。

预览


2
虽然这至少直接回答了我的问题,但我在谷歌上第二个搜索结果(对我来说)是一个StackOverflow的问题,询问为什么 center 被弃用。感谢您的努力。 - Meredith

34
|-|用于表示表头,:用于表示文本对齐。文本默认为左对齐|:-|用于左对齐表头和列中的文本。 |-:|用于右对齐表头和列中的文本。 |:-:|用于居中对齐表头和列中的文本。

6
有时候,实际的答案就在底部。 - AIDoubt
10
问题是关于将表格本身居中,而不是它的内容。我能理解为什么会有误解。 - Bryan K
在原始答案中,对齐方式与符号错误地关联起来了。正确的关联应该是:- 表示左对齐,- 表示右对齐。 - Dheemanth Bhat

16

一个简单的方法似乎被大家忽视了,就是将表格包含在一个 div 中,并在其上使用 align="center" 属性。

<div align="center">

| Tables   |      Are      |  Cool |
|----------|:-------------:|------:|
| col 1 is |  left-aligned | $1600 |
| col 2 is |    centered   |   $12 |
| col 3 is | right-aligned |    $1 |

</div>

<center>一样工作。 不需要再担心<center>被弃用了。 ;)


@Gangula 确保这将使表格中的文本居中对齐,但要使表格本身在 markdown 文件中居中对齐(这实际上是 op 想要的),您需要使用 <div align="center"> - Rohan Lekhwani
哦,我的错。我误解了问题。 - Gangula
align属性也已被弃用。 - undefined

14

是的。您可以在API Blueprint中使用GFM表格——请查看http://docs.tables.apiary.io,以获取下面蓝图源代码的呈现版本。

FORMAT: 1A

# Tables API 
Note: Tables can be handcrafted or generated at <http://www.tablesgenerator.com/markdown_tables>.

## Table 1
**Discussion option 1**

| Tables   |      Are      |  Cool |
|----------|:-------------:|------:|
| col 1 is |  left-aligned | $1600 |
| col 2 is |    centered   |   $12 |
| col 3 is | right-aligned |    $1 |

# Message [/pages]
## Create a Message [POST]

### Table 2
**Discussion option 2**

| Tables   |      Are      |  Cool |
|----------|:-------------:|------:|
| col 1 is |  left-aligned | $1600 |
| col 2 is |    centered   |   $12 |
| col 3 is | right-aligned |    $1 |

+ Request (application/json)

    ## Table 3
    **Discussion option 3**

    | Tables   |      Are      |  Cool |
    |----------|:-------------:|------:|
    | col 1 is |  left-aligned | $1600 |
    | col 2 is |    centered   |   $12 |
    | col 3 is | right-aligned |    $1 |

    + Headers

            Authorization:Bearer tokenString

    + Body

            { ... }

+ Response 201

5
这个问题不是关于“将整个表格放在页面中心”的吗?而这个回答则是关于“将单列文本居中对齐”的吗?我找不到任何一种Markdown变体允许居中或缩进文本(除了预格式/代码块,但它们也会将定义表格的Markdown字面化)。 - jackr
好的观点!我误解了答案 - 据我所知,在Apiary内部没有办法做到这一点。我会及时更新嵌入式文档中是否/如何实现的情况。 - Zdenek

6

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接