How do you center text in Gitlab markdown?

青春壹個敷衍的年華 提交于 2020-07-06 09:10:15

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/53273660/how-do-you-center-text-in-gitlab-markdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!