posted on:February 7, 2011

Drawing Calendar Icon With CSS3

In this article I am going to use some of the CSS tricks to create a calendar icon you can use for your blog posts or something similar. It is important to notify that there are no images used here and the markup couldn’t be simpler than it is.

The final result looks like the image below.

Take a look at the demo


As mentioned the HTML I am using for making this calendar icon to work is very simple:

<p class="calendar">7 <em>February</em></p>

We have a wrapping paragraph, although you may go with the DIV (or HTML5’s new element – TIME). Inside the wrapping element you need one extra element that contains a month’s name.

The principle

Now, I have 2 elements to work with, plus I will create 2 pseudo element for each of the real elements which will give me total of 6 elements I can use to draw shapes and position them properly. Pseudo elements will be used for the calendar’s spiral (actually those are some kind of rings, not sure what is the correct English word for it… sorry about that).

If you take a look at the image below the idea behind this will be more clear.

First we style the container element. You will notice that I used box-shadow, border-radius and CSS gradients. Not all browsers will render all of these properties, but at least it will degrade nicely.
Note that the fixed height is not defined, you control the overall height with line-height properties in both container and nested em element.

	margin:.25em 10px 10px 0;
	background: -webkit-gradient(linear, left top, left bottom, from(#ededef), to(#ccc)); 
	background: -moz-linear-gradient(top,  #ededef,  #ccc); 
	font:bold 30px/60px Arial Black, Arial, Helvetica, sans-serif;
	text-shadow:#fff 0 1px 0;	
	-moz-box-shadow:0 2px 2px #888;
	-webkit-box-shadow:0 2px 2px #888;
	box-shadow:0 2px 2px #888;

Em element is also styled, it contains the month’s name.

.calendar em{
	font:normal bold 11px/30px Arial, Helvetica, sans-serif;
	text-shadow:#00365a 0 -1px 0;	
	background:-webkit-gradient(linear, left top, left bottom, from(#04599a), to(#00365a)); 
	background:-moz-linear-gradient(top,  #04599a,  #00365a); 
	border-top:1px solid #00365a;

Now I am styling the pseudo elements. Container’s pseudo elements (:before and :after) are used to create thos circles, "holes in te paper".

.calendar:before, .calendar:after{
	-moz-box-shadow:0 1px 1px #fff;
	-webkit-box-shadow:0 1px 1px #fff;
	box-shadow:0 1px 1px #fff;

…and em’s pseudo elements are used to create the rings:

.calendar em:before, .calendar em:after{
	background:-webkit-gradient(linear, left top, left bottom, from(#f1f1f1), to(#aaa)); 
	background:-moz-linear-gradient(top,  #f1f1f1,  #aaa); 
.calendar em:before{left:13px;}	
.calendar em:after{right:13px;}	

There are 2 demos you can check out:

It is important to say that if you’re going to customize the width there will be some tweaking needed, mainly for the placement of the pseudo elements.

That is it! Enjoy! 🙂

Enjoyed the article?

Then don't miss our next one! Subscribe to our RSS feed or share it with your friends.

DeliciousStumbleUpon RedditSubscribe

Comments (20 Comments)

  1. redsonic
    February 7, 2011

    Hi, Thanks, These colors make me happy :) However the em element (Blue) is not as larger as the day container element (Grey) I added theses rule to the .calendar em class to get the same width : margin-left: -1px; width: 82px; May you have a better choice? Thanks
  2. redsonic
    February 7, 2011

    may be*
  3. Michael Martin
    February 7, 2011

    The icon looks great, awesome to see such a good demo of CSS3!
  4. cssglobe
    February 7, 2011

    @redsonic: They're both the same width. The blue appears to be more narrow, but it's not.
  5. Dave
    February 7, 2011

    Is there an official list of what browsers support CSS3? Would like to use something like this on a site I am working on, but unclear how the browser coverage would be?
  6. Ralph
    February 8, 2011

    Nice one, Allen! It's so cool what you can do with the before and after pseudo classes to keep the mark-up clean and lean. I got inspired too by Nicolas Gallagher who does crazy things with them, so I'm experimenting quite a lot with these two selectors. @Dave Pretty much all the latest browsers (including IE8) support the :before and :after pseudo classes. They're actually part of the CSS2.1 selectors. For a complete list of browser support re CSS selectors, I found this site very useful.
  7. san fransisco
    February 8, 2011

    Loved the article.
  8. dreamdezigns
    February 8, 2011

    hi its nice to look and your explanation is nice easy to understand by all
  9. Dan
    February 8, 2011

    Your example page gives an error in IE8.
  10. cssglobe
    February 8, 2011

    @Dan: Actually it is a third party banner ad script I have no control of... I didn't use nay JS in this particular case. I will have to see if that can be fixed.
  11. SEO Ontario
    February 9, 2011

    This is actually pretty clever. Most CSS3 icons require a huge amount of ugly markup, but this uses almost no extra HTML.
  12. Web design manchester
    February 9, 2011

    Nice work Great post
  13. Barry La Vette
    February 9, 2011

    Wow, code giving the illusion of an image. Fantastic!
  14. Video Gamer
    February 13, 2011

    thats kinda cool, ive been wanting to learn CSS, anyways thanks for the tutorial :D
  15. Michael Clark
    February 13, 2011

    Nice article. Great example of CSS. Runs beautifully for me in Chrome and FireFox, but it doesn't work in IE8. All I get in IE is the number and a single line below that. Any ideas of what could be done to get it to work in IE8?
  16. Michael Clark
    February 13, 2011

    Oops! I've been playing around with the DOCTYPE tag in HTML and I've found that the settings used can make a huge difference in the various browsers and how they react to CSS. For example, when I use the HTML 5 DOCTYPE tag, your CSS Calendar example does actually work in IE8. Still nowhere near as attractive as in Chrome or FireFox, though. Since I answered the question I asked earlier, I thought I'd drop back and comment on what I had found. Thanks again for the article.
  17. droope
    February 14, 2011

  18. Lars Schmidt
    February 16, 2011

    Sorry for my english. Is there any way to bring the content in one vertical line ? Please look at my second blogspot. Thanks.
  19. Lars Schmidt
    February 17, 2011

    Sorry. New position.
  20. San francisco
    February 17, 2011

    thats kinda cool..

Sorry, the comment form is closed at this time.