www 101

All you need to know about the internet

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Merry Gridmas! Building a Festive Advent Calendar With CSS Grid

In this tutorial we’re going to build a seasonal advent calendar using CSS Grid, SVG, and a handful of festive cheer! Let’s begin by taking a look at what we’ll be working towards–click the days to see what’s underneath:

What You’ll Need

You don’t actually need much for this tutorial, just a code editor (lean on CodePen to make things easier) and some basic HTML and CSS knowledge. You will need some festive visuals though–I’ve taken illustrations by the very talented masastarus from Envato Elements (they’re vector and absolutely perfect for this):

Happy children celebrating Christmas
Happy children celebrating Christmas
Group of children and christmas tree
Group of children and christmas tree

1. A Grid is Born

For our Advent calendar we’re going to use 25 grid items; one for each of the days leading up to Christmas and another to hold a title graphic. It will look something like this on small screens:

And this on larger screens:

The turquoise grid item will hold our title graphic.

Markup

To kick things off we’ll need 25 items in a container, so let’s put some HTML together:

Tip: Given the repetitive nature of this markup you might prefer to use a templating language like HAML. HAML will let you loop over 24 items to save you writing out each one. The following little snippet compiles into exactly what you see above:

When we nest even more elements within these divs you’ll appreciate the time HAML saves you!

Basic Grid Styles

Let’s now add some basic styles to establish our grid. We begin by assigning display: grid; to our container element. Then, after some dimensions, we define the grid items.

We’ve gone “mobile first”, so you’ll see we’re defining just three columns to start with; we’ll use a media query in a moment to allow for larger screens.

  • grid-template-columns: repeat(3, 1fr); gives us three columns, each of equal width (1fr unit)
  • grid-template-rows: auto; is actually the default value, but it states that the rows will have no specific height attributed to them–we can add as many as we like and they’ll grow with the content.
  • grid-gap: 25px; defines our gutters.

That’s really all we need, Grid can handle things from here if we leave it, but we want to be more specific about where we place each grid item. For that reason we’ll use Grid Areas.

Grid Areas

We can assign a name to each area of the grid we defined, and we can write it in a really visual way too:

Here we’re naming the first area, which spans three columns on the first row t (where we’re going to place the title). This might not be the most helpful name, but it will do the job for now. Each of the other areas is given a name in accordance with the day numbers, and as you can see, we can put them wherever we want. The ability to lay things out “randomly” like this is perfect for an advent calendar.

Going Responsive

With the addition of a media query we can (really easily) change the layout for larger screens.

With this media query we’re stating that for viewports wider than 500px (arbitrary figure) we’ll change the grid so it has six columns: grid-template-columns: repeat(6, 1fr);.

And we can completely redefine the arrangement of the items, placing the days wherever we feel like it. You’ll notice that the title block now takes up three columns and three rows to the top left, and d24 takes up an area of 2×2, to give it more significance. This will become clear when we assign our grid items to the grid areas in the next step.

For now, we can’t actually see any of what we’ve done, so let’s add some provisional styles to the grid items to make things visible.

Take a look:

Need a primer on grid areas? Learn more about the whole thing in this beginner’s tutorial:

Assigning Grid Areas

We now need to assign our grid items to the areas we’ve set out. We do this as follows:

You can see our div.title has been assigned to grid area t, and each day has been assigned to a corresponding area. There’s nothing to say you can’t assign .day-3 to grid-area: d16; of course, we’re just choosing to organise things this way. How’s it looking?

2. Opening Doors

That’s the basic structure all sorted, now we need to make our CSS doors. We’re going to put two divs within each grid item–one for the front of the door, another for the back–both wrapped in another div, and we’re going to use a checkbox hack to flip the whole thing round when it’s clicked.

We begin by adding a label, checkbox, and the divs to each grid item:

Again, you need to do this for all 24 grid items, which is a bulky chunk of repetitive markup 245 lines long, so you might find HAML helpful. Here’s how that would look:

With slightly adjusted padding, this is what all those checkboxes look like now:

The Doors

Prepare yourself; what’s coming is arguably the most complex bit of this whole build. It’s a big chunk and very little works without the whole thing being present, so bear with me. We’re going to do some shifting around with 3D Transforms. 

Firstly, we hide the checkboxes, then we state that we want a certain level of perspective applied to our labels (perspective: 1000px; handles that). transform-style: preserve-3d; states that children of the label will be displayed in a 3D space instead of along a flat plane. We then use some Flexbox styles to make sure the label fills the whole area available to it.

Some styles on the .door element (which contains the front and back faces) then set the transition timing and tidy things up further:

Front and Back

Okay, now to concentrate on the front and back faces of our door.

The .door div selector points to both the .front and the .back divs. We use it to make each one 100% the width and height of the .door container, and absolutely positioned top left. The Flexbox rules make sure the content within (the number) is centrally aligned and the backface-visibility: hidden; rule ensures that neither of the faces can be seen when their reverse side is facing us. That’s important, as we then focus on the .back and set it to flip over with transform: rotateY(180deg);.

Now For the Fun Stuff

All this has been leading up to what’s actually a pretty cool effect. We’re going to use the checked state of the checkbox to transition the faces. When we click on the label (which fills the whole grid area) it checks and unchecks the checkbox, even though it’s not displayed. Depending on that state, we alter which way the .door is facing.

The first rule gives us a hover state. The second rule uses an adjacent selector, so when a .door element is immediately preceded by a :checked element we rotate it 180 degrees on the Y axis (we flip it over). All of that gives us our fundamental door functionality!

3. Time to Decorate

Let’s make things look pretty. The first visual we’re going to slot in is our title graphic. Using the Happy children celebrating Christmas illustration I took the title, changed the details and saved an SVG file

Tip: for detailed vectors like these, in Illustrator go to Object > Path > Simplify… and you can reduce the complexity. By trimming the number of curves and points you can significantly lower the file size without losing too much of the overall effect.

There are a few ways we could add this image to the page, but I’ve just placed an img tag in our markup:

Some styles give our image fluidity and align it centrally within the grid area:

To enhance the blank background we add another SVG (the snowfall and clouds) to the body via CSS:

Adding Images to the Doors

Each of our 24 doors also need to have an image hiding behind them. Again, we could do this in a few different ways, but in this case we’ll save a bunch of SVG files and add them as backgrounds via the CSS. For each door, after we define its grid area, we’ll have a rule for the .back face:

This is what that gives us:

Pretty good! Now we just need to tidy those images up with some general rules.

Tidying Up

First up, those background images need to appear just once, so we’ll add a no-repeat. They also need to be central and scaled properly.

Some border-radius added to .door and .door div gives us a more friendly aesthetic. And finally, we link to the font Kalam on Google fonts to polish that last typographic detail:

Styles:

Conclusion

That about wraps things up! Of course, this is really just for novelty value and practicing CSS Grid (the status of each door isn’t saved for the next browser session or anything) but we’ve come a long way with just some markup and some styles. 

Support is one last point to mention; there are a few aspects of this tutorial which don’t enjoy complete browser support yet–for some tips on how to deal with this see the resources linked below.

I hope you enjoy the tutorial, and enjoy counting down the days!

Useful Resources

CSS Grid Courses on Tuts+