Left-align headers in Markdown table?

26,608

Solution 1


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.

Solution 2

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.

Solution 3

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.

Share:
26,608
THE JOATMON
Author by

THE JOATMON

Hobbies include off-roading, gaming, reading and infuriating SO users by attempting to write code myself. "I must create a system, or be enslaved by another man's; I will not reason and compare: my business is to create." - William Blake

Updated on July 09, 2022

Comments

  • THE JOATMON
    THE JOATMON almost 2 years

    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?

  • THE JOATMON
    THE JOATMON almost 2 years
    The header cells in your result image are not left aligned. Did SO change something or was your answer always incorrect?
  • THE JOATMON
    THE JOATMON almost 2 years
    Yes, this left aligns everything, which is not what I want.