Themes

In a Jekyll site, the content is separate from the layout.

Content is written in the pages where we want it, as normal.

Design is applied by using a page template,
which in Jekyll is called a “layout”.

A page layout can be a single file
with an area specified for the content.

A page layout can also be made up of separate
pieces such as a header, content area and footer.

These pieces are known as “includes”.

Our theme will contain all layouts, includes,
stylesheets and design images for our site.

Creating a Theme

We want to have one place where we keep all files
related to the look and feel of our website.

In your project folder,
create a new folder called “theme”.

Create a theme folder.

Creating Layouts

A layout defines one way that a page can be displayed.
Each layout defines a content area and may use includes.

In your theme folder, create a new folder called _layouts.

Add a new line to your _config.yml,
telling Jekyll the location of your layouts.

name: Jekyll Site
version: 3.0.3

# where things are
layouts_dir:  ./theme/_layouts

Stop Jekyll by pressing Ctrl + C in the command line.

Restart Jekyll with the new configuration
by typing jekyll serve and pressing enter.

In the layouts folder, create a
new file called page.html

Add the following code to page.html:

<!doctype html>
<html>
    <head>
        <title>My Awesome Site</title>
    </head>

    <body>
        {{ content }}
    </body>
</html>

Make sure to save your page layout!

The curly brackets are a placeholder for where we
want the page content to be inserted by Jekyll.

Use your site name from _config.yml as the page title:

<!doctype html>
<html>
    <head>
        <title>{{ site.name }}</title>
    </head>

    <body>
        {{ content }}
    </body>
</html>

Applying Layouts

To apply our page layout to our pages,
we need to modify each page’s HTML file.

Open your index.html and remove everything
except for the heading which says “Hello!”

<!doctype html>
<html>

    <head>
        <title>My Jekyll Site</title>
    </head>

    <body>
        <h1>Hello!</h1>
    </body>

</html>

Your index.html should now only contain a heading.

Now at the top of your index.html add some front matter:

---
layout: page
---

<h1>Hello!</h1>

Make sure to save your changes!

Preview your site and view source to check
that the full HTML page is being built.

Your page source should still contain head and body tags.

Challenge

Apply the page layout to your contact page.

Styling

All of your CSS should also be
included in your site’s theme.

In your theme folder, create a new folder called css.

Inside the CSS folder, make a new file called styles.css.

Add some basic styling for your site.

/* import the Roboto font from Google Fonts */
@import url(https://fonts.googleapis.com/css?family=Roboto:400,700,400italic,700italic);

html {
  font-family: 'Roboto', sans-serif;
  font-size: 14px;
  line-height: 150%;
}

body {
  margin: 0;
  background: red;
}

Update your page layout to import the CSS.

<!doctype html>
<html>

    <head>
        <title>My Jekyll Site</title>
        <link rel="stylesheet" href="/theme/css/styles.css">
    </head>

    <body>
        <h1>Hello!</h1>
    </body>

</html>

Refresh your page to check
that the stylesheet is working.

Your site should be horribly colourful.

Remove the background colour from your CSS.

/* import the Roboto font from Google Fonts */
@import url(https://fonts.googleapis.com/css?family=Roboto:400,700,400italic,700italic);

html {
  font-family: 'Roboto', sans-serif;
  font-size: 14px;
  line-height: 150%;
}

body {
  margin: 0;
  background: red;
}

Your site should be back to normal.

Includes

These are re-usable pieces of code
which can be used in multiple layouts.

In your theme, create a new folder called _includes.

Tell Jekyll the location of this folder.

layouts_dir: ./theme/_layouts
includes_dir: ./theme/_includes

Make sure to save your configuration!

Restart Jekyll with the new configuration.

Ctrl + C
jekyll serve

In your includes folder,
create a file called header.html

Create some header code in header.html

<header>

    <h1>{{ site.name }}</h1>

    <nav>
        <a href="/">Home</a>
        <a href="/contact.html">Contact</a>
    </nav>

</header>

Save your new header include.

In your page layout, include the header
in the body, before your content.

<!doctype html>
<html>
    <head>
        <title>My Awesome Site</title>
    </head>

    <body>

        {% include header.html %}

        {{ content }}

    </body>
</html>

Because we added the header to our template,
it should show up on both of our pages.

Check that your navigation works.

Add these styles to get your header looking snazzy:

header {
  background: #222;
  color: white;
  padding: 10px 30px;
  display: flex;
  align-items: baseline;
}

header h1 {
  font-size: 16px;
  flex-grow: 1;
}

header nav a {
  display: inline-block;
  color: white;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 2px;
  margin: 0 10px;
  text-decoration: none;
  border-bottom: 2px solid transparent;
}

header nav a:hover {
  color: lightblue;
  border-color: lightblue;
}

Challenge: Add Another Page

Add a “shop” page and apply the same theme.
Also add the new page to the navigation.

Your site should have three pages, with a nav on every page.

Images

Page designs often use images,
which we can include in our theme.

In your theme folder,
create a new folder called img.

Download a Creative Commons background
for your website from Subtle Patterns.

Visit Subtle Patterns

Extract the zip file from Subtle Patterns and copy
the pattern image to your img folder with a tidy name.

In your site folder, create a new page called credits.html.
Add the credit for your image to this page and save.

---
layout: page
---

<ul class="image-credits">

  <li>

    <img src="/theme/img/background.png">

    <div class="info">
      <span class="title">
        GPlay by <a href="http://dhesign.com/">Dimitrie Hoekstra</a> 
      </span>
      <span class="licence">Licence: CC BY-SA 3.0</span>
      <a class="source" href="http://subtlepatterns.com/gplay/">Original Source</a>
    </div>
  </li>

</ul>

Add the credits page to your header navigation.

<nav>
  <a href="/">Home</a>
  <a href="/contact.html">Contact</a>
  <a href="/credits.html">Credits</a>
</nav>

And here’s some CSS to format
your image credits list.

.image-credits li {
  padding: 10px 0;
  border-bottom: 1px dotted #666;
  display: flex;
}

.image-credits img {
  width: 150px;
  margin-right: 15px;
}

.image-credits .title,
.image-credits .licence,
.image-credits .source  {
  display: block;
}

.image-credits .title {
  font-weight: bold;
  font-size: 16px;
  line-height: 200%;
}

You can now use the image in your CSS as usual:

body {
  margin: 0;
  background: url('/theme/img/background.png');
}

Summary

  • Layouts
    Every page uses a layout, which defines a content area.
  • Includes
    Layouts can be made up of other pieces of code called includes.
  • Images
    Borrowed images must be Creative Commons and clearly credited.

Themes: Complete

Next Chapter

Loading...