276

Does markdown support native text-alignment without usage html + css?

fat
  • 6,435
  • 5
  • 44
  • 70

15 Answers15

236

native markdown doesn't support text alignment without html + css.

Teaqu
  • 2,953
  • 1
  • 14
  • 21
  • 97
    Therefore wrap your text in `

    ` and `

    ` which should work in any markdown
    – SDJMcHattie Mar 31 '16 at 13:36
  • 17
    **Github users:** inline styles do not work on github and are not included as extended features of Github Flavored Markdown. This is [all github supports as of Jan 2017](https://guides.github.com/features/mastering-markdown/). There are many online markdown testers that say they comply with GFM and show things like inline styles working, but github markdown [pretty much]* doesn't support HTML/CSS at the moment. *`
    ` works so there might be some hidden tags that work.
    – Govind Rai Jan 10 '17 at 23:22
  • 30
    **Github users:** As of *6/8/2017*, Diego Vinícius's answer below successfully centers text in markdown files. Just wrap your text in a `p` tag with `align` set to `center`, like so: `

    centered text

    `
    – Kröw Jun 08 '17 at 08:10
  • 3
    BTW, which would be better to use, `
    ` or `

    `? A `p` is a paragraph, so maybe `div` would be a more neutral and better alternative?

    – VasiliNovikov Jun 24 '17 at 08:19
  • 2
    @SDJMcHattie This doesn't work when converting .md to .pdf. – Marc Le Bihan Oct 20 '19 at 05:45
  • Anybody know _why_ text alignment is such a challenge and/or bad idea for native markdown? Seems like such a strange omission. – k3davis Jun 18 '20 at 11:50
  • 1
    @k3davis alignment is a styling specification. Markdown specifies structure. – Peter Wood Dec 07 '21 at 12:23
  • 1
    This answer has earned more points than my account... for a fancy "no." Gotta love it. Btw, anyone else find it ironic that we are using md syntax to discuss md syntax? – Nate T Feb 08 '22 at 23:08
  • 2
    @PeterWood and yet it supports (or is commonly extended to support?) underline, bold, etc. which have nothing to do with structure. the distinction seems arbitrary. – k3davis Mar 08 '22 at 14:55
  • **In Jupyter notebooks:** Inline styles and style definitions in markdown don't work. @mdarens native markdown table syntax answer below does work but looks miihh!? Saying `from IPython.display import *; display(HTML(""))` in a code cell **and running it** retroactively affects styles for the whole notebook and lets you define a style class like "div.centered". Jupyter also has a standard location for a "custom.css" file. – SteveWithamDuplicate Aug 08 '22 at 18:58
137

In order to center text in md files you can use the center tag like html tag:

<center>Centered text</center>
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
Erwann Blancart
  • 1,459
  • 1
  • 9
  • 4
52

I known this isn't markdown, but <p align="center"> worked for me, so if anyone figures out the markdown syntax instead I'll be happy to use that. Until then I'll use the HTML tag.

Diego Vinícius
  • 2,125
  • 1
  • 12
  • 23
52

The div element has its own alignment attribute, align.

<div align="center">
  my text here.
</div>
cbilliau
  • 987
  • 9
  • 20
25

It's hacky but if you're using GFM or some other MD syntax which supports building tables with pipes you can use the column alignment features:

|| <!-- empty table header -->
|:--:| <!-- table header/body separator with center formatting -->
| I'm centered! | <!-- cell gets column's alignment -->

This works in marked.

mdarens
  • 464
  • 4
  • 5
  • 1
    How would one go around this to apply for a heading? If I use a plain "#" inside the "|" it appears verbatim. – nilon Aug 07 '17 at 03:32
  • 1
    This only appears to work for text. I'm trying to center an image. – Vince Apr 22 '18 at 21:04
14

In Github You need to write:

<p align="justify">
  Lorem ipsum
</p>
Oleg Pro
  • 2,323
  • 1
  • 15
  • 23
8

For Markdown Extra you can use custom attributes:

# Example text {style=text-align:center}

This works for headers and blockquotes, but not for paragraphs, inline elements and code blocks.

A shorter version (but not supported in HTML 5):

# Example text {align=center}
  • @AlmostPitt As mentioned, it's a Markdown Extra specific feature, it's not likely to work elsewhere. –  Oct 11 '19 at 09:29
7

A qualified 'yes' using table syntax. For example, you can center-align plain text as follows:

| |
| :-: |
| Excerpts from Romeo and Juliet (arr. V. Borisovsky) |

This yields:

Excerpts from Romeo and Juliet (arr. V. Borisovsky)

Note that you can still use Markdown inside an HTML block. For example:

<div style="font-style: italic; text-align: center;" markdown="1">

## Excerpts from Romeo and Juliet (arr. V. Borisovsky)
### Sergei Prokofiev
#### Timothy Ridout, viola ∙ Frank Dupree, piano

</div>
chmaynard
  • 121
  • 1
  • 9
3

I found pretty useful to use latex syntax on jupyter notebooks cells, like:

![good-boy](https://i.imgur.com/xtoLyW2.jpg  "Good boy on boat")

$$\text{This is some centered text}$$
JackS
  • 423
  • 3
  • 12
3

For most markdown parsers, there is no way to natively align text. A few parsers support this syntax: -> centered <-.

But, if your parser doesn't support it, you can use HTML for that, even allowing you to render markdown inside the tags.

When using any element such as a title, you can use an equivalent html tag, for instance

# Title
## title 2

is equivalent to

<h1> Title </h1>
<h2> Title 2 </h2>

With title, for instance, you can align the text using the following attribute:

<!-- title only -->
<h1 align="center"> Title </h1>

<!-- title with div -->
<div align="center"> <h1 align="center"> Title inside div! </h1> </div>

But sometimes you don't want to use HTML, because it butches the capability of using markdown inside it, in these cases, you can use span, which allows you to render markdown inside HTML tags:

<!-- title with span (you can render emojis or markdown inside it) -->
<span align="center"> <h1> :star: ~~Centered~~ </h1> </span>

Please note that this attribute is deprecated, while it's deprecated, it is also the only one that works with some flavors of markdown, such as Github's markdown

Overclocked Skid
  • 465
  • 5
  • 12
2

For python markdown with attr_list extension the syntax is a little different:

{: #someid .someclass somekey='some value' }

Example:

[Click here](http://exmaple.com){: .btn .btn-primary }

Lead information paragraph
{: .lead }
Janusz Skonieczny
  • 17,642
  • 11
  • 55
  • 63
2

For Readme file of Github Using div can center everything-

<div align="center">
    Any Text/Card/Item
</div>

but using p this way doesn't help for all item-

<p align="center">
    Any Text/Card/Item
</p>
1

I was trying to center an image and none of the techniques suggested in answers here worked. A regular HTML <img> with inline CSS worked for me...

<img style="display: block; margin: auto;" alt="photo" src="{{ site.baseurl }}/images/image.jpg">

This is for a Jekyll blog hosted on GitHub

Vince
  • 3,962
  • 3
  • 33
  • 58
1

You can just use this for headers

# <center>Title</center>

This will center your header "title"

Bercove
  • 987
  • 10
  • 18
-15

To center align, surround the text you wish to center align with arrows (-> <-) like so:

-> This is center aligned <-
Glen Selle
  • 3,966
  • 4
  • 37
  • 59
shailesh
  • 865
  • 1
  • 11
  • 18
  • 3
    I'm test this option in some online Markdown. Can you add an URL or tell us in wich Markdown this sentence work? – equiman Oct 08 '13 at 14:17
  • This works in Discount (http://www.pell.portland.or.us/~orc/Code/discount/), a C implementation of the original markdown. – Teaqu Nov 12 '13 at 19:59
  • And also in [RDiscount](http://rubygems.org/gems/rdiscount) its `ruby` implementation. Thanks! – jibiel Dec 13 '13 at 22:01
  • You have to add this plugin to have this working: https://www.npmjs.com/package/markdown-it-center-text – Lukas Liesis Oct 17 '16 at 10:45
  • 1
    Doesn't work in Notepad++ (python markdown + Live HTML plugin) – 816-8055 Oct 25 '16 at 08:40
  • @PierreArlaud Sorry for being OT, but: Does Redmine actually support markdown? I've been waiting for ages for this app to support MD .... – Bunjip May 17 '17 at 10:01
  • @Bunjip Like everyone else, I guess they just did whatever they wanted so that their format looks like a markdown dialect… – Pierre Arlaud May 17 '17 at 10:05
  • @PierreArlaud Alright, that's my impression, too. Having been around for many years now, RN probably has started even before the markdown trend has started. Guess, folks at RedNotebook would still call there style 'Rich Text Format' or such. Nevemind, with real md support and a fresh and modern UI, RedNotebook could become an awesome tool even these days - [as I've imagined the other day](https://softwarerecs.stackexchange.com/questions/14045/journal-and-note-taking-desktop-app-for-ubuntu-with-markdown-support-modern-dis) – Bunjip May 17 '17 at 12:47
  • This answer was really helpful, because I was looking for an elegant syntax to centre text (and perhaps blocks of text). Thanks for this! – Paul Harris Nov 09 '21 at 06:17