CSS for Markdown, write your own

This is the third part of some posts on using CSS in and with DT/DTTG. Though I wrote it as a single piece, I broke it up in smaller installments to keep the posts (barely) managable. Please let me know (preferably by a PM) if you find errors of any kind or are missing details. I’ll correct the first ones and try to help with the second ones.

Previous post | Next post

Delving deeper into CSS

CSS is a complicated beast, and it’s getting more so every day (no thanks to Google inventing and implementing new exciting stuff). The only authoritative documentation is the one at the W3C. However, that’s unwieldy stuff not meant for mere mortals. There are numerous websites out there providing easier to digest examples and reference material, notably CSS tricks and the Mozilla Developer Network.

Structure of HTML generated from Markdown

The good news is that the HTML generated from Markdown is fairly simple. It contains only HTML elements without any classes or IDs. Also, the number of elements is quite small, so you won’t see the fancier stuff like aside, nav, section and the like, nor any divs wrapping elements. Basically, a HTML document generated from Markdown, has this structure:

<head>....</head>
<body>
  <h1>...</h1>
  <p>...</p>
  <h2>...</h2>
  <p>...</p>
  <blockquote><p>...</p><p>...</p</blockquote>
  other elements
</body>

where other elements can be lists or tables. All elements except head and body can repeat any number of times. Links and images are just rendered as shown above inside a p element. This simple structure makes styling HTML generated from Markdown quite easy, at least compared to a full-fledged HTML document.

Lay out the foundation

To start from scratch, I suggest to set all relevant values to reasonable defaults on the body element. This is sometimes referred to as a CSS reset. Of course, it is up to you what you consider “reasonable”. Something like this might be a starting point:

body {
  margin-left: 2em;
  font-family: "Century Schoolbook", Times, serif;
  font-size: 1em;
  color: black;
  background-color: white;
  padding: 0;
  max-width: 55ch;
}

The choice of the font-family is very much a metter of taste (more so then everything else, probably). Here, I went for a serif font: “Century Schoolbook” is used if it’s available. Otherwise, “Times”, and if that’s also missing, the default serif font. Also, I specified a font-size of 1em, which is exactly the same as the font size specified for the user agent (aka “browser”) used to render the HTML. This is not strictly necessary, but it makes sure that the default text size in your HTML is the same the user specified in their browser settings.

To make the text easier to read, the maximum width of the body element (and all its children!) is set to 55ch, that is 55 average characters (loosely speaking). padding is the inner margin of an element that will remain blank, whereas margin is the space between the current element and its parent. Here, I specify a left margin of 2em so that the text does start flush left.

Fine-tune HTML elements

These styles for body define a baseline. Now you can specify differing styles for other elements to your heart’s content. For example, if you’d like the first line of every paragraph to be indented by an em space, you could use

p {
  text-indent: 1em;
}


or if you’d want all first level headlines to have a red underline

h1 {
  text-decoration: underline red;
}

or if you’d want all third level headings to appear in uppercase

h2 {
  text-transform: uppercase;
}

The latter, though, is probably not a very good idea – firstly, all uppercase letters are more difficult to read, secondly many readers will think that you’re screaming at them. And thirdly, the text looks fundamentally different than what you see in your Markdown document.

7 Likes

I am trying to develop my own style sheet, and while I understand the basic structure and syntax of CSS (thanks to your guide, in large part), I am at a loss when trying to modify some of the finer things in Markdown. For example, when I transclude other Markdown documents, there’s a horizontal bar that appears at the top of the transcluded noted. If the transcluded note also has properties, some of the properties appear in a bulleted list.

Can you kindly offer any direction to help me make changes to these things? Is there documentation/a lookup that I could use? Thanks

Converting a markdown document to HTML would reveal the HTML elements. MDN is a good place to learn how to apply CSS styling to those elements.

3 Likes

Transcluded documents display their contents. A horizontal rule will not appear unless your including it.

A sample might help. I guess when you’re saying “properties”, you do mean metadata? In that case, I have no idea if the metadata should be visible at all. If you’re talking about something else, please clarify.
And convert the MD to HTML, as suggested by @meowky. You can then open that HTML in your favorite browser and use its developer tools to fiddle around with the CSS.

Oh, excellent!

Three dashes are used to specify the beginning and end of document metadata, which I put it in all of my notes. Is it it possible that, when a note is transcluded, these dashes are being ‘interpreted’ as a horizontal bar? [Otherwise, I have no explanation for this, because there is no horizontal bar in the transcluded note.]

Why are you using YAML style metadata? Are you working with Obsidian or something?

Yes. I also like that DT indexes some of these fields. It allows me to do advanced searches with other kinds of files with the same metadata.

The YAML format is not required by DEVONthink in order to be indexed. MultiMarkdown metadata is the standard in DT (essentially remove the prefixing and trailing rules).

And no the rules aren’t displayed in a transclusion as a horizontal rule.

2 Likes

I tried doing this with a note to see if I could figure out how transcluded notes are handled, but I couldn’t make sense of the giant, undifferentiated html code block at the top of the page. So I am really at a loss at what to do, perhaps you can offer some advice:

Hierarchical transclusion is very useful in mimicking the structure of a forum thread, where some replies are to the OP, and those replies may have replies of their own, and so own. The way I capture this structure is with tree-like transclusions - subreplies to replies are translcuded in the parent replies, not in the master note, which only has the “top-most” replies transcluded. Indenting the children of these top-most replies would be very helpful in making the master note where everything is transcluded more readable. Any idea on how I would go about accomplishing this?

That is likely from you including Markdown extensions, i.e., Prism, Mermaid, or MathJax. You can temporarily disable those to look at a “cleaner” conversion.

PS: This thread is wandering too far afield. Please start a new post so we can preserve this one a bit more. Thanks.

1 Like