Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you center text in Gitlab markdown?

After Gitlab switched its markdown engine to CommonMark it's no longer as easy to add things like custom styling to your markdown files.

I've used Gitlab for some time and for the longest time I've liked how nicely I could make my README.md file look, having a centered icon, title and description for my project. When they switched the engine all my markdown files that relied on having such stylings look really bad.

How do I center text in Gitlab after the transition to CommonMark?

like image 793
Simon Hyll Avatar asked Nov 13 '18 04:11

Simon Hyll


People also ask

How do I change alignment in Markdown?

Aligning text in native markdown is not possible. However, you can align the text using inline HTML tags. To justify, replace right with justify in the above. It works, but it breaks links like <div style="text-align: right"> [up](https://github.com/loretoparisi/CapsNet#CapsNet) </div> will be not rendered as a link.

How do I center align 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.

Does GitLab use Markdown?

GitLab uses "GitLab Flavored Markdown" (GFM). It extends the standard Markdown in a few significant ways to add some useful functionality. It was inspired by GitHub Flavored Markdown. You can also use other rich text files in GitLab.


1 Answers

Update

I checked out an old project of mine and noticed that it was already centered. It turns out that CommonMark allows you to set align="center" on <div> tags as well!

So, the simplest solution for centering is currently (note the empty line after the opening <div>:

<div align="center">  # This is gonna be centered! </div> 

Original

The only CommonMark html object that supports centering (as far as I know) is when you center a table cell. First you might've thought about just making a table and then using align="center", but the table won't take up the entire width of the page, so you'd get a small table on the left hand side of the page, which wouldn't solve our problem of wanting to center stuff relative to the page rather than the table.

To get around this we set the table width (not using CSS with an inline style tag since it's not supported in CommonMark at the time of writing) to a large value that will take up way more than the total width of the page. Since the max-width: CSS property of tables in Gitlab markdown is 100% it means that by setting a ridiculously high width="" we're essentially setting the table width: to 100% by using only the allowed pure html width="" property.

The markdown below if placed in e.g. README.md in your Gitlab project will result in a 100% width table with a centered image, title and description. The most notable part is that we're setting width="9999" on the <td> element in the table.

<table align="center"><tr><td align="center" width="9999"> <img src="/icon.png" align="center" width="150" alt="Project icon">  # MyProject  Description for my awesome project </td></tr></table>  ... More content 

Below you can see an example of how your README.md file could look on Gitlab using the above markdown. README.md file rendered on Gitlab

like image 134
Simon Hyll Avatar answered Sep 28 '22 05:09

Simon Hyll