jekyllminimajekyll-theme

Separate style for markdown in single backticks vs. triple backticks using the Minima theme for Jekyll


I am trying to customize the Minima theme for Jekyll. I'd like to have a dark background for code between triple backticks (i.e. in its own paragraph), but I don't want to affect the background of inline code between single backticks. Is there a way to do this?


Solution

  • An inline code block in Markdown renders as:

    <code>Lorem ipsum</code>
    

    A fenced code block - the triple backticks - renders as:

    <pre><code>Lorem ipsum</code></pre>
    

    Following is an example of CSS selectors targeting theses patterns. You can see these behaviours in my example:

    code {
        font-weight: bold;
        background-color: GreenYellow;
    }
    
    pre > code {
      background-color: AliceBlue;
      color: Blue;
    }
    <code>Lorem ipsum</code>
    
    <pre><code>Lorem ipsum</code></pre>

    In summary, if you want to apply a rule to the fenced code block but not the inline code block, use a more specific CSS selector, like pre > code. This selector matches <code> blocks where the parent is a <pre> tag.