CSS Tricks: Individually-Styled Markdown Elements
Markdown is used all the time, for example in GitHub readme.md files, in Slack messages, and in WordPress themes. For example, below is two HTML lists created with this Markdown:
1 2 3 4 5 6 7 8 9 | * Item 1 * Item 2 and 1. Item 1 1. Item 2 - Sub a - Sub b |
The above Markdown renders to the following HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <ul> <li>Item 1</li> <li>Item 2</li> </ul> <p>and</p> <ol> <li>Item 1</li> <li>Item 2 <ul> <li>Sub a</li> <li>Sub b</li> </ul> </li> </ol> |
The Markdown ultimately renders to the following output:
- Item 1
- Item 2
and
- Item 1
- Item 2
- Sub a
- Sub b
Problem
Pretty neat, but what if you want to style a particular Markdown list with, say, a bold blue list? Markdown renders to empty HTML tags like <ul>
, <li>
, <p>
, etc. without class names, so there is no way to add a unique class identifier to a Markdown element.
Your first thought might be to wrap the Markdown in something like a <div class="bold-list"> ... </div>
block. Let’s try that.
1 2 3 4 | <div> * Item 1 * Item 2 </div> |
The above unfortunately renders to:
* Item 2
How about with some extra spacing as in the below mixed Markdown?
1 2 3 4 5 6 | <div> * Item 1 * Item 2 </div> |
The above also problematically renders to:
* Item 1
* Item 2
Solution
Markdown doesn’t like to necessarily be mixed with HTML, and it certainly doesn’t like to be nested in HTML tags. So how can we style an individual Markdown element? Let’s use a neat CSS trick to easily style an individual Markdown element: the immediate sibling selector (+).
Consider this CSS:
1 2 3 4 5 6 | <style> .bold-list + ol { font-weight: 900; color: #257f96; } </style> |
Can you see what will allow us to render a specific ordered list in bold with blue text?
Let’s try this now:
1 2 3 4 5 6 7 8 9 10 | <div class="bold-list"></div> 1. Item 1 1. Item 2 - Sub a - Sub b but this is not styled: 1. Another item 1 1. Another item 2 |
With this neat CSS trick we get this output:
- Item 1
- Item 2
- Sub a
- Sub b
but this is not styled:
- Another item 1
- Another item 2
Here is a Codepen example to experiment with.
How it Works
The CSS immediate sibling selector (+) allows us to select an element immediately after the empty <div class="bold-list"></div>
. If there is a Markdown ordered list after that selector, then we have effectively selected it with CSS and without class names. Since the Markdown is at the root level (i.e. not nested in HTML elements), it renders into HTML as it normally does. From here you can get creative.
For example, I use this trick for hanging indents.
1 2 3 4 5 6 7 8 9 | <style> .hanging-indent + ul > li { padding-left: 22px; text-indent: -22px; } .hanging-indent + ul > li:not(:last-child) { padding-bottom: 1em; } </style> |
The above CSS with an immediately preceding <div class="hanging-indent"></div>
results in this nice bulleted hanging indent list.
This is a simple way to make Markdown more flexible and allow non-developers to style sections.