Styling Markdown
Writing blog posts in markdown is great. It’s simple and fast.
Markdown is a simple way to write text using common symbols to add things like **bold**, *italics*, or lists. 1
Many webpages have their content written in markdown. The markdown content gets then transformed into HTML that is displayed on the website. But when you are building your own website you will also want to style that content, so how could that look like?
On styling content written in markdown
There are multiple ways you can approach styling your own markdown content.
- Using a package such as Tailwind Typography with pre-defined styling. Here’s a list of other drop-in CSS frameworks you could use.
- Writing your own CSS.
Because I’d like my written contents design to fit into the rest of the page I like to write my own styles. This is a bit of work though as we have to write styling for the various elements that markdown allows us to create.
Part of your styling such as <p>
and <h1>
elements might already have styling defined in your main CSS file that you can continue to use. But markdown content can also include images, code blocks, quotes, tables and more which we have to take care of.
Styling the different elements
I will asume that the html should look something like this:
Note: Using main
or article
is okay.
To this container we want to apply some styling as well as on all its children like this:
To space the elements I give all direct children of the container except for the first a margin-top of 1.5em
.
The markdown-container will have a max width of 700px
, or smaller for mobile devices. The keyword min
selects the smaller amount. You might also want to make the container into a flex-box or grid depending on how you want to control your layout.
Parapgraph <p>
We want the content that gets brought in through markdown to match the already existing content of the website. Which means we want to already have defined CSS variables in the root and applied styling to elements such as the body for text. I have explained how CSS variables should be used here: CSS Variables.
Which means we already should have something similar to this:
This should take care of all <p>
elements without needing to further style them excplicitly.
Section Headings <h1>, <h2> ..
Section headings are of major importance since they got used often and important for keeping an overview. !!!
You might also want to set a line-height
, font-weight
and color
for your section heading elements.
The Font-size
for each section heading should also be set, it makes sense to do this in via CSS variables to make heading sizes uniform. If you are looking for specific font sizes to use check out CSS variables.
Images <img>
& <picture>
Object-fit: scale-down
makes sure we can always see the full picture. By setting max-width
and height
we ensure proper sizing of the image. Border-radius
to set slightly rounded corners.
If you are using a framework like Astro, to center the image you will want to add margin-inline: auto
and display: block
to center the image.
Lists
Ordered and unordered
Quote
Table
Code & Code Block
- overflow-wrap: anywhere
- font-family: mono font
hr
Last words
Text here
Sources & References
- Best practices for section heading elements: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Heading_Elements
- test 2
Footnotes
Footnotes
-
Markdown cheat sheet: https://www.markdownguide.org/cheat-sheet/ ↩