Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll on Github Pages not rendering CSS

I've been experimenting with Jekyll using Octopress for deployment. I've got it working perfectly on localhost, but for some reason the CSS isn't getting loaded when the site loads on Github Pages. I'd appreciate any assistance understanding why.

When I view the site loading at the Github Pages url it shows a 404 error for main.css. The initiator column (using developer tools in Chrome btw) indicates this comes from the index.html file, at the line in the HTML head:

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

The tree for the _site directory on my local machine is:

.
├── 2015
│   └── 11
│       └── 09
│           ├── another-jekyll-blog.html
│           ├── fifth-time-is-the-charm.html
│           └── jekyll-and-octopress-is-this-thing-on.html
├── about
│   └── index.html
├── css
│   └── main.css
├── feed.xml
├── index.html
└── jekyll
    └── update
        └── 2015
            └── 11
                └── 09
                    └── welcome-to-jekyll.html

This tree matches exactly in the Github repository after the site has been pushed up (I used jekyll build followed by octopress deploy).

When I look at the Sources tab in developer tools, for the deployed site, the tree shows as:

[USER].github.io
|-css
|   |-main.css
|
|-octo-5
|   |-(index)

But when I view the site on localhost, the site tree is:

localhost:4000
|-css
|   |-main.css
|
|-(index)

The issue clearly seems to have something to do with the way the main.css file is being referenced on Github Pages. I assume that the link to the stylesheet in the index file is not working because main.css isn't in the relative path /css/main.css as expected. It works locally, but not on the Github Pages site. But I can't understand why, since the site tree appears to be correct, hasn't been modified from Jekyll defaults, and is the same locally as well as on the remote.

For good measure, including the _config.yml file below. It is essentially unchanged from the default settings at install, though I added some Octopress settings:

# Site settings
title: Test Site
email: [email protected]
description: > # this means to ignore newlines until "baseurl:"
  Site description here...
baseurl: "" # the subpath of your site, e.g. /blog
url: "http://yourdomain.com" 
twitter_username: jekyllrb

## --- Octopress defaults --- ##
# Default extensions for new posts and pages
post_ext: markdown
page_ext: html

# Default templates for posts and pages, found in _templates/
post_layout: post
page_layout: page

# Format titles with titlecase?
titlecase: true

# Change default template file (in _templates/)
post_template: post
page_template: page
draft_template: draft

For reference, the repository is publicly available at: https://github.com/bg-scro/octo-5

like image 571
Scro Avatar asked Nov 10 '15 16:11

Scro


People also ask

Where do I put CSS in Jekyll?

Using CSS, JS, images and other assets is straightforward with Jekyll. Place them in your site folder and they'll copy across to the built site. Jekyll sites often use this structure to keep assets organized: .

How do I link CSS to GitHub?

How Do I Add Html And Css Code To Github? For example, create your own repositories on GitHub and create a few things such as a repositories. By clicking on “Uploading an existing file” once the repository has been created, you will be able to upload and drop HTML and CSS files or select manually how to choose.

Why does my website look different on GitHub Pages?

It appears to be a mixed-content error. Since GitHub Pages are served over HTTPS, you have to make sure that when requesting external resources they're also served over HTTPS. In your index. html file change the http in http://code.jquery.com/jquery-3.5.1.min.js to https and you should be good to go!

How does GitHub Pages work with Jekyll?

Your site is automatically generated by GitHub Pages when you push your source files. Note that GitHub Pages works equally well for regular HTML content, simply because Jekyll treats files without front matter as static assets. So if you only need to push generated HTML, you're good to go without any further setup.


2 Answers

I'm using jekyll and github's personal website generator. Adding the following code to my _config.yml file below solved the issue of my css not rendering in production. Not exactly sure why though. Would love an explanation. :)

baseurl: /PROJECT
url: http://USERNAME.github.io

Repo: https://github.com/kaeland/kaeland.github.io

like image 103
kaeland Avatar answered Nov 08 '22 12:11

kaeland


In _config.yml, set root: /octo-5.

Call css like the original octopress :

<link rel="stylesheet" href="{{ root_url }}/css/main.css">
like image 37
David Jacquel Avatar answered Nov 08 '22 13:11

David Jacquel