DEV Community

Cover image for Remove empty cells from the calendar
Phuoc Nguyen
Phuoc Nguyen

Posted on • Originally published at phuoc.ng

Remove empty cells from the 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.

<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>
Enter fullscreen mode Exit fullscreen mode

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:

.calendar__dates {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
}
Enter fullscreen mode Exit fullscreen mode

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.

 <!-- Week 1 -->
<div></div>
<div></div>
<div></div>
<div class="calendar__date--first">1</div>
<div>2</div>
<div>3</div>
<div>4</div>
Enter fullscreen mode Exit fullscreen mode

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.

.calendar__date--first {
    grid-column-start: 4;
}
Enter fullscreen mode Exit fullscreen mode

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:

.calendar__date--first {
    grid-column: 4;
}
Enter fullscreen mode Exit fullscreen mode

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 series 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!

If you want more helpful content like this, feel free to follow me:

Top comments (0)