1. Web Design
  2. HTML/CSS
  3. HTML

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

Scroll to top
This post is part of a series called HTML5 and You.
HTML5 Apps: What, Why, and How

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 all up-to-date browsers like Chrome, Safari, Firefox, and Opera. Older browsers simply get some yellow squares.

Step 1: The HTML and Basic Squares

We will be using some common CSS properties that work 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:

1
<ul>
2
  <li>
3
    <a href="#">
4
      <h2>Title #1</h2>
5
      <p>Text Content #1</p>
6
    </a>
7
  </li>
8
  <li>
9
    <a href="#">
10
      <h2>Title #2</h2>
11
      <p>Text Content #2</p>
12
    </a>
13
  </li>
14
  […]
15
</ul>

Notice that each note is surrounded by a link. This is a good element to use for interactive items, 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:

1
body {
2
  margin: 20px auto;
3
  font-family: 'Lato';
4
  background:#666;
5
  color:#fff;
6
}
7
8
*{
9
  margin:0;
10
  padding:0;
11
}
12
13
h2 {
14
  font-weight: bold;
15
  font-size: 2rem;
16
}
17
18
p {
19
  font-size: 1rem;
20
  font-weight: normal;
21
}
22
23
ul,li{
24
  list-style:none;
25
}
26
ul{
27
  display: flex;
28
  flex-wrap: wrap;
29
  justify-content: center;
30
}
31
ul li a{
32
  text-decoration:none;
33
  color:#000;
34
  background:#ffc;
35
  display:block;
36
  height:10em;
37
  width:10em;
38
  padding:1em;
39
}
40
ul li{
41
  margin:1em;
42
}

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 want the list items to be side by side. We would have used the float property to achieve this a while back. However, we can now use the much more powerful flexbox layout module. This makes it very easy for us to place elements in a two-dimensional layout.

All we have to do is set the display property to flex on the ul element. This will place all our list items in a single row. The list elements will start to overflow at some point. The best way to avoid that is to use the flex-wrap property and set its value to wrap. Extra elements will now keep moving to the next row instead of overflowing.

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:

Sticky Notes: Basic Yellow BoxesSticky Notes: Basic Yellow BoxesSticky Notes: Basic Yellow Boxes

Flexbox is supported in all major browsers today. This covers around 98.5% of users at the time of writing this tutorial. If you need to support older browsers, you can consider using float to lay out the elements.

Step 2: Drop Shadows and Scribbly Font

Now it's 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 Google Fonts and the fonts they provide us with, called "Reenie Beanie" and "Lato".

Google FontsGoogle FontsGoogle Fonts

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

1
<link href="https://fonts.googleapis.com/css2?family=Reenie+Beanie&display=swap" rel="stylesheet">

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:

1
h2 {
2
  font-weight: bold;
3
  font-size: 2rem;
4
}
5
p {
6
  font-family: 'Reenie Beanie';
7
  font-size: 2rem;
8
}

In order to give the sticky notes a shadow to make them stand out from the page, we need to apply a box-shadow. Most browsers in use today support this property without use of prefixes.

1
ul li a{
2
  text-decoration:none;
3
  color:#000;
4
  background:#ffc;
5
  display:block;
6
  height:10em;
7
  width:10em;
8
  padding:1em;
9
  box-shadow: 5px 5px 7px rgba(33,33,33,.7);
10
}

The box-shadow property sets the offset, spread, and color of the shadow: in this case, a dark grey with an opacity of 70%. Together with the new font, our sticky notes now look like this:

Sticky Notes: Box ShadowSticky Notes: Box ShadowSticky Notes: Box Shadow

Step 3: Tilting the Notes

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. 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:

1
ul li a{
2
  transform: rotate(-6deg);
3
}

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:

1
ul li:nth-child(even) a{
2
  transform:rotate(4deg);
3
  position:relative;
4
  top:5px;
5
}
6
ul li:nth-child(3n) a{
7
  transform:rotate(-3deg);
8
  position:relative;
9
  top:-5px;
10
}
11
ul li:nth-child(5n) a{
12
  transform:rotate(5deg);
13
  position:relative;
14
  top:-10px;
15
}

This tilts every second link four degrees to the right and offsets it 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:

Sticky Notes: Random RotationsSticky Notes: Random RotationsSticky Notes: Random Rotations

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.

1
ul li a:hover,ul li a:focus{
2
  box-shadow:10px 10px 7px rgba(0,0,0,.7);
3
  transform: scale(1.25);
4
  position:relative;
5
  z-index:5;
6
}

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:

Zooming the Sticky NotesZooming the Sticky NotesZooming the Sticky Notes

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:

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

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 color into the mix by making every second sticky note green and every third light blue:

1
ul li:nth-child(even) a{
2
  position:relative;
3
  top:5px;
4
  background:#cfc;
5
}
6
ul li:nth-child(3n) a{
7
  position:relative;
8
  top:-5px;
9
  background:#ccf;
10
}

Your sticky notes should now look like this image:

Stixky Demo: Random ColorsStixky Demo: Random ColorsStixky Demo: Random Colors

Step 6: Making the Sticky Notes Editable

The easiest way to make the sticky notes editable is to use the contenteditable attribute on all the links. This will allow users to click inside the title or the content of the sticky notes and change it.

However, the changes won't stick if the user reloads the page. We can change that by using some JavaScript. Load the latest version of jQuery on the webpage and add the following JavaScript after that.

We will be using localStorage to store the data about our sticky notes. The key will be based on the index of our list item, and its value will be the title and content of the sticky note.

1
all_notes = $("li a");
2
3
all_notes.on("keyup", function () {
4
  note_title = $(this).find("h2").text();
5
  note_content = $(this).find("p").text();
6
7
  item_key = "list_" + $(this).parent().index();
8
9
  data = {
10
    title: note_title,
11
    content: note_content
12
  };
13
14
  window.localStorage.setItem(item_key, JSON.stringify(data));
15
});

The above code attaches a keyup event to all the sticky notes. Whenever a user types something inside the title or content of a sticky note, we get its content using the text() method. This data is stored in the localStorage of the browser by using the index of the sticky note as a key.

Storing the updated list in localStorage serves no purpose if we can't retrieve it and show it to users. The following code will loop through all the list items and check for their corresponding keys in localStorage. If the key exists, we extract our title and content from it and update the HTML of our list item.

1
all_notes.each(function (index) {
2
  data = JSON.parse(window.localStorage.getItem("list_" + index));
3
4
  if (data !== null) {
5
    note_title = data.title;
6
    note_content = data.content;
7
8
    $(this).find("h2").text(note_title);
9
    $(this).find("p").text(note_content);
10
  }
11
});

With this code in place, you should now try to edit any of the sticky items and reload the pages to see if the changes stick. Here is a screenshot of my sticky notes.

Sticky Notes: Saved DataSticky Notes: Saved DataSticky Notes: Saved Data

Here is the complete JavaScript code that you need to use in one place. Please make sure that you have also are also loading jQuery on the page.

1
$(document).ready(function () {
2
  all_notes = $("li a");
3
4
  all_notes.on("keyup", function () {
5
    note_title = $(this).find("h2").text();
6
    note_content = $(this).find("p").text();
7
8
    item_key = "list_" + $(this).parent().index();
9
10
    data = {
11
      title: note_title,
12
      content: note_content
13
    };
14
15
    window.localStorage.setItem(item_key, JSON.stringify(data));
16
  });
17
18
  all_notes.each(function (index) {
19
    data = JSON.parse(window.localStorage.getItem("list_" + index));
20
21
    if (data !== null) {
22
      note_title = data.title;
23
      note_content = data.content;
24
25
      $(this).find("h2").text(note_title);
26
      $(this).find("p").text(note_content);
27
    }
28
  });
29
});

Summary

There you have it—smoothly animating and tilted sticky notes. All 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 means that keyboard users can observe the results as well. We were also able to add some useful functionality to the sticky notes by using some JavaScript.

While you're here, check out some of our other CSS tutorials here on Envato Tuts+.

This post has been updated with contributions from Monty Shokeen. Monty is a full-stack developer who also loves to write tutorials and to learn about new JavaScript libraries.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.