Building a calendar with CSS Grid

Building a calendar with CSS Grid

Building a calendar with CSS Grid is quite simple. Here's how to do it.

Here’s what you’ll create by the end of this article:

Creating the HTML

You can tell from the image that the calendar contains three parts:

  1. The month indicator
  2. The weekday/weekend indicator
  3. The dates themselves

The best way to structure the HTML is to go with what feels right. We'll create the HTML according to these three sections:

<div class="calendar">
  <div class="month-indicator">...</div>
  <div class="day-of-week">...</div>
  <div class="date-grid">...</div>
</div>

You should also be able to see we need seven columns for the grid.

We'll focus the conversation on .day-of-week and .date-grid since we're only talking about grid.

Structuring the grid

There are two ways to create the CSS Grid.

The first way is to merge elements within .day-of-week and .date-grid into one selector. If we do this, we can set the selector in display: grid. Here's what the HTML would have looked like if we did this:

<div class="grid">
  <!-- Day of week -->
  <div>Su</div>
  <div>Mo</div>
  <div>Tu</div>
  <div>We</div>
  <div>Th</div>
  <div>Fr</div>
  <div>Sa</div>

<!-- Dates -->
<button><time datetime="2019-02-01">1</time></button>
<button><time datetime="2019-02-02">2</time></button>
<button><time datetime="2019-02-03">3</time></button>
<!-- ... -->
<button><time datetime="2019-02-28">28</time></button>
</div>

I discourage this method because the HTML loses its structural meaning. I prefer keeping .day-of-week and date-grid as separate elements if possible. This makes it easy for me to read/understand the code I've written.

Here's the HTML structure i chose to go with:

<div class="day-of-week">
<div>Su</div>
<div>Mo</div>
<div>Tu</div>
<div>We</div>
<div>Th</div>
<div>Fr</div>
<div>Sa</div>
</div>

<div class="date-grid">
<button><time datetime="2019-02-01">1</time></button>
<button><time datetime="2019-02-02">2</time></button>
<button><time datetime="2019-02-03">3</time></button>
<!-- ... -->
<button><time datetime="2019-02-28">28</time></button>
</div>

The best way to create a CSS Grid with the structure I proposed is to use subgrid. Unfortunately, most browsers don't support subgrid yet. In the meantime, the best way is to create two separate grids—one for .day-of-week and one for .date-grid.

Both .day-of-week and .date-grid can use the same seven-column grid.

/* The grid /
.day-of-week,
.date-grid {
display: grid;
grid-template-columns: repeat(7, 1fr);
}

Pushing the dates

February 2019 begins on a Friday. If we want the calendar to be correct, we need to make sure:

  1. 1 Feb 2019 falls on Friday
  2. 2 Feb 2019 falls on Saturday
  3. 3 Feb 2019 falls on Sunday
  4. And so on...

With CSS Grid, this part is easy.

CSS Grid has placement algorithm that kinda follows the following rules (if you didn't set grid-auto-flow to dense):

  1. Place items that has explicit grid-column or grid-row first
  2. Fill in the rest according to the last-placed item

What this means is:

  1. If the first item falls on column 6
  2. The second item will be placed in column 7.
  3. The third item will be placed on the next row, in column 1 (because there are only seven columns).
  4. The fourth item will be placed in column 2,
  5. And so on...

So, if we position 1 February on the sixth column (friday), the rest of the dates will be placed correctly.

Simple as that!

/ Positioning the first day on a Friday */
.date-grid button:first-child {
grid-column: 6;
}

Here's a codepen for you to play with:

Want to learn more?

This article contains one fraction of a component (a datepicker) from Learn JavaScript. There's so much more I want to show you. (But they're mostly JavaScript related topics).

For example, in Learn JavaScript, I show you how to:

  1. Build a calendar for any month (and any year)
  2. Add a previous/next button to switch between months
  3. Click on each date to display a date

Here's what it looks like:

Thanks For Visiting, Keep Visiting. If you liked this post, share it with all of your programming buddies!

Further reading

Build a React Calendar Component from scratch

Making a Calendar in Vanilla Javascript

Introduction To HTML & CSS: Web Development For Beginners

Introduction To HTML & CSS: Web Development For Beginners

Introduction To HTML & CSS: Web Development For Beginners - Complete HTML & CSS web developer bootcamp. Learn & practice website design using HTML5 & CSS3 web development resources

Welcome to Introduction To HTML & CSS: Web Development For Beginners: This is a comprehensive and Practical guide that is designed to pick up every interested student from the state of "zero-knowledge" to a state of "Hero-knowledge" in HTML and CSS. As a senior software developer and an instructor with many years of experience in the industry, I have mastered the best way to give my students packages that will help them to learn and understand HTML and CSS in one course.

What you'll learn

  • You will learn the basics of web development
  • You will be able to use the HTML tags professionally
  • You will learn how to work with images in HTML and CSS
  • You will learn how to create awesome backgrounds
  • You will how to use CSS colors
  • You will master most web development documentations used by professionals
  • You will learn how to embed multimedia in web pages
  • You will learn how to make comments in both HTML and CSS
  • You will learn the Box Model

Mobile App Development Company India | Ecommerce Web Development Company India

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

We are custom eCommerce Development Company working with all types of industry verticals and providing them end-to-end solutions for their eCommerce store development.

Know more about Top E-Commerce Web Development Company

Web Development Services

Web Development Services

As one of the best Web Application Development Company, it provides a fine quality mobile app development service at an affordable price. Especially, it encourage start-ups that have unique ideas, by offering a more competitive price

HireFullStackDeveloperIndia is rated as one of the top Web Application Development Company in India by various industry magazines and review sites. They have a right blend of award-winning designers, expert programmers and Google certified digital marketers which make them a unique one-stop solution for hundreds of our clients, spread across all countries.

A Good website reflects not only your business but also it is one of the main factors why a potential customer would convert into Client. A good website design helps increase traffic driving leads to grow business. The best web design company create a custom design for each corporate website so as to help them meet their business goals.

Get Quote: https://hirefullstackdeveloperindia.com/get-a-quote/