Search code examples
htmlcsscolorsrestructuredtext

Color text in reStructuredText - files (.rst) without leading space character in front of :color:`text`


The text/code piece is the following:

Timestamps
-----------

...

1) UTC+2: 2021-06-10T09:00:42:red:`+02:00`
2) UTC: 2021-06-10T09:00:42:red:`Z`

Undesired result (no colored text achieved): enter image description here

It works when inserting a space in front of :red:, but this is not an acceptable workaround as the ISO-8601-format does not allow for spaces.

Undesired result of workaround (colored text achieved, but with spaces): colored text with spaces

How can I achieve colored text without undesired spaces in front of the colored piece?


Solution

  • As you realized, you must have a space before your role. Next you can escape the space with a backslash (\).

    .. raw:: html
    
        <style> .red {color:red} </style>
    
    .. role:: red
    
    1) UTC+2: 2021-06-10T09:00:42\ :red:`+02:00`
    2) UTC: 2021-06-10T09:00:42\ :red:`Z`
    

    See a similar question and answer.