PikoPong
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux
No Result
View All Result
PikoPong
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux
No Result
View All Result
PikoPong
No Result
View All Result
Home Web Dev

A Calendar in Three Lines of CSS – Calendar Tricks

December 24, 2020
in Web Dev
289 3
A Calendar in Three Lines of CSS – Calendar Tricks


This is an attempt to demonstrate how CSS Grid Layout makes it possible to make a calendar with only three lines of CSS.

While many would argue that a calendar should be built semantically using a table, there are others who think a list would also be appropriate. And the <time> element could also be a potential solution for semantic calendars.

My choice for this CSS calendar tutorial is a list. Why? Because it’s the easiest way to show the amazing capabilities of CSS grid. Yes, making more complex calendars has been getting easier since flexbox, but CSS grid simplifies things even more.

Calendar with CSS Grid

Let’s start our demo with the HTML part:

<div class="calendar-wrapper">
  <h1>Decemeber</h1>
  <ul class="calendar">
    <li class="weekday">Sun</li>
    <li class="weekday">Mon</li>
    <li class="weekday">Tue</li>
    <li class="weekday">Wed</li>
    <li class="weekday">Thu</li>
    <li class="weekday">Fri</li>
    <li class="weekday">Sat</li>

    <li class="first-day">1</li>
    <li>2</li>
    <li>3</li>
    
    <li>29</li>
    <li>30</li>
    <li>31</li>
  </ul>
</div>

To emphasize the simplicity I kept the calendar in one list. But you use use two separate lists, an unordered one for the days of the week and an ordered one for the actual days.

The CSS part couldn’t be shorter:

.calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
}
.first-day {
  grid-column-start: 3;
}

The first line is easy, it tells the browser to display the list as a grid. The second line, well, the second property, grid-template-columns, is used to define the size of each column. A week has seven days, we have seven columns. I could have easily written:

grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr;

Or:

grid-template-columns: 40px 40px 40px 40px 40px 40px 40px;

But that’s not only repetitive, it’s also prone to typos, so we can use repeat() because all our columns have the same width. The 1fr you see there is a new CSS unit of flexible length, you can read more about it here.

Finally, because most months don’t start on Sunday, we can use the grid-column-start property to specify which day is the first of the month.

There you have it: three lines of CSS to make a calendar. I encourage you to visit MDN and read more about CSS grid and all the things it can do.

See the Pen
Simple Calendar With CSS Grid
by Calendar Tricks (@calendartricks)
on CodePen.



Source link

Share219Tweet137Share55Pin49

Related Posts

Web Dev

Fresh Inspiration For March And A Smashing Winner (2021 Wallpapers Edition) — Smashing Magazine

Our wallpapers post this month is a special one: There’s not only a new collection of wallpapers created by...

February 28, 2021
Weekly Platform News: Reduced Motion, CORS, WhiteHouse.gov, popups, and 100vw
Web Dev

Weekly Platform News: Reduced Motion, CORS, WhiteHouse.gov, popups, and 100vw

In this week’s roundup, we highlight a proposal for a new <popup> element, check the use of prefers-reduced-motion on...

February 26, 2021
The Things I Add to Tailwind CSS Right Out of the Box
Web Dev

The Things I Add to Tailwind CSS Right Out of the Box

In every project where I use Tailwind CSS, I end up adding something to it. Some of these things...

February 26, 2021
Laravel Sail adds support for choosing which services you’d like installed
Web Dev

Laravel Sail adds support for choosing which services you’d like installed

Laravel Sail, a lightweight CLI for interacting with Laravel's default docker environment, recently launched a new update that allows...

February 26, 2021
Next Post
Integrating TypeScript with Svelte | CSS-Tricks

Integrating TypeScript with Svelte | CSS-Tricks

Responsible, Conditional Loading | CSS-Tricks

Responsible, Conditional Loading | CSS-Tricks

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

How to Italicize Text | CSS-Tricks

How to Italicize Text | CSS-Tricks

July 17, 2020
Continuous delivery for AWS CDK applications : idk.dev

Continuous delivery for AWS CDK applications : idk.dev

July 15, 2020
React Form Validation With Formik And Yup — Smashing Magazine

Data Visualization With ApexCharts — Smashing Magazine

November 5, 2020
Chapter 4: Search | CSS-Tricks

Chapter 4: Search | CSS-Tricks

September 15, 2020

Categories

  • AWS
  • Big Data
  • Database
  • DevOps
  • IoT
  • Linux
  • Web Dev
No Result
View All Result
  • Web Dev
  • Hack
  • Database
  • Big Data
  • AWS
  • Linux

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In