05 Dec 2014
Google's transforming material icons in HTML and CSS
This week I am deconstructing the Delightful Details from Google’s Material Design. It’s not built in HTML and CSS but that’s not important. What’s important is that we can.
This week I am deconstructing the Delightful Details from Google’s Material Design.
It’s not built in HTML and CSS but that’s not important. What’s important is that we can. If you haven’t read Google’s Material Design Spec I highly recommend it. It’s a well crafted design spec with a lot of good information. I have followed the different android specs over the years but this one excites me as it includes information for larger screens.
What is it?
This is the finished clone of the Delightful Details. Note that it isn’t exactly the same but it proves that it can be done in HTML and CSS. You click on each section to toggle between icons.
Now let’s break it down.
This is just a square grid cut in four with different colours. My colours differ slightly as I chose to use the 500 strength colours from the material colour palette.
Here’s the CSS for the grid, the Roboto font will come in handy later. The
user-select will allow us to click the elements without selecting them.
Here’s another nice effect for clicking the grid section. My inspiration comes from the material design spec again; it adds a circular active effect on click. Try clicking any of the grids on this page (you will need to hold it for a bit).
To recreate the icons in the grids I have chosen to use a div with three span tags. With these elements I will create the following icons:
- The infamous hamburger icon
- A back arrow
- The animated loader (a failed attempt)
- +1 Icon
- +2 Icon
By creating all of these with the same elements I can ensure that I can smoothly transition between any of them. Each one having it’s own class to add to the
.material-icon element I will break them down in the order listed above (also the order I built them).
Here is the base CSS used to construct my icons:
The infamous hamburger icon
The hamburger is an on going debate in the UX community but I leave that for another day. All I need to do for this icon is create three horizontal bars of equal height. I will move the second span to the middle and the third to the bottom.
Now you may be asking, why not use absolute positioning here? By using transforms we get some fantastic performance gains as well as sub-pixel positioning on our transitions. Paul Irish breaks this down really well in his article on absolute positioning vs translate.
A back arrow
I will build the back arrow to be an arrow facing right, then rotate the whole container 180 degrees. This will give we the same effect as the video and allow me to focus each animation separately. The first span is exactly the same so I will combine that selector. The second and third are rotated 45 degrees and need to be moved into place. You can click this one to see the animation.
That’s the green section all done. Let’s move on to the amber section.
The pause is just as easy as the hamburger; It’s just two vertical bars. I decided to rotate it 90deg and have two horizontal bars for the animation to play.
This one took me a while to think about. You can probably imagine me trying to stick three rectangles together to create a triangle. Hmmm… I’ll come back to this one after stop.
This one was obvious everyone who knows CSS knows how to create a coloured square. I did however use two half blocks so the animation to pause would look like it’s splitting from the middle.
After doing stop and looking at the original animation closely I realised that I needed something else to achieve this. I employed the same technique from my Facebook content placeholder post to mask the icon into a triangle shape.
Here’s my mask, no extra elements required. I default it to sitting on top and bottom so my earlier icons are not effected.
Here’s the additional CSS for the play icon. It rotates the container so that it has the animation on change and rotates our masks into place to create a triangle.
That’s the amber section all done. Let’s move on to the blue loader section.
The animated loader a failed attempt
I’m going to be honest, I was on a bit of a high getting the last two parts done. The loader did not seem like a huge leap as I could use all the same tactics and pair it with an infinite CSS animation. As you can see I gave up getting the animations anything close to perfect:
I will break each step down anyway. Since releasing this post, a good article using SVG and CSS animations has popped up to implement the loader.
Here are the steps I took to recreate the loader. To get the general shape I created a div with border on three sides (1st version) then added a border radius (2nd version). The third step was to add a constant rotation to the outer element (3rd version). The next image is a bit weird on it’s own; it rotates one of the spans and while adding and removing one of the borders. This image paired with another span rotating behind it gives the growing and shrinking look.
If I were to build it again I would have had a full circle in white and used the masks from before. The masks would use the same technique to grow and shrink. This would allow the white segment to get much smaller like in the video. This was not possible in the previous version as the minimum size was set by one of the edges.
+1 and +2 Icon
I was finally up to my last segment and I was cut down by the mighty loader. I decided to start with the plus. Very similar to techniques before but I overlap them instead of putting them parallel. For the +2 icon I can rotate the two elements of the plus 90 degrees.
For the number, I mentioned earlier that the Roboto font will come in handy. I figured the easiest way to add the numbers was to make the content of the masks “1” and “2”. Then all I need to do is position the masks to the right of the plus.
Once I added the CSS to position it I noticed a really nice effect of the masks fading back to their original positions. That wasn’t an intended effect but I liked it so much I kept it like that. It feels like the plus is a level and it is pulling the numbers in and out.
Why would I ever use this?
Android used to have a rule, maybe they still do, that for every negative experience a user has there must be three positive reactions to make up for it. These are just a few examples that will truly delight a user and these are the things they remember. Using things like this will show the user how the interface is changing rather relying on education.
This Deconstruction took me a bit longer than expected which is why there wasn’t one last week. I hope to use some of these techniques soon. I am thinking of writing a tutorial to demonstrate custom controls on HTML5 videos which will feature the play, pause and stop. Definitely check out Google’s Material Design Spec, it’s full of great designs and guidelines to think about. If you have any questions or you fixed my loading symbol add a comment below.