Create a Sticky Note Effect in 5 Easy Steps with CSS3 and HTML5

Create a Sticky Note Effect in 5 Easy Steps with CSS3 and HTML5

Tutorial Details
  • Languages Used: HTML, CSS
  • Estimated Difficulty: Intermediate

Final Product What You'll Be Creating

This entry is part 14 of 14 in the HTML5 and You Session
« Previous

Twice a month, we revisit some of our readers’ favorite posts from through out the history of Nettuts+. This tutorial was first published in August, 2010.

In this tutorial, you’ll learn how to transform an HTML list into a wall of “sticky notes” that look and work like the following…

The effect is built up gradually and works on the latest Webkit browsers (Safari, Chrome), Firefox and Opera. Other browsers simply get some yellow squares.


Step 1: The HTML and Basic Squares

Let’s start with the simplest version that works across all browsers. As we are using HTML5 for the effect, the basic HTML of our sticky notes is an unordered list with a link containing all the other elements in each list item:

<ul>
  <li>
    <a href="#">
      <h2>Title #1</h2>
      <p>Text Content #1</p>
    </a>
  </li>
  <li>
    <a href="#">
      <h2>Title #2</h2>
      <p>Text Content #2</p>
    </a>
  </li>
  […]
</ul>

Notice that each note is surrounded by a link which is always a good element to use as it automatically means that our notes become keyboard accessible. If we used the list item for the effect we’d need to set a tabindex property to get the same access.

The CSS to turn this into the yellow squares is simple:

*{
  margin:0;
  padding:0;
}
body{
  font-family:arial,sans-serif;
  font-size:100%;
  margin:3em;
  background:#666;
  color:#fff;
}
h2,p{
  font-size:100%;
  font-weight:normal;
}
ul,li{
  list-style:none;
}
ul{
  overflow:hidden;
  padding:3em;
}
ul li a{
  text-decoration:none;
  color:#000;
  background:#ffc;
  display:block;
  height:10em;
  width:10em;
  padding:1em;
}
ul li{
  margin:1em;
  float:left;
}

We reset things the browser normally gives us like margins and paddings and the list style to get rid of the bullets of the list.

We then give the UL element some padding and set its overflow property to hidden – this makes sure that when we float the list items later on they are contained in the list and the following elements in the document automatically clear the float.

We style the link as a yellow rectangle and float all of the list items to the left. The result is a series of yellow boxes for our list:

Step1: a series of yellow boxes

This works for every browser out there – including IE6. This is also where we end supporting this browser as we should not shoe-horn visual effects supported by modern technology into outdated one.


Step 2: Drop Shadows and Scribbly Font

Now it is time to add a drop shadow to the notes to make them stand out and to use a scribbly, hand-written font as the note font. For this we use the Google Fonts API and the font they provide us with, called “Reenie Beanie”. The easiest way to use this API is to play with the Google font previewer:

The Google font previewer allows you to play with the fonts API and get copy+paste CSS code

Using this, we get a simple line of HTML to include this new font into the page. This is supported by all modern browsers.

<link  href="http://fonts.googleapis.com/css?
family=Reenie+Beanie:regular" 
rel="stylesheet" 
type="text/css">

We then can set some padding to the headings in the sticky notes, and set the font of the paragraphs to the new font we included. Notice that Reenie Beanie needs to be big to be readable:

ul li h2{
  font-size:140%;
  font-weight:bold;
  padding-bottom:10px;
}
ul li p{
  font-family:"Reenie Beanie",arial,sans-serif;
  font-size:180%;
}

In order to give the sticky notes a shadow to make them stand out from the page, we need to apply a box-shadow. For this, we must add a line for each of the different browsers we want to support to the style of the links:

ul li a{
  text-decoration:none;
  color:#000;
  background:#ffc;
  display:block;
  height:10em;
  width:10em;
  padding:1em;
  /* Firefox */
  -moz-box-shadow:5px 5px 7px rgba(33,33,33,1);
  /* Safari+Chrome */
  -webkit-box-shadow: 5px 5px 7px rgba(33,33,33,.7);
  /* Opera */
  box-shadow: 5px 5px 7px rgba(33,33,33,.7);
}

The syntax is luckily the same for each: offset, spread and colour – in this case a dark grey with an opacity of 70%. Together with the new font our sticky notes now look like this:

Step2: adding new fonts and drop shadows

Step 3: Tilting the Notes

Disclaimer: Both the tilting of the notes and the zooming we’ll add in the next step were already explained in the past, in this article by Zurb, but lacked the support for other browsers, as they weren’t out at the time of writing. So big thanks to them for publishing this trick.

In order to tilt an element you use the transform:rotate property of CSS3, again adding the prefix for each of the browsers:

ul li a{
  -webkit-transform:rotate(-6deg);
  -o-transform:rotate(-6deg);
  -moz-transform:rotate(-6deg);
}

This tilts all the links by six degrees to the left. Now to make the sticky notes appear to be randomly tilted, we can use the nth-child property of CSS3:

ul li:nth-child(even) a{
  -o-transform:rotate(4deg);
  -webkit-transform:rotate(4deg);
  -moz-transform:rotate(4deg);
  position:relative;
  top:5px;
}
ul li:nth-child(3n) a{
  -o-transform:rotate(-3deg);
  -webkit-transform:rotate(-3deg);
  -moz-transform:rotate(-3deg);
  position:relative;
  top:-5px;
}
ul li:nth-child(5n) a{
  -o-transform:rotate(5deg);
  -webkit-transform:rotate(5deg);
  -moz-transform:rotate(5deg);
  position:relative;
  top:-10px;
}

This tilts every second link four degrees to the right, and offsets it a bit by five pixels from the top. Every third link gets tilted by three degrees to the left and pushed up five pixels. And every fifth link gets rotated five degrees to the right and offset ten pixels from the bottom. All in all this gives the impression of random sticky notes:

Step3: seemingly random sticky notes

Step 4: Zooming the Sticky Notes on Hover and Focus

To make a sticky note stand out we use a larger drop shadow and the scale transformation of CSS3. Again, we need to define these for each of the browsers:

ul li a:hover,ul li a:focus{
  -moz-box-shadow:10px 10px 7px rgba(0,0,0,.7);
  -webkit-box-shadow: 10px 10px 7px rgba(0,0,0,.7);
  box-shadow:10px 10px 7px rgba(0,0,0,.7);
  -webkit-transform: scale(1.25);
  -moz-transform: scale(1.25);
  -o-transform: scale(1.25);
  position:relative;
  z-index:5;
}

We also add a higher z-index to ensure that the enlarged sticky note covers the others. As we apply this on hover and focus,it means that moving the mouse over or tabbing to a link now makes it stand out:

Step4: Zooming the current sticky note

Step 5: Adding Smooth Transitions and Colors

The last step is to make the change from tilted to zoomed smooth and appealing rather than sudden. For this we use the CSS3 transition module in its different browser vendor implementations:

ul li a{
  text-decoration:none;
  color:#000;
  background:#ffc;
  display:block;
  height:10em;
  width:10em;
  padding:1em;
  -moz-box-shadow:5px 5px 7px rgba(33,33,33,1);
  -webkit-box-shadow: 5px 5px 7px rgba(33,33,33,.7);
  box-shadow: 5px 5px 7px rgba(33,33,33,.7);
  -moz-transition:-moz-transform .15s linear;
  -o-transition:-o-transform .15s linear;
  -webkit-transition:-webkit-transform .15s linear;
}

In essence this says: if something is to change to this element, do not just switch to that other definition but do it gradually during a quarter of a second. As another extra, let’s add some colour into the mix by making every second sticky note green and every third light blue:

ul li:nth-child(even) a{
  -o-transform:rotate(4deg);
  -webkit-transform:rotate(4deg);
  -moz-transform:rotate(4deg);
  position:relative;
  top:5px;
  background:#cfc;
}
ul li:nth-child(3n) a{
  -o-transform:rotate(-3deg);
  -webkit-transform:rotate(-3deg);
  -moz-transform:rotate(-3deg);
  position:relative;
  top:-5px;
  background:#ccf;
}

In order to see the difference to the last step, you’d need to try the last demo out in a browser.

Step 5:Coloured and smoothly zooming sticky notes

Summary and Download

There you have it – smoothly animating and tilted sticky notes without any use of images or JavaScript – supported by Firefox, Opera, Safari and Chrome and falling back to a set of yellow boxes in older browsers. By clever use of the nth-child selector and CSS transformations and transitions, we saved ourselves some scripting. Further, Google’s Web Font API made it easy to use a custom font. Using both hover and focus for the effect also means that keyboard users can observe the results as well.

Download the sticky notes example as a zip.


About the Author

Christian Heilmann is an international Developer Evangelist who works for the Yahoo Developer Network in the lovely town of London, England. He’s written two books: “Beginning JavaScript with DOM Scripting and AJAX“, and “Web Development Solutions.”

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://gsvoren.net Stownard

    Thats so cool! Thanks for sharing! :-)

  • http://siriusgraphics.net Jordan Vidrine

    Great tutorial, one minor fix would be to take off the outline of the links. For firefox, it borders the whole link plus some.

    Thanks for the tut!

    • http://wait-till-i.com Chris Heilmann

      I’d be very careful with removing outlines of links – some people who can only use keyboards are dependent on that hint.

    • http://jonathancutrell.com Jonathan Cutrell

      An acceptable solution is to make the outlines nicer looking, of course. But don’t remove them – accessibility is still important!

  • http://www.jebrini.net Mohd Jebrini

    Great Tuts .. thank you

  • http://laroouse.com edurup

    very nice work thanks for sharing

  • http://inviadesign.com ivan

    nice tut but would be nice if you use mootools to make it “Moove”

    here is the link if anyone wanna take a look

    http://code.google.com/apis/ajaxlibs/documentation/index.html#mootools

    thanks for this tuts :D

    • http://wait-till-i.com Chris Heilmann

      I could also add bananas to it, but that doesn’t make it sound like a monkey. What exactly was the point of this comment?

    • http://www.stevendavisphoto.com Steven Davis

      Why add javascript (bulk) to something that can be done natively with CSS3? The only case for that I see is if you wanna support IE6. And in that case, I’d use jQuery over Mootools :P

      • Ben Cooper

        What about support for IE7/8? ….. Only IE9 has partial support

  • http://mikebarnhardt.com Mike

    Wouldn’t the shadows be lighter as the sticky note moves farther away from the background?

    • w1sh

      Maybe someone is shining a flashlight on them as they pick them up to inspect them.

  • Fernando Lee

    Great work, I also apreciate the way u structured the demo files in steps :D its a simple way to see how it works, and get the things u really need to implement on aproject, thank u

  • http://www.squidoo.com/carheadrestdvdplayer headrest dvd player guy

    i really get excited seeing all the great effects of css3 and html 5.But the sadest part is firefox still doesnt support webkit.I sometimes use a the next version firefox name minefield and it supports everything and very cool but still its just a sort of demo what coming next.Anyone have any idea when mozilla will launch it officially?

  • Duane Gran

    Nice implementation and effect. I did some experimenting with this and found that it is pretty easy to create too much text to fit within the note. I added an “overflow:auto” directive tot he “ul li a” section, but this doesn’t look very nice and the scroll bars don’t work properly when the zoom effect happens. A better solution seems to me to set “height:auto”. The notes aren’t perfect boxes, but it handles different lengths of content more nicely.

    If anyone has other tweaks or ideas how to handle variable content I’m interested to hear of it.

  • Paul Gisbourne

    Great Tut,

    tried this on a test page with some scrolling and noticed that when you scrolled down the page and then first hovered over an element the page would flicker.
    Using with Safari and webkit code only.

    • Jeff Rafter

      Great tutorial.

      Paul: in webkit you need to add

      ul li a {
      -webkit-backface-visibility: hidden;
      }

      Unfortunately this makes the text very blurry. Not sure how to do both.

  • http://www.ifranzi.com iFranzi

    super articol!

  • chichibek

    wow increible, realmente util estoy trabajando en algo parecido este ejemplo me ayudara
    gracias por compartir

  • http://adrusi.com/ adrian sinclair

    I created a similar effect, and coauthored an article with chris coyier over on CSS-tricks, it adds a pure css click event using tabindex (or in the original, contenteditable). http://css-tricks.com/expanding-images-html5/
    I also made a css module about a week ago that did almost the exact effect in this tutorial, it’s amazing

  • http://holamiamor.xo.st Irene

    Gooooooooood ; )

  • http://www.ahkeno.com ahkeno

    That why I love net.tutsplus..:) Nice post

  • http://www.swordair.com/blog iifksp

    Cool!Thanks for your great tuts!

  • http://www.xcellence-it.com Xcellence IT

    cool demo.. thanks for sharing

  • http://gauravmishra.com Gaurav Mishra

    Very smart execution. Superb!

  • http://www.hamptonassociates.com David

    Very nice
    What number of browsers support all the features on here out of interest?

    • http://www.stevendavisphoto.com Steven Davis

      All the modern browsers (FF, Chrome, Safari, and Opera) support it.

  • http://ahoyman.com Dustin Hoyman

    Where does the HTML5 part come in?

  • http://cg-lion.com CGLion

    A great start to get involved in HTML 5, thanx a lot editor…

  • http://www.sagie.es cesar

    Excellent work, thanks for sharing!

    I would personally add ‘ul li a{outline: 0;}’ to the CSS to get rid of those ugly borders u get when selecting a note.

    Anyway, it´s a superb job, and the post is very well written. Thanks a lot for sharing this!

  • http://shamekh.ws/en Waseem

    Nice tut , Thank you .

  • Yousuf

    Nice tutorial!!

  • http://www.vincentveri.com Vincent Veri

    I totally agree with CGLion. Nice article.
    There’s just a little error in the declaration of box-shadow property, that causes an error in the css validation of the code:

    # /* Opera */
    # box-shadow: 5px 5px 7px rgba(33,33,33,.7);

    the property should be -o-box-shadow.

    It’s a very good article btw.
    Thanks a lot for sharing

  • Angela

    Great tutorial, thanks!

  • Djkanna

    Awesome tutorial,
    perhaps adding like a Two shade darker border to under the sticky note title to give it that stuck down (on the generally sticky part) look.

    Djkanna.

  • http://rajesharma.com broncha

    Nice tutorial. Thanks !!

  • http://aoberoi.me Ankur

    great tutorial! i just added the ‘contenteditable’ attribute on the h2 and p tags and its quite usable in webkit. i also appreciate how you break things down into steps and truely consider progressive enhancement.

    one question, when resetting in the first few css statements, why do you explicitly set font-size to 100%? is this a workaround for some known browser bug?

  • http://rapbaz.com Ika

    Thanks! It was really useful

  • http://itspice.net Avani

    Fantastic Tutorial ! CSS3 & HTML 5 can do wonders !

  • http://www.twitter.com/dajowebdesign Darren Hayes

    Very cool, thanks for sharing.

  • http://www.ardakazanci.tk Arda Kazancı

    Good Tutorial ;)

  • James

    Hi, nice looking tutorial! Only gripe I have with it is that wrapping an anchor around a heading or paragraph tag is not valid xHTML. If that is not an issue for you, then – as you were!

  • http://www.richardkotze.com Richard Kotze

    Great tutorial – you can really see a strong future for HTML5 and CSS3.

  • http://None Ardi Vaba

    Believe me or not, but i can’t test the demo, because my Firefox 4 beta crashes each time the demo gets fully loaded…

  • http://kevinurrutia.com/ Kevin

    Thank you for this. I will be trying this out soon.

  • http://www.crearedesign.co.uk Steve Maggs

    Really nice quick tutorial. Proves that they don’t all have to be in depth forays in to coding.

  • http://myfacefriends.com Jehnee

    really nice tuts!

  • http://www.litmusonline.com Litmusonline

    Nice tutorial and good explanation of sticky note. Let me try it.

  • http://www.litmusonline.com Litmusonline

    Video tutorial is amazing and good explanation of sticky note. Let me try it.

  • http://www.move-my-site.com/ Server Migration

    nice tutorial . I will do this my self…Thanks for sharing….

  • http://www.biztechconsultancy.com/ PHP Developer India

    hey!!! this is different kind of post i like it.Thanks for sharing…

  • Gixx

    Nice! But using prefixed css is the same as using filter:progid:DXImagetransform… for the IE. Neither the prefixed and filter is valid, so don’t say that IE cannot rotate, because it can.

    demo: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/filters/matrix.htm
    reference: http://msdn.microsoft.com/en-us/library/ms533014(v=VS.85).aspx

  • http://beben-koben.blogspot.com/ Beben

    wow…its can be rolling2 in hover…
    nice nice ^^

  • http://healthenfermeradesalud.blogspot.com cutecankil

    nice and interesting keep it up!!

  • http://www.sector3it.com Sector3

    Liking the effect, we created a similar effect for a client just using images pushed out in SVG although I remember it was a pain. Things do seem easier in HTML5 and CSS3 looking forward to the future.

  • http://tarex.name tareq

    awesome tuts :)
    keep it up!

  • ngassmann

    And over a year later, the demo still renders all jaggedy in Chrome on a PC. Le sigh.

  • http://toopan.wordpress.com TopanLutfi

    Great tuts,
    but I just want to look good in IE. I don’t look feature css3 in there.
    can u do it.

    thanks