When I run jekyll --server
my site is available at http://localhost:4000/
, but when I deploy to GitHub Project Pages, it's available at http://username.github.com/projectname/
.
This means that I cannot use absolute URLs when referring to stylesheets and other resources. Relative URLs break when the same layout is used by e.g. index.html
and 2012/01/01/happy-new-year.html
. What is the-accepted/a-good way of adding stylesheets and other resources to a GitHub Project Pages repository?
Cross-posted to GitHub Issues.
Prerequisites. Before you can use Jekyll to create a GitHub Pages site, you must install Jekyll and Git. For more information, see Installation in the Jekyll documentation and "Set up Git."
GitHub Pages are public web pages for users, organizations, and repositories, that are freely hosted on GitHub's github.io domain or on a custom domain name of your choice. GitHub Pages are powered by Jekyll behind the scenes, so they're a great way to host your Jekyll-powered website for free.
Jekyll is a static site generator with built-in support for GitHub Pages and a simplified build process. Jekyll takes Markdown and HTML files and creates a complete static website based on your choice of layouts. Jekyll supports Markdown and Liquid, a templating language that loads dynamic content on your site.
This problem arises because the root directory is always the user url (user.github.com), in both user and project pages. I found a solution to this: Configure the url
variable in the _config.yml
file to the github project page:
safe: true ... url: "http://user.github.io/project-name"
then, in the layouts, use absolute references, using the site.url
variable to do that:
<link rel="stylesheet" href="{{ site.url }}/css/styles.css">
and run the server using:
$jekyll --server --url=http://localhost:4000
The command line option overwrite the setting in the configuration file in local mode. With those settings, I get all the links pointing to the right place, both hosted in github and in local mode.
UPDATE: Jekyll 1.0 Since Jekyll reached 1.0, the aforemetioned options server
and url
and its respective command line options --server
and --url
have been deprecated. Instructions for the new version:
In the _config.yml
file, add the variable baseurl
(without trailing slash):
baseurl: "http://user.github.io/project-name"
In the layouts or pages, use absolute references, using the site.baseurl
variable:
<link rel="stylesheet" href="{{ site.baseurl }}/css/styles.css">
and then, run the local server (the baseurl option is empty on purpose):
$ jekyll serve --watch --baseurl=
More details about the changes in the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With