Skip to main content

How to Center Text in Markdown

Markdown has no centering syntax, so you drop in a little HTML. This guide covers the one method that survives GitHub's sanitizer, the CSS method for renderers you control, and how to center headings, images, badges, and tables.

Short answer

Wrap the content in <div align="center">. It works on GitHub, GitLab, VS Code, Obsidian, and every static site generator. Inline CSS like style="text-align: center" is stripped by GitHub, and <center> is removed entirely.

Method 1: The align Attribute

The align attribute is deprecated HTML, but it is the most portable way to center content in Markdown. GitHub allows it on div, p, h1h6, img, td, and th.

Centered text
<div align="center">This text is centered.</div>

<p align="center">So is this paragraph.</p>
Preview
This text is centered.

So is this paragraph.

Centered README header
<h1 align="center">Project Name</h1>

<p align="center">A one-line description that sits under the title.</p>
Why a deprecated attribute? GitHub's Markdown pipeline sanitizes HTML and removes anything that could restyle the page, including style, class, and id. The align attribute stayed on the allowlist, which makes it the only alignment tool that works there.

Keeping Markdown Alive Inside the Block

Markdown is not parsed inside a raw HTML block. Add a blank line after the opening tag and before the closing tag: the blank line ends the HTML block, so headings, bold, links, and tables in between are parsed normally. This is the pattern behind almost every centered README header.

Blank lines keep Markdown working
<div align="center">

## Project Name

**Everything in this block is still Markdown.**

</div>
Preview

Project Name

Everything in this block is still Markdown.

Without the blank lines you get literal **asterisks** and a line starting with a stray # instead of a heading.

Method 2: Inline CSS

If you control the renderer — a static site generator, a docs platform, a Jupyter notebook, a local preview — inline CSS is the modern, non-deprecated option. It is also the only way to combine alignment with other styling in one tag.

text-align with inline CSS
<p style="text-align: center;">Centered with inline CSS.</p>

<div style="text-align: center;">

This block is centered too.

</div>
Preview (CSS allowed)

Centered with inline CSS.

This block is centered too.

Do not use this in a GitHub or GitLab README. The markup renders, the style attribute is removed, and your text quietly stays left-aligned with no warning and no error.

Method 3: The <center> Tag (Avoid)

The <center> tag is the method most tutorials still recommend. It is obsolete in the HTML standard, and GitHub removes it because it is not on the allowlist of permitted tags. Browsers do still render it in plain HTML, so it works in a local preview and then fails where it matters.

Works locally, disappears on GitHub
<center>This text is centered.</center>
Swap it for <div align="center">. Same result, no obsolete markup, and it survives sanitization.

Centering Images and Badge Rows

Markdown image syntax has nowhere to put an alignment hint, so wrap the image in an aligned paragraph. The same wrapper centers a row of shields.io badges, which is why nearly every polished README opens with one.

Centered image with a fixed width
<p align="center">
  <img src="logo.png" alt="Project logo" width="240" />
</p>
Centered badge row
<p align="center">
  <img src="https://img.shields.io/badge/build-passing-brightgreen" alt="Build status" />
  <img src="https://img.shields.io/badge/license-MIT-blue" alt="License" />
  <img src="https://img.shields.io/badge/version-1.0.0-orange" alt="Version" />
</p>
Keep the alt text. Switching from ![alt](src) to an <img> tag is the most common place alt text gets dropped. See the image syntax guide for sizing and alt-text rules.

Centering a Table vs Centering a Column

These are two different things and they get mixed up constantly. Colons in the separator row set the alignment of the text inside a column. To move the whole table to the middle of the page, wrap it in an aligned block with blank lines around the Markdown.

Column alignment with colons
| Left | Center | Right |
| :--- | :----: | ----: |
| a    | b      | c     |
| dd   | ee     | ff    |
Preview
Left Center Right
a b c
dd ee ff
Centering the whole table
<div align="center">

| Plan  | Price | Seats |
| ----- | ----- | ----- |
| Free  | $0    | 1     |
| Team  | $12   | 10    |

</div>
Preview
Plan Price Seats
Free $0 1
Team $12 10

Common Mistakes

Alignment problems are almost always a sanitizer stripping your markup or a raw HTML block swallowing your Markdown.

Broken

<div style="text-align: center;">
Centered on my machine, left-aligned on GitHub.
</div>

Fixed

<div align="center">
Centered everywhere, including GitHub.
</div>
GitHub's HTML sanitizer strips every style attribute, so inline CSS silently does nothing in a README. The deprecated align attribute is explicitly allowed and is the only method that survives.

Broken

<center>Centered text</center>

Fixed

<div align="center">Centered text</div>
The <center> tag is obsolete and is not in GitHub's allowlist of permitted tags, so the whole tag is removed. Browsers still honor it in plain HTML, but do not rely on it in Markdown.

Broken

<div align="center">
**This stays literal asterisks**
</div>

Fixed

<div align="center">

**This renders as bold**

</div>
Markdown inside a raw HTML block is not parsed. Leave a blank line after the opening tag and before the closing tag so the parser drops back into Markdown mode.

Broken

| Left | Center |
| :--- | :----: |
| a    | b      |

Fixed

<div align="center">

| Left | Center |
| :--- | :----: |
| a    | b      |

</div>
Colons in the separator row center the text inside a column. They do not center the table itself on the page - for that you need to wrap the table in an aligned HTML block.

Quick Reference

SyntaxResultWorks on GitHub?
<div align="center">Centered blockYes
<p align="center">Centered paragraphYes
<h1 align="center">Centered headingYes
<img align="center" ...>Centered imageYes
<div align="right">Right-aligned blockYes
<p style="text-align: center">Centered paragraphNo - style stripped
<center>Centered blockNo - tag removed
| :---: |Centered column textYes (column only)

Platform Support

Alignment depends entirely on how much HTML the renderer lets through. Document platforms allow some. Chat apps allow none.

PlatformSupported?Notes
GitHubPartialalign="center" works on div, p, h1-h6, img, td, and th. All style attributes and the <center> tag are stripped.
GitLabPartialSame picture as GitHub: the align attribute is allowed, inline CSS is sanitized away.
VS Code previewYesRenders both the align attribute and inline CSS in the built-in preview.
ObsidianYesReading view honors align and inline CSS. Live Preview can lag behind on raw HTML blocks.
Jupyter / R MarkdownYesInline CSS and the align attribute both render in notebook and knitted HTML output.
Docusaurus / VitePress / AstroYesMDX and remark-based pipelines pass HTML through, so both methods work.
NotionNoPasted HTML is converted to Notion blocks. Alignment is set from the block menu, not Markdown.
DiscordNoNo HTML and no alignment syntax. Messages are always left-aligned.
SlackNomrkdwn has no HTML and no alignment. Even Block Kit has no text-align option.
RedditNoHTML is stripped in both old and new Reddit. There is no centering workaround.
CommonMarkPartialThe spec defines no alignment syntax. Raw HTML passes through if the renderer allows it.

Frequently Asked Questions

How do you center text in Markdown?

Markdown itself has no centering syntax, so you embed a small piece of HTML. Wrap the content in <div align="center"> or <p align="center"> for the widest compatibility, including GitHub and GitLab. In renderers that allow inline CSS, <p style="text-align: center;"> also works.

Why does text-align: center not work on GitHub?

GitHub sanitizes the HTML in Markdown files and aggressively removes anything that could affect the page, including inline style attributes, class names, and IDs. Your markup renders, but the style attribute is gone, so the text stays left-aligned. Use the align attribute instead.

Does the <center> tag work in Markdown?

It depends on the renderer. <center> is obsolete in HTML, and GitHub does not include it in its allowlist of permitted tags, so it is removed entirely. Local previews and some static site generators still render it. Use <div align="center"> instead so the result is the same everywhere.

How do I center an image in a README?

Wrap the image in an aligned paragraph: <p align="center"><img src="logo.png" alt="Logo" width="240" /></p>. You can also put align="center" directly on the img tag, but wrapping is more predictable when the image sits next to other content.

How do I center a heading in Markdown?

Replace the # syntax with the HTML equivalent and add the attribute: <h1 align="center">Project Name</h1>. On GitHub this still gets an anchor link, so table-of-contents links to the heading keep working.

How do I center a table in Markdown?

Wrap the Markdown table in <div align="center"> with a blank line before and after the table so the pipe syntax is still parsed as Markdown. The colons in a table separator row (:---: ) only center the text inside a column; they do not move the table on the page.

Why is my bold text showing literal asterisks inside a centered div?

Markdown is not parsed inside a raw HTML block. Put a blank line after the opening tag and another before the closing tag. The blank line ends the HTML block, so everything between the tags is parsed as Markdown again.

How do I right-align text in Markdown?

Use the same approach with a different value: <div align="right"> or <p align="right">. This is handy for attribution lines and signature blocks. The left value works too, though left is already the default.

Can I center text in Discord, Slack, or Reddit?

No. All three strip HTML and none of them expose a text-alignment option, so messages are always left-aligned. Padding a line with spaces or non-breaking spaces is unreliable because it depends on the reader's window width and font.

Related Markdown Guides

Centering is one of a handful of things Markdown leaves to HTML. Here are the others: