Let's Learn 11ty Part 2: Partials, Styling & Images

Let's Learn 11ty Part 2: Partials, Styling & Images

Read 4804 times

#eleventy

#webdev

#ssg

In the last article we setup the basis for our site. It’s bare bones, but it is a valid site. We are now going to add more to it to make it a bit nicer

Partials

Partials are those files that contain information/data repeated across the site that doesn’t change a lot - navigation, footer.

It’s now time to structure our site a bit better. Let’s make our navigation first

---
layout: base
title: About
---

# About Page
<!-- _includes/partials/_navigation.njk-->
<nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
</nav>

Later on we will improve this navigation, but for now it’s fine

 <body>
  {% include "partials/_navigation.njk" %}
  <main> 
    {{content | safe}}
    </main>
  </body>

We can repeat the same steps for the _footer.njk file

Let’s run our server and see what we have so far

npm start

navigation added


Styling

Let’s now make our site look a bit nicer. Starting with a font. We’ll add a google font in our head:

  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2? 
     family=Sora:wght@300;400;500;600&display=swap" rel="stylesheet"> 
    <title>My Eleventy Site - {{title}}</title>
  </head>

The CSS File

We also need to add a CSS file to apply style changes. Let’s make an assets folder, in it a css and then in it a style.css file

/* src/assets/css/style.css*/
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
}

body {
  font-family: "Sora", sans-serif;
}

Above is a general reset that I use for my CSS.

We then have to add it to our head tag as well

   <link rel="stylesheet" href="{{ '/assets/css/style.css' | url }}">

After doing all that, and checking our site, you are going to notice something peculiar - our styles are not being applied.

If we checked our _site folder, where our site files are generated to, there is no CSS file in there

But why is that?

Pass Through Copy

If you recall when we started, I mentioned that Eleventy supports several templating languages - ways to author your site. With those file types, Eleventy automatically converts them to html.

CSS and JavaScript are not part of these languages. With them we actually have to tell Eleventy to copy those files to our output directory through a process called Pass Through Copy

Open the .eleventy.js file and modify it to look like this:

module.exports = function(eleventyConfig) {
  eleventyConfig.addPassthroughCopy("src/assets/css/style.css");

  return {
    dir: {
      input: "src",
      data: "_data",
      includes: "_includes",
      layouts: "_layouts",
    },
  };
};

NOTE: Any files that are not part of the template languages that you want to be present on the site will need to have a Pass Through Copy - that goes for images, local fonts, icons etc

Now, if we check our site, our styles are being applied.

styles passed

We can now add more styles to our style sheet

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
}

body {
  font-family: "Sora", sans-serif;
  line-height: 1.5;
}

a {
  text-decoration: none;
}

img {
  max-width: 100%;
  display: block;
}

nav {
  display: flex;
  justify-content: center;
  align-items: center;

  height: 3.5rem;
  width: 100%;
  background-color: black;
  color: lightgray;
}

nav > a {
  display: block;
  padding: 1rem 2rem;
  color: lightgray;
}
nav > a:hover {
  color: aqua;
}

main {
  max-width: 50rem;
  margin: 0 auto;
  padding: 3rem 2rem;
  min-height: 100vh;
}

main > h1 {
  margin: 2rem 0 0.5rem;
}

main > img {
  border-radius: 5px;
}

footer {
  background-color: black;
  color: lightgray;
  padding: 2rem;
}

styles applied


Images

Lets add an image to our site to it doesn’t look so plain

 eleventyConfig.addPassthroughCopy("src/assets/images");
---
layout: base
title: Home
---

![hero image](assets/images/learn.png)

# My Eleventy Site

I am a site built with [Eleventy](https://www.11ty.io/).

And here we go:

Image added


More Info


We have done a good deal this time:

For now:


Thank you for reading, let’s connect!

Thank you for visiting this little corner of mine. Let’s connect on Twitter, Polywork and LinkedIn

Back to articles