← Back toCSS grid

Remove empty cells from the calendar

Written byPhuoc Nguyen
Created
21 Dec, 2023
Tags
CSS grid calendar
In our previous post, we learned how to use CSS grid to create a calendar. Now, let's take a closer look at the markup we used to lay out the calendar.
The calendar is made up of two elements: one for the weekdays and one for the days in the month. This simple structure helps us organize the calendar and make it easy to read.
html
<div class="calendar">
<div class="calendar__days">
<div>Sun</div>
<div>Mon</div>
<div>Tue</div>
<div>Wed</div>
<div>Thu</div>
<div>Fri</div>
<div>Sat</div>
</div>

<div class="calendar__dates">
<!-- Week 1 -->
<div></div>
<div></div>
<div></div>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>

<!-- Week 2 -->
<!-- Continue with the rest of the days in the month -->
</div>
</div>
We also learned how to use CSS grid to display the days of the month. First, we applied the `display: grid` property to the dates container to turn it into a grid. Then we set the `grid-template-columns` property to `repeat(7, 1fr)`, which created seven columns of equal width. This ensured that each day of the week would be evenly distributed across the width of the container.
Here's a code snippet to refresh your memory:
css
.calendar__dates {
display: grid;
grid-template-columns: repeat(7, 1fr);
}
This is how we create a calendar using CSS grid:
Take a look at the container of dates in the month and pay attention to the empty `div` elements. These `div` elements are used to represent dates that do not belong to the current month. For example, if the first day of the month falls on a Wednesday (which is the fourth column of the grid) rather than a Sunday (which is the first column), we would need to use three empty `div` elements.
html
<!-- Week 1 -->
<div></div>
<div></div>
<div></div>
<div class="calendar__date--first">1</div>
<div>2</div>
<div>3</div>
<div>4</div>
In the previous post, we introduced the `grid-column-start` property, which we can use to remove empty cells in our calendar. For example, if we want to skip the first three cells in the grid and start with the fourth cell, we can set the `grid-column-start` property value to 4 for the first day of the month. This will cause it to span over to column four of the grid and fill up that space. By doing so, we can avoid adding empty divs for those first few days that are not part of the current month.
css
.calendar__date--first {
grid-column-start: 4;
}
Since the content only occupies a single cell, there's no need to set the `grid-column-end` property. However, if you prefer the shorthand property, you can use the following declaration instead and achieve the same result:
css
.calendar__date--first {
grid-column: 4;
}
Take a look at the result:

Conclusion

In conclusion, we have learned how to remove empty cells from the calendar using CSS grid property. By setting the `grid-column-start` property value to the appropriate column, we can start displaying dates that belong to the current month without leaving any empty cells. This technique not only improves the appearance of our calendar but also makes it more efficient by reducing its code complexity.
If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

Questions? 🙋

Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Ask me questions

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn