37

I have been playing around with Jekyll for a couple of weeks now and I am trying to create a default style for each of my blog posts, but I'm not exactly sure where and how it's supposed to be done. My main index page works fine in terms of styling but my posts have no CSS being passed to them whatsoever despite trying various methods.

Is the CSS for blog posts supposed to be written in _layouts/default.html or _layouts/posts.html, and do I have to specify which stylesheets I want to use in the YAML, by using {% include ...%}, or by writing

{% if page.style %}
    <link rel="stylesheet" href="{{ page.style }}">
{% endif %}

I wasn't able to find information that gave a clear cut answer.

0

4 Answers 4

28

The Jekyll way to do this is to take whichever layout you are going to use for the final page (for example _layouts/posts.html) and create your HTML document skeleton there (i.e. html, head, and body tags). In the head of the layout HTML, put {% include blog-head.html %}. This blog-head.html file is where we are going to include all of the CSS, JS, etc. that is required for every blog page.

Then in your blog-head.html, you can just write the CSS file include for a custom CSS file:

<link rel="stylesheet" href="blogposts.css">

This way, you can easily include the same head section in all of your blog post pages, and you can easily update that head section (adding, removing, or changing CSS/JS) and it will automatically take effect across all of your blog posts.

The following link provides general steps on overriding theme defaults: Jekyll: Overriding Theme Defaults. The page provides instructions for getting a copy of the html base layout file (from your theme) that you will need to modify with the new CSS link.

Please follow up if you would like me to clarify anything!

8
  • thanks for the guidance. Once question though. Is the blog-head.html file supposed to be in the _layouts folder?
    – gabed123
    Commented Sep 22, 2015 at 19:06
  • @gabed123 blog_head.html goes in the _includes folder. Hope that helps! Commented Sep 22, 2015 at 19:12
  • And where would the CSS file have to be located. Because for some reason it still isn't rendering after doing all of that. Sorry for the ridiculous questions.
    – gabed123
    Commented Sep 22, 2015 at 20:04
  • @gabed123 The CSS file should be located in the main Jekyll folder, if I'm not mistaken. (You can use an absolute or relative path on the link tag if you would like your CSS in a subdirectory.) Commented Sep 22, 2015 at 20:18
  • 1
    Do you need a closing "/" on that tag? E.g. <link rel="stylesheet" href="blogposts.css" />? Commented Jan 22, 2020 at 17:56
27

When I want a quick and dirty way to add some CSS into a post, I'll just add a <style> tag in the body of my post.

---
layout: post
title: "quick and dirty CSS in jekyll posts"
date: 2016-04-08 13:04:00
---

<style type="text/css">
  p {
    border: 1px solid black;
  }
</style>

whoa this paragraph has a border around it, such magic
0
4

Simple solution is create in your _includes/a head.html (will replace the head that you use in your index c:) file that includes the import from your css. So you can include it anywhere, but you must put the ../css/main.css, the key here are the 2 dots.

<link rel="stylesheet" href="../css/main.css">

Now it works. At least it solves the 404 non-style error and the url like myUrl:4000/categories.

Hope it helps you :)

1
2

You can get really crazy with includes and no doubt the code ends up looking well factored and cool. However, productivity can take a hit. Especially initially, when you may be doing most of your work with a 'Live Preview' tool like Brackets.io. Early on you may not be ready to start your 'jekyll serve' workflow pattern. I suspect part of your issue is not including the 'baseurl' config parameter.

A happy medium solution that works for me is to conditionally include header links.

{% if site.baseurl %}
  {% include links.html %}
{% else %}
  <link rel="stylesheet" href="/solarized-dark.css">
  <link rel="stylesheet" href="/site.css">
{% endif %}

This handles all workflows: live preview, jekyll server and production.

If you are testing locally with jekyll you can pass an empty baseurl parameter.

jekyll serve --baseurl ''

http://jekyllrb.com/docs/github-pages/#project-page-url-structure

1
  • Regarding to that url and baseurl Issue, I created an "ruby plugin" website_url see here
    – KargWare
    Commented Oct 5, 2021 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.