Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center an image in the README.md file on GitHub?

I've been looking at the Markdown syntax used in GitHub for a while, but except resizing an image to the width of the README.md page, I can't figure out how to center an image in it.

Is this possible? If so, how can I do it?

like image 244
Johnny Pauling Avatar asked Aug 23 '12 11:08

Johnny Pauling


People also ask

How do I center an image in Markdown?

Solution 2 : use the "align" deprecated attribute This is not recommended, but in some cases this is the only way to center the image. Insert this HTML snippet into your markdown file. You can also try to put the "align" attribute directly into the "img" tag.

How do I center align in GitHub Markdown?

To center images, text, and anything else in Github markdown and READMEs simply wrap the element in an HTML tag with the align attribute set to "center" .


2 Answers

This is from GitHub's support:

Hey Waldyr,

Markdown doesn't allow you to tweak alignment directly (see docs here: http://daringfireball.net/projects/markdown/syntax#img), but you can just use a raw HTML 'img' tag and do the alignment with inline css.

Cheers,

So it is possible to align images! You just have to use inline CSS to solve the problem. You can take an example from my GitHub repository. At the bottom of README.md there is a centered aligned image. For simplicity you can just do as follows:

<p align="center">   <img src="http://some_place.com/image.png" /> </p> 

Although, as nulltoken said, it would be borderline against the Markdown philosophy!


This code from my README file:

<p align="center">   <img src="https://github.com/waldyr/Sublime-Installer/blob/master/sublime_text.png?raw=true" alt="Sublime's custom image"/> </p> 

It produces this image output, except centered when viewed on GitHub:

<p align="center">   <img src="https://github.com/waldyr/Sublime-Installer/blob/master/sublime_text.png?raw=true" alt="Sublime's custom image"/> </p> 
like image 161
waldyr.ar Avatar answered Oct 01 '22 22:10

waldyr.ar


I've been looking at the Markdown syntax used in GitHub [...], I can't figure out how to center an image

TL;DR

No, you can't by only relying on Markdown syntax. Markdown doesn't care about positioning.

Note: Some Markdown processors support inclusion of HTML (as rightfully pointed out by @waldyr.ar), and in the GitHub case you may fallback to something like <div style="text-align:center"><img src="..." /></div>.

Beware that there's no guarantee the image will be centered if your repository is forked in a different hosting environment (CodePlex, Bitbucket, etc.) or if the document isn't read through a browser (Sublime Text Markdown preview, MarkdownPad, Visual Studio Web Essentials Markdown preview, ...).

Note 2: Keep in mind that even within the GitHub website, the way Markdown is rendered is not uniform. The wiki, for instance, won't allow such CSS positional trickery.

Unabridged version

The Markdown syntax doesn't provide one with the ability to control the position of an image.

In fact, it would be borderline against the Markdown philosophy to allow such formatting, as stated in the "Philosophy" section.

"A Markdown-formatted document should be publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions. "

Markdown files are rendered by github.com website through the use of the Ruby Redcarpet library.

Redcarpet exposes some extensions (such as strikethrough, for instance) which are not part of standard Markdown syntax and provide additional "features". However, no supported extension allow you to center an image.

like image 22
nulltoken Avatar answered Oct 01 '22 21:10

nulltoken