user736893
user736893

Reputation:

Left-align headers in Markdown table?

Using the table example from "Markdown Cheatsheet" on GitHub, you get this:

| Tables        | Are           | Cool  |
| ------------- |:-------------:| -----:|
| col 3 is      | right-aligned | $1600 |
| col 2 is      | centered      |   $12 |
| zebra stripes | are neat      |    $1 |

enter image description here

My question is, is there any way to left-align the header cells while retaining the alignment in the image? IE, I want to align the header differently than the body.

Upvotes: 76

Views: 53242

Answers (3)

yeOldeDataSmythe
yeOldeDataSmythe

Reputation: 833


Markdown:

| Tables        | Are           | Cool  |
|:------------- |:-------------:| -----:|
| col 3 is      | right-aligned | $1600 |
| col 2 is      | centered      |   $12 |
| col 1 is      | left-aligned  |   $42 |
| zebra stripes | are neat      |    $1 |

Result:

Tables Are Cool
col 3 is right-aligned $1600
col 2 is centered $12
col 1 is left-aligned $42
zebra stripes are neat $1

Note that the colon (":") on the second row, and the second character is what worked to The other option is to move the numbers to the left-most column (as below)

Amount | Items
------:|:-----
    20 | Wooden Boards
     5 | Old Parts

Try it on StackEdit.

It does not seem to work putting the ---- line above the headers.

Upvotes: 44

konyak
konyak

Reputation: 11706

On the second line, I would change to this to align everything (headers and content) to the left:

| :------------ | :-------------- | :----- |

Notice the colons on the left of each column.

Upvotes: 9

Waylan
Waylan

Reputation: 42497

It depends on which implementation you are using.

Tables are a non-standard feature of Markdown and each implementation which supports them does so differently. For example, the "cheetsheet" pointed to in the question is within the Markdown Here project. That project's Readme includes the following explanation:

To discover what can be done with Markdown in Markdown Here, check out the Markdown Here Cheatsheet and the other wiki pages.

So that "cheetsheet" is specific to the implementation used by Markdown Here.

GitHub has documented their implementation of Markdown as an extension of the Commonmark spec (Commonmark is a Markdown variant which does not support tables). According to example 192, the column headers receive the same alignment as the column cells:

| abc | defghi |
:-: | -----------:
bar | baz

<table>
<thead>
<tr>
<th align="center">abc</th>
<th align="right">defghi</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">bar</td>
<td align="right">baz</td>
</tr></tbody></table>

So, you need to check the specific implementation of Markdown you are using and read that implementation's documentation. However, personally, I have never come across an implementation which allows you to define separate alignment for the headers from the cells. In my experience, either you get headers which match the cells, or headers which have no alignment assigned.

Upvotes: 31

Related Questions