Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you programmatically apply a CSS class to paragraphs in Jekyll?

It looks like Jekyll plugins provide a way to alter the transformation from Markdown to HTML, but I'm not sure where to get started.

Say I wanted to apply a CSS class to all of the paragraphs in a post. How do I do that? E.g. I've got a post:

---
title: "my cool post"
...
---

Hey I want this paragraph wrapped in a class called my-custom-class

And the HTML outputs:

...
<p class="my-custom-class">Hey I want this paragraph wrapped in a class called my-custom-class</p>
...

If I'm mistaken about plugins, I'm cool with another solution (other than manually adding the class to each paragraph in the Markdown).

like image 435
Kayce Basques Avatar asked Jan 04 '17 02:01

Kayce Basques


People also ask

How do I apply a CSS to a specific class?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

How do I apply markdown in CSS?

Markdown does not support CSS styles. Since Markdown converts to HTML, Any valid HTML works. CSS styles are added with inline or style tags in HTML.

Can you make a class selector particular to an element type?

You can create a selector that will target specific elements with the class applied.

How to name a CSS class?

First, edit the question you'd like to affect with CSS and go to the Layout tab. Enter your class name in the CSS Class Name field and click Save Question. Your class name can be anything in plain words.


2 Answers

Using Kramdown IALs

To apply styles to just one paragraph you can use kramdown's IAL, after writing the paragraph apply the class you want with {: class="my-custom-class"}

---
title: "my cool post"
...
---

Hey I want this paragraph wrapped in a class called my-custom-class
{: class="my-custom-class"}

Using SCSS

If you want to apply the custom style to all your posts paragraphs,

  • wrap your posts content with a specific class like <div class="post">...</div>
  • edit your SASS with a custom style that affects only to .post p like:

    .post {
          p {
              #my-custom-style properties..
          }
     }
    

As a side note, remember also that you can always add plain html in markdown like:

<div class="my-custom-style">
    Some cool paragraph
</div>
like image 145
marcanuy Avatar answered Nov 05 '22 09:11

marcanuy


Apparently you need to use

{::options parse_block_html="true" /}
<div class="my_class">
...
</div>
{::options parse_block_html="false" /}

to parse the markdown between the html.

like image 24
user11198071 Avatar answered Nov 05 '22 09:11

user11198071