Right-align, justify-align and center text in Markdown

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. Right-align text in Markdown or Jupyter Notebook
  2. Justify text in Markdown or Jupyter Notebook
  3. Center text in Markdown or Jupyter Notebook

# Right-align text in Markdown or Jupyter Notebook

To right-align text in Markdown or Jupyter Notebook:

  1. Wrap the text in a div or a p tag.
  2. Set the style attribute on the `div element.
  3. Set the text-align CSS property to right.
README.md
<div style='text-align: right;'> bobbyhadz.com </div>

right align text in markdown

If you use VS Code, you can click on the Open Preview to the Side icon at the top to view the output in a separate window.

This approach also works in Jupyter Notebook.

Note that you must select Markdown from the dropdown menu as shown in this screenshot before running your code.

right align text in jupyter notebook markdown

However, GitHub strips the style attribute from markdown files.

To right-align text in GitHub, set the align attribute to right.

README.md
<div align="right"> bobbyhadz.com </div>

github right align text

If you need to right-align a link, make sure to use an a tag inside the div.

README.md
<div align="right"> <a href='https://bobbyhadz.com'>bobbyhadz.com</a> </div>

If you don't need to support GitHub, you can also use the style attribute.

README.md
<div style='text-align: right;'> <a href='https://bobbyhadz.com'>bobbyhadz.com</a> </div>

right align link

If you need to right-align in a table, add a colon on the right side of the hyphens below the headings.

README.md
| Name | Description | | -----: | -----------------------------: | | first | ABC 123. | | second | XYZ 456. | | third | Some longer text here ABC 123. |

Notice that there is a colon on the right side of the hyphens - below each heading.

right align text in table

If you need to right-align all text in the document, use a style tag at the top of your markdown file.

README.md
<style>body {text-align: right}</style> bobbyhadz.com

right align all text in document

The text-align property on the body element applies to the entire markdown document.

However, note that this approach doesn't work in GitHub markdown and Jupyter notebook.

# Justify text in Markdown or Jupyter Notebook

To justify text in Markdown or Jupyter Notebook:

  1. Wrap the text in a div or a p tag.
  2. Set the style attribute on the `div element.
  3. Set the text-align CSS property to justify.
README.md
<div style='text-align: justify;'> Some very long text here. The text must span multiple lines in order to demonstrate that this works. bobbyhadz.com </div>

justify text markdown

The text is spaced to line up its left and right edges to the left and right edges of the enclosing box, except the last line.

The same approach can be used to justify text in Jupyter Notebook.

justify text in jupyter notebook

If you need to support GitHub, set the align attribute to justify because GitHub automatically strips the style attribute.

README.md
<div align='justify'> Some very long text here. The text must span multiple lines in order to demonstrate that this works. bobbyhadz.com </div>

github justify text

The examples above use a div tag, however, you can also use a p tag to achieve the same result.

README.md
<p align='justify'> Some very long text here. The text must span multiple lines in order to demonstrate that this works. bobbyhadz.com </p>

If you need to justify all text in the document, use a style tag at the top of your markdown file.

README.md
<style>body {text-align: justify}</style> Some very long text here. The text must span multiple lines in order to demonstrate that this works. bobbyhadz.com

justify entire markdown document

The text-align property on the body element applies to the entire markdown document.

However, note that this approach doesn't work in GitHub markdown and Jupyter notebook.

# Center text in Markdown or Jupyter Notebook

To center text in Markdown or Jupyter Notebook:

  1. Wrap the text in a div or a p tag.
  2. Set the style attribute on the `div element.
  3. Set the text-align CSS property to center.
README.md
<div style='text-align: center;'> bobbyhadz.com </div>

center text in markdown

This approach also works in Jupyter Notebook.

Note that you must select Markdown from the dropdown menu as shown in this screenshot before running your code.

center text in jupyter notebook markdown

However, GitHub strips the style attribute from markdown files.

To center text in GitHub, set the align attribute to center.

README.md
<div align="center"> bobbyhadz.com </div>

center text in github markdown

If you need to center a link, make sure to use an a tag inside the div.

README.md
<div align="center"> <a href='https://bobbyhadz.com'>bobbyhadz.com</a> </div>

If you don't need to support GitHub, you can also use the style attribute.

README.md
<div style='text-align: center;'> <a href='https://bobbyhadz.com'>bobbyhadz.com</a> </div>

center link in markdown

If you need to center all text in the document, use a style tag at the top of your markdown file.

README.md
<style>body {text-align: center}</style> bobbyhadz.com

The text-align property on the body element applies to the entire markdown document.

However, note that this approach doesn't work in GitHub markdown and Jupyter notebook.

I've also written articles on:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.