Is it possible to have a table in the center in Github gist markdown?

馋奶兔 提交于 2019-11-30 17:00:05

问题


Is it possible to have a table in the center in Github gist markdown? If so, how?

I've used the following syntax to create a table on a markdown file:

 Somehow the table is always flushed to the left!!!

|Column1|Column1|Column1|
|:----|:----:|----:|
|Column1|Column1|Column1|

But the table is flushed left, see https://gist.github.com/alvations/5095d3bcc0eec357c78f1c21a49e334f

Is it possible to have the table at the center of the page when viewing?

I've tried the suggestion from https://stackoverflow.com/a/24639508/610569 to use:

Somehow the table is always flushed to the left!!!

<center>

|Column1|Column1|Column1|
|:----|:----:|----:|
|Column1|Column1|Column1|

</center>

And the table disappears when viewing, see https://gist.github.com/alvations/cd3495e7107b7701cf1cf1da2a839534

I've also tried https://stackoverflow.com/a/12118349/610569 :

Still on the left!!!
<p align="center">

|Column1|Column1|Column1|
|:----|:----:|----:|
|Column1|Column1|Column1|
</p>

But it's still on the left, see https://gist.github.com/alvations/23c18681df7a6bbf175d0e8c2cfccba3

Images for all 3 versions above:


回答1:


In short, it's not possible. GitHub does not allow you to define your own styling.

First, note that there is no mention of the ability to apply any styling to any block level types in the GitHub Flavored Markdown spec (see the tables section). As your examples show, you are aware that you can center text within table cells, but that only applies to the cells and has no effect on the parent table (which is how HTML and CSS work and is not specific to Markdown or GitHub).

There are a few ways to define custom styles for HTML (which Markdown generates), but GitHub does not permit them.

One such way is to define CSS rules. However, right in the spec, GitHub explicitly states that they do not allow <style> tags.

Another way is to include raw HTML right in the Markdown document (with inline styles). However, for security reasons, GitHub is very selective about what they allow. In the Markup project they define the filters they apply to all markup languages they support (including, but not limited to Markdown). In pertinent part, the docs explain (emphasis added):

  1. The HTML is sanitized, aggressively removing things that could harm you and your kin—such as script tags, inline-styles, and class or id attributes. See the sanitization filter for the full whitelist.

Given the above, it is simply not possible to define your own styling for documents hosted on GitHub. That said, some expect to be able to define styling within the Markdown syntax itself. However, the original Markdown rules explain (emphasis added):

Markdown’s syntax is intended for one purpose: to be used as a format for writing for the web.

Markdown is not a replacement for HTML, or even close to it. Its syntax is very small, corresponding only to a very small subset of HTML tags. The idea is not to create a syntax that makes it easier to insert HTML tags. In my opinion, HTML tags are already easy to insert. The idea for Markdown is to make it easy to read, write, and edit prose. HTML is a publishing format; Markdown is a writing format. Thus, Markdown’s formatting syntax only addresses issues that can be conveyed in plain text.

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself.

As it is not a "publishing format," providing a way to style your document is out-of-scope for Markdown. Which leaves us with the ways which GitHub explicitly disallows. Therefore it is not possible to center a table (or apply any other custom styling) on GitHub.


As an aside, while GitHub uses the CommonMark spec (with extensions) rather than the original Markdown Rules, I make reference to the original rules as the section I quote from discusses the philosophy behind various design decisions made when creating Markdown. Markdown's (and CommonMark's) behaviors are directly related to that philosophy. While the CommonMark spec does not get into the design decisions (expect when it differs from Markdown), it does make reference to some of the points discussed in the very paragraph I quoted above. And nowhere does it contradict that philosophy. Therefore, I consider it relevant to the expectations we should have about what is and what is not part of CommonMark, and by extension, GitHub Flavored Markdown.


For completness, let's examine each of the examples provided by the OP.

  1. The first example is simply a table with the middle column aligned "center". If we "view source" (or use the browser's "inspect" tool), we see the following HTML was generated:

    <table>
        <thead>
            <tr>
                <th align="left">Column1</th>
                <th align="center">Column1</th>
                <th align="right">Column1</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td align="left">Column1</td>
                <td align="center">Column1</td>
                <td align="right">Column1</td>
            </tr>
        </tbody>
    </table>
    

    Note that align="center" is only defined on the middle cell of each row. As such styling is only inherited by children elements, not parent elements, this does not get applied to the the table as a whole. As an aside, the align attribute is not even mentioned in the HTML5 spec (that I could find); however, in the HTML 4.01 spec, you can define an align attribute on a table element or any of its children which is then inherited by the children of that element only. Of course as established above, Markdown does not provide a mechanism to define alignment on anything except the cells. But even if you could define align on the table element, the spec explains that "[t]his attribute specifies the alignment of data and the justification of text in a cell." Therefore, if would still have no effect on how the table is positioned in its parent element.

  2. The second example is a table wrapped in a <center> element. A look at the source HTML reveals that the <center> tag was stripped out. In fact, a look at GitHub's whitelisted elements reveals that center elements are not allowed and stripped out.

  3. The third example attempts to wrap the table in a paragraph with align="center" defined on the paragraph. However, note the (interpreted) HTML:

    <p align="center"></p>
    <table>
        <thead>
            <tr>
                <th align="left">Column1</th>
                <th align="center">Column1</th>
                <th align="right">Column1</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td align="left">Column1</td>
                <td align="center">Column1</td>
                <td align="right">Column1</td>
            </tr>
        </tbody>
    </table>
    <p></p>
    

    According to the HTML5 spec:

    A p element's end tag may be omitted if the p element is immediately followed by an... table... element.

    Therefore, the paragraph does not actually wrap the table, but is implicitly closed by the table's opening tag.

    But that got me curious. What if you used a div instead of a paragraph. But that makes no difference. As we've established earlier, the align attribute only effects cell text. You need to assign a style to change the position of a table on the page and Github explicitly disallows defining your own styles.




回答2:


As you can see in the following image, GitHub automatically renders tables so that they're already taking up the full width. Because of this, you cannot center the text that GitHub's Markdown renderer generates (aka the table is really, really fat and technically already centered).



来源:https://stackoverflow.com/questions/44172954/is-it-possible-to-have-a-table-in-the-center-in-github-gist-markdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!