
*update* I have re-done this tutorial with unordered lists and css sprites. Check it out here.
Calendars are such an everyday object for most people – they are literally everywhere, in print, on our cell phones, computers and on the web. I recently had to design and code a calendar at work and learned a few things along the way. I thought I would show you guys my work process and share some code with you.
Check out the Yoga Get Started Guide on the Men’s Health website »
The Design
First off lets start out with the design. I designed the layout in photoshop. One thing that helps with designing is to make your PSD file look as real as possible so there’s no guesswork when it comes to coding on things like color, dimension or size.

So the challenging part of this the coding the calendar. I’m not the best css-wizzard out there, but I can hold my own. I knew the design was going to be a bunch of divs floated left with some margins going on.
A few things I learned:
Behavior of a:visted states: they remain in their visited state sitewide!!!
Behavior of display:block on an anchor link – this makes the whole calendar day a clickable link. Not sure why I did not know this before, but hey – my lightbulb went on!
So with this new knowledge in hand I knew that all the magic for this design was going to happen with the link states.
Layout
The layout of the calendar will be pretty basic: a container div, and a div for each day:

Each day is 105×105px square with 1px margin on the right and bottom. The client wanted this to look like a real calendar so there are 7 days in each row. This means the container div needs to be at least 742px wide. I threw in a sprinkle of a few extra pixels for fun. The divs will be floated left and displayed inline (because IE is stupid and doubles margins on floated objects). I also wanted to hide any text just in case any text ran long. Take a look at the basic CSS and HTML for the layout:
Basic CSS
#calendarContainer {
width:745px;
}.day {
width:105px;
height:105px;
overflow:hidden;
display:inline;
float:left;
margin-right:1px;
margin-top:1px;
}
Basic HTML
<div id=”calendarContainer</div>
<div class=”day”>
<a href=”#”>
<h3>1</h3></a>
</div><!– end day –>
</div><!– end calendar container–>
I should also note that I have a (good) habbit of comenting on closing divs just so I can tell what the hell is going on
Background Images
Then I went back to my .PSD and sliced up the 4 background images and saved them as .gifs:
Blank Days:

Normal Days:

Hover State

Visited State

…and more CSS!
So now that we have our basic layout and images lets get down to the cool parts: The link states!
First off we have the blank days that make it look like an actual calendar:
.blankDay {
width:105px;
height:105px;
background:url(../images/day-blank.gif);
margin-right:1px ;
margin-top:1px;
overflow:hidden;
display:inline;
float:left;}
Next up we define the background image for the days with content and the hover state. Setting the link to display as a block level element makes the whole div clickable! Yay!
.day a {
width:105px;
height:105px;
background:url(day-normal.gif);
background-repeat:no-repeat;
text-decoration:none;
display: block;
color:#3e3d3d;}
.day a:hover {
background:url(day-hover.gif);
background-repeat:no-repeat;
width:105px;
height:105px;
display: block;
color:#ffffff;
}.day a:visited {
background:url(day-visited.gif);
background-repeat:no-repeat;
width:105px;
height:105px;
display: block;
color:#7f7e7e !important;
}
Typography
Next up we define the typography and the colors of the Day # on all the link states:
.day h3 {
font-size:33px;
font-weight:bold;
color:#982302;
float:right;
margin-top:15px;
margin-right:15px;
}a:hover h3 {
color:#ffffff !important;
}a:visited h3 {
color:#7f7e7e !important;
}
The Finished Product!!!
If all goes well you should have a nice cleanly coded calendar like this:
23 Responses to “Coding Calendars For The Web”
Leave a Reply
Trackbacks & Pingbacks:
-
Pingback from Arbenting's Weekly Inspiration and Best of the Web #6 | Arbenting
May 24th, 2009 at 3:49 pm[...] Coding Calendars For The Web – Calendars are such an everyday object for most people – they are literally everywhere, in print, on our cell phones, computers and on the web. I recently had to design and code a calendar at work and learned a few things along the way. I thought I would show you guys my work process and share some code with you. [...]
-
Pingback from The Design O'Blog
May 31st, 2009 at 9:14 am[...] few weeks ago I wrote a tutorial explaining how I coded a calendar for a client project at my day job (Brunello Creative). After some comments from readers I realized [...]











Marco says:
Nice one Niki, very useful.
Just one thing: You should have used CSS sprites for the background images – It now takes a short while to load the “hover” effects, making it look like a “blink”.
Anyway, keep up the good work!
May 18th, 2009 at 4:56 am
Niki says:
@marco
Good point! I have not really looked into CSS sprites – will have to take a crack at it!
May 18th, 2009 at 5:01 am
Jack F says:
Have to say I would have been tempted to use a table, surely this qualifies as tabular data, is there a particular reason why you chose not to do so?
May 18th, 2009 at 5:45 am
Seth Stevenson says:
Brilliant thinking with the a:visited state!
May 18th, 2009 at 6:41 am
Simon Douglas says:
Great post.
Have to agree with @marco about CSS sprites, and @jackf re. tabular data.
I would also be tempted to change the order of your css, such that the a:hover state comes after a:visited.
May 18th, 2009 at 7:12 am
Liz says:
You’re going to love sprites!
This was a great post to see – especially since I am currently working with a client to develop a calendar system that pulls from gcal and detects time zone, etc. We haven’t even got into the design phase, but i know that will be coming up and its good to see that a traditional view calendar can be accomplished and looks sleek and not cluttered and gross! Good work, Niki!
May 18th, 2009 at 9:27 am
Benjamin Reid says:
Nice run down on the on how you made it. I can see jQuery fitting in with this quite nicely too. I need to make/find a decent calendar plugin for WordPress as most are pretty nasty. This would be a good starting point.
May 18th, 2009 at 9:33 am
Mario says:
WOW. This is really good. Thanks,
May 18th, 2009 at 1:14 pm
Gavin Tronset says:
Aside from using sprites and the order of the CSS, the addition of the pseudo-class :active would improve usability. Current, when clicked and held down, the only change of appearance is the date changes to white, meaning you have white text on white background: that’s a no-no
.
May 18th, 2009 at 1:17 pm
Niki says:
@gavin – i actually have the a:active class in the CSS of the example…dunno why i didnt have it in the post…will update!
Thanks everyone for the feedback!
Hopefully I’ll do more posts like this. I learn so much by explaining how I did things and you guys point out excellent things!
May 18th, 2009 at 1:29 pm
Steph Adamo says:
I love that you did this with CSS. Nice work!
May 18th, 2009 at 2:35 pm
Dani McDaniel says:
Great post Niki! Thanks so much for sharing
May 18th, 2009 at 4:21 pm
Nikki says:
Oh I’m loving this! I was looking into Calendar plugins for a Wordpress project I’ve got, but I think I might just make my own – this is a lot easier than I thought it was, and it looks great! Thanks for sharing this.
May 18th, 2009 at 8:40 pm
Sebastian Steinmann says:
Nice work putting a block-element inside a inline element tho :p
I would move that a inside the h3.
Also, you should add a state for the day to be in the past. Done with sprites ofcource.
You can see the problems of not using it by checking your example.
Takes a second before the hover image is shown.
Nice simple example tho!
May 18th, 2009 at 10:35 pm
Tracey Grady says:
Thanks Niki, this will save me some time when the next project with a calendar crops up. To everyone who has contributed useful additions in the comments, thanks as well.
May 19th, 2009 at 3:23 am
matt says:
Niki,
Nice! I like it. The only concern I have though is an accessibility thing: This would seem to be an excellent candidate for a TABLE (as Jack mentions in comment 3). That would allow your calendar to be awesome and accessible to more users!
In fact, there seems to be a dearth of good TABLE based calendars on the web. Maybe you could bring your CSS to bear on a TABLE based design?
May 19th, 2009 at 4:34 am
Catherine Cantieri, Sorted says:
I’m impressed. Great work, and elegant, too!
May 19th, 2009 at 7:08 am
Daniel [HiddenCSS] says:
DUDE! Thats pretty cool. Now I just need an excuse to use it!
May 20th, 2009 at 8:01 am
d2m.ca says:
Hahaha
This is sick. I love the presentation!!
Thanks
May 20th, 2009 at 10:59 pm
CreativeNotice says:
This is a great head start for me, thanks. I can see this really taking off with some jQuery goodness.
My first thought was also to use a table, but I wonder if some very cool animations couldn’t be cooked up using the divs, like sliding the days into the calendar with some easing, etc. Can’t do that with a table.
Also, it’s possible to stile layers as table elements, wonder how that would play with screen readers? Anyone have insight into that?
Great job Nikki!
May 21st, 2009 at 8:44 am
John Fredrickson says:
It makes zero sense to mark up a calendar as a table. Tables are for tabular data, and a calendar is only tabular in it’s display. On the code level, tables are actually lists of data and should probably be marked up as an ordered list, if you want to be really picky.
June 9th, 2009 at 7:14 am