A Better Github Pages Theme
Published on 2017-12-10 by Tristan Sweeney
Foreword
T’was the week before Christmas break, and all through the house, and not a soul was stirring, except for the mouse. The deadlines were approaching, the semester drawn short. Stress was upon us, and the time drew near. But wait! We can procrastinate for years!
I’m working on a series of blog posts documenting how to setup a Linux HPC cluster, and need to customize my theme a bit. Here’s what I did, and how to build your site locally to test on Windows. If you’re looking for the GitHub docs on inserting styling (which this elaborates on), it lives here.
Modifying Templates
Let’s walk over how to modify the templates used by Github pages, a process I used to insert QTip2, a tooltip javascript library, into to my site.
Create a file named _layouts/default.html
at the root of your Github pages
repo and copy your theme’s default layout template to it (each github.io theme
has a public repo on Github). This will be used by the engine that Github pages
uses to build your site in place of the default-default layout. Patch the below
content into the template.
The QTip CSS file provides styling for some sleek hover-over tooltips, and the
JS files provides the functionality to load content for the tooltips from the
HTML source. To add a tooltip to a HTML element, add the data-qtip=<id>
attribute to it, referring to the id of the tooltip content.
The child element should have it’s style set to display: none
, which will be
unset by the javascript that rips out the html for the tooltip (that’s all that
the qtip-tip
class does below). We’ll get to injecting your own CSS to do that
in a minute. First, let’s see some examples (full docs for the QTip tool live
here).
Hey look at me, I’m a tooltip!
Look, anything can be tooltipped!Okay, enough showboating, on to adding your own styling.
Adding Your Own CSS
The CSS used in your final site is generated from Sass, a preprocessor extension of CSS. The full documentation for the language is worth reading, but the extension is pretty simple. The additions come together to allow to allow a more pragmatic design of CSS sheets, with variables, nested elements, and importing.
You can inject your own styling with Sass adding a SCSS (Sass CSS) file at
assets/css/style.scss
, which will be loaded after the site’s default CSS is
generated. The style.scss
file must begin with two rows of dashes, but
other SCSS files should not (it’s not valid SCSS and will break the sass-lang
processor). Smaller partial files have filenames begining with ’_’ and
and are used to store variables (they aren’t used to generate CSS).
A version of my own SCSS file is below.
Note you’ll have to add the below content to your _config.yml
if you want to
be able to properly find other files to import.
Building Locally on Windows
I spent many hours late at night trying to build my page locally on Windows, an endeavor that should have been straightforward. I just had to install ruby, and a few dependencies (libz and libcurl). Should be easy, right?
Almost everything worked, but ruby couldn’t find libcurl despite it being on the path. Trivial problem on Linux, should be a simple problem on Windows, but it wasn’t working and drove me mad. I could confirm the .dll was on the path, but the app still was failing.
Finally, the revelation came: give up on using the Windows environment for anything fancy and use the Ubuntu-On-Windows subsystem to do all the development work!
(I can’t wait for the 15th, Star Wars hyyyyyyyyyype!)
Follow the instructions on the Microsoft Docs to enable the Windows subsystem for Linux. After, head to the Ubuntu app on the windows store and open Ubuntu. Run the below commands to install all your dependencies:
Write a file named Gemfile
at the top level off your github.io repo, and
write the below contents to the file. This allows the site to be built locally
against a remote github theme (the github-pages gem facilitates downloading the
theme and building against it).
After that, bundle exec jekyll serve
will serve up your site on
localhost:4000
. It’ll rebuild your site when any files in the site repo
change, so go wild! If you think for some reason your site isn’t in sync,
Ctrl-C
the server and launch it again to rebuild.
Afterword
It’d be very, very nice if you could write a _colors.scss
partial file that
could be used to override the default colors of different elements on the site.
Namely, it’d be useful to override the green elements on the page with others,
or set the background color.
I just had a pull request accepted that moved the style sheets from using magic values everywhere (and really just using plain CSS in a SCSS file) to use more sass. That pull request was just approved today, lucky me. This should lay the groundwork, if I make it through finals week.
Written by Tristan Sweeney
← Back to blog