How to Create a Beautiful Icon with CSS3

How to Create a Beautiful Icon with CSS3

Tutorial Details
  • Topic: CSS3
  • Difficulty: Intermediate
  • Estimated Completion Time: 20 minutes

Today, I’d like to show you a neat trick. We’ll create a document icon with pure CSS3. Even better, this effect will only require a single HTML element.


The Game Plan

  1. Create a square box
  2. Round the edges
  3. Use pseudo elements to create a curled corner
  4. Create the illusion of text with a striped gradient

Let’s get started!


Step 1: Create a Box

We’ll begin by adding our single HTML element: an anchor tag. This makes sense, as most icons also serve to be links.

    <a class="docIcon" href="#">Document Icon</a>

Let’s set the somewhat arbitrary dimensions for our icon. We’ll do 40x56px – simply for this demo. In a real world application, you’ll likely want to reduce this! Also, keep in mind that we need to add display: block, since all anchor tags are inline, by default.

.docIcon
{
    background:#eee;
    background: linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
    border:1px solid #ccc;
    display:block;
    width:40px;
    height:56px;
    position:relative;
    margin:42px auto;
}

Note that, above, we’re setting a positioning context in order to work with pseudo elements shortly. You’ll find that I’ve only used the official CSS3 syntax for the gradient. You’ll likely want to use the various browser prefixes as well. To speed things up, you can use Prefixr.com, or its API in your favorite code editor. Simply copy the code snippet above, paste it into Prefixr’s textarea, and click enter. It’ll then spit out all of the various prefixized properties, like so:

.docIcon {
	background: #eee;

	background: -webkit-linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
	background: -moz-linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
	background: -o-linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
	background: -ms-linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);
	background: linear-gradient(top, #ddd 0, #eee 15%, #fff 40%, #fff 70%, #eee 100%);

	border: 1px solid #ccc;
	display: block;
	width: 40px;
	height: 56px;
	position: relative;
	margin: 42px auto;
}
Prefixr

Next, let’s add some shine using CSS box shadows. I’ve also used the text-indent property to hide the text.

.docIcon
{
   ...
    -webkit-box-shadow:inset rgba(255,255,255,0.8) 0 1px 1px;
    -moz-box-shadow:inset rgba(255,255,255,0.8) 0 1px 1px;
    box-shadow:inset rgba(255,255,255,0.8) 0 1px 1px;

    text-indent:-9999em;
}

So Far, We Have:


Step 2: Rounded Corners

Next, we need to create a rounded corner effect. Add the following:

.docIcon
{
    ...
    -webkit-border-radius:3px 15px 3px 3px;
    -moz-border-radius:3px 15px 3px 3px;
    border-radius:3px 15px 3px 3px;
}

By passing four values, we can specify the top, right, bottom, and left radii, accordingly. This is similar to the way you would apply margins or padding.

Which Gives Us…


Step 3: One Curled Corner

To create the illusion of a curled corner, we’ll use generated content, or pseudo elements.

First, add content :before our icon. In this case, we don’t require any specific text. Instead, we need to create a 15px box, and apply a background gradient.

.docIcon:before {
	content: "";
	display: block;
	position: absolute;
	top: 0;
	right: 0;
	width: 15px;
	height: 15px;
	background: #ccc;

	background: -webkit-linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);
	background: -moz-linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);
	background: -o-linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);
	background: -ms-linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);
	background: linear-gradient(45deg, #fff 0, #eee 50%, #ccc 100%);

	-webkit-box-shadow: rgba(0,0,0,0.05) -1px 1px 1px, inset white 0 0 1px;
	-moz-box-shadow: rgba(0,0,0,0.05) -1px 1px 1px, inset white 0 0 1px;
	box-shadow: rgba(0,0,0,0.05) -1px 1px 1px, inset white 0 0 1px;

	border-bottom: 1px solid #ccc;
	border-left: 1px solid #ccc;
}

In order for our generated content to also receive the top-right rounded edge, we must, again, apply the same radii to align it.

...
-webkit-border-radius:3px 15px 3px 3px;
-moz-border-radius:3px 15px 3px 3px;
border-radius:3px 15px 3px 3px;

Tada!


Step 4: Adding Lines

Next, we’re going to use the :after psuedo element to add some dashed lines to represent zoomed out text. Apply a width of 60%, and a margin-left and margin-right of 20% (which equals 100%). Next, we specify a height and position it at 0 0.

.docIcon:after
{
    content:"";
    display:block;
    position:absolute;
    left:0;
    top:0;
    width:60%;
    margin:22px 20% 0;
    height:15px;
}

Creating a set of lines is a bit tricky. If we’re clever, though, we can use CSS gradients to achieve this effect. First, divide the total height by five, and set each block with a solid fill. Refer to the image below for a clearer exemplification of this thinking. Nifty, ay? It’s a nice technique to have in your toolbelt.

Multiple Lines (Stripes) with CSS3 Gradienst

.docIcon:after
{
    ...
    background:#ccc;
    background: -webkit-linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
    background: -moz-linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
    background: -o-linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
    background: -ms-linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
    background:linear-gradient(top, #ccc 0, #ccc 20%, #fff 20%, #fff 40%, #ccc 40%, #ccc 60%, #fff 60%, #fff 80%, #ccc 80%, #ccc 100%);
}

We’re Finished!

Did you enjoy the post? Have other similar tricks? If so, link to them in the comments below.

Tags: css3
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://twitter.com/drale2k Drazen Mokic

    Pretty clever!

  • http://amosrivera. arivera.utpmp@gmail.com

    Clever but if i had a penny for every time i will actually use this i would have less than a penny.

    • http://michaeltunnell Michael Tunnell

      lol, agreed. I really like this idea and the article is very thorough but I don’t really see myself using this when it takes so many lines in css and doesn’t work in all browsers…Hopefully when IE 32 comes out we can use stuff like this.

      • http://www.fahdmurtaza.com Fahd

        lolz, true that. IE 32 might have some problems resolved.

  • http://blogsh.de Sebastian Hörl

    This is quite impressive and a good example of what can be done with css! :)

  • Victor Rodrigues

    Estimated Time to Code an Icon: 20 minutes
    Estimated Time to Crop a PNG: 1 minute

  • http://www.croftmedia.co.uk/ Greg

    That’s pretty cool! I’m interested to know what people think about CSS icons. They’re not supported by older browsers, but they require the visitor to handle less files (since it’s -1 image file and CSS is already inclcuded). But, yeah, do people think CSS icons are a good idea or just gimmicky?

    • Tyler

      As much as I really really want to like them, I’d say they are too gimmicky. I love what CSS3 will allow us as web designers to do with front ends. I also don’t feel as though too many images are going to be a problem, computer specs increase every year, and every 5 years or so a new “standard” is implemented. I really like the idea’s presented here, I just feel that they aren’t necessary.

    • http://www.youtube.com/user/izvarzone AntoxaGray

      I see only one reason to use it: if you need to animate colours or size of icon dynamically, otherwise it’s not practical to use such a big code, when you can make it faster in Photoshop.

  • Tony

    I don’t know how long this takes, but I could do this is PS in like 3-5 min. Seems counter-productive. What are the benefits of this method?

  • Alistair

    Yehhhhh pretty clever indeed.

    But why?

  • http://www.geektopia.me Shaun Dunne

    Nice. Now I’ll begin looking at all Icons and wondering which ones I can achieve using CSS.

  • http://blog.donwilson.net Don Wilson

    Nice tutorial. However, the rounded corner really looks weird to me. Somehow, you managed to fold that paper in a complicated way to give it a round fold!

  • Jesse

    Why? Because it could be done, that’s why. Not every demo, tutorial has to have practical application or even be a better alternative. Sometimes, they can just show off what *can* be done; a way to illustrate the possibilities and get you try new techniques.

    • Alistair

      Well that’s a demo then and not a tutorial.

      The tutorial came with this but it was ultimately about the demo and not the tutorial.

      We’ve seen how to accomplish this here on Nettuts with a plethora of CSS3 tutorials. A practical use is always good, but this isn’t very practical and most articles here on Tuts+ usually come with a practical example.

      Redundant springs to mind, but it’s nobodies fault. Guess it’s one of those ones that’s simply here to raise awareness.

      Good demo, tut for me and most above not required but newbies maybe.

      Thanks for sharing.

    • http://www.cssjunction.com Shekhar Sharma
      Author

      Jesse,

      Think about low bandwidth mobile web apps, its much more efficient to product graphics like this with CSS3 rather using PNG/GIF, saving page load time is something we must consider & this is where CSS3 comes in hand.

      • http://jared.com Jared

        Consider data urls.

  • http://www.mediaty.com Martin

    I think I’ll using the pictures, this is unnecessary

  • http://www.cyberstream.us/blog Eli Mitchell

    The tutorial is written in cross-browser code, but the demo isn’t.

  • http://about.me/ber79 Ber79

    Love it.

  • aleks

    Nice Tutorial …The Power of CSS3 shown here….Maybe the file icons is useless with code but…all techniques can be used for other elements …like a menu and so……

  • Sleeman

    Greeting Net tuts :
    First thanks for all the great tutorials you present so far .
    Second where is Jeffery !! we miss your tutorials man , because there is too many new things in CSS 3 animation and JQuery that we need you to teach us , of course with all respect to all the staff of net tuts.

  • http://www.codedninja.com Michael Pivonka

    It’s a good asset to know about. Think of it this way… Do css for icon and save one less image for newer browser but you can have an image as a fall back thus speeding up page by a little bit. Any time saved is good.

  • mar

    jpg or png isn’t enought ?

    only for idea its ok … but for use totaly stupid idea ….

    • Jordan

      How so? It takes less physical space (weighs less) and it uses fewer HTTP requests. It’s only supported by modern browsers, which is the drawback, but that’s entirely dependent on use, and need.

  • http://www.calipo.eu ecoue

    Hey :) Nice tut

    I think it is absolutely worth the effort because if your take the raw CSS code and compress it as much as possible it only takes around 0.9 kByte of traffic no matter which size the icon has!
    If you build a png file the size is a lot higher – around 3.3kByte.
    Assume you have millions of page request per day that is a LOT of traffic you could save this way!

    Greetings eCoue

    • http://www.calipo.eu ecoue

      It would save aroung 11,4 GB every 5 million requests :)

  • kankuro

    nice tuts…..

  • Brad

    Thanks for showing this technique!

  • takeFlight

    I think this is great! Perhaps there are better methods and ways. Sure an image may be faster and more reliable…. but it’s also not as cool. Thanks Shekhar; you’re making the web a better place. (and that’s not a waste of time)

  • Elegant Rao

    Mmmmagnificent. good job.

  • ido

    Brilliant :) thank you

  • nXqd

    Very nice article :)

  • http://www.jeffadams.co.uk Jeff Adams

    I think this tutorial is getting some unfair feedback – sure it’s way, way easier to use some of the amazing icons there are available and to be perfectly honest some of them will likley be smaller in size than the finished CSS files after doing all this!

    But the point of the tutorial (I think!) was to demonstraight CSS3 features like gradients etc. and it’s a good way to show these in action so for that – thanks!

  • http://www.gasoline.nl Marc

    Nice tutorial, but it WILL NOT speedup your website. I think the PNG is SMALLER than all the code. And it takes far more time to code this.

  • http://www.gasoline.nl Marc

    Nice tutorial, but it WILL NOT speedup your website. I think the PNG is SMALLER than all the code. And it takes far more time to code this.

    If tried it, a good transparant PNG 32Bit = 396 bytes
    The code is approxamly = 1.900 bytes

    So the code is almost 5 times SLOWER.

    However I love your website, great info and tutorials.

  • Mike Mellor

    This is really clever, would be pretty cool to increase the attributes applied to the :before pseudo element on hover of the link to animte the page curl.

  • http://www.nightocoder.com Rawaf

    Perfect article, like it :)

  • http://simonwjackson.com Simon W. Jackson

    Wow, this is really sweet! I can see the benefit over static images too. Potentially, you could even animate this with javascript or CSS3 animations. I bet this method takes up a bit more bandwidth though.

    Great post!

  • Benjo

    I guess I am more inclined to agree with ecoue’s point of view, however I understand why other web dev/designers might question this method. Before anyone says anything negative though, think about how much page traffic you encounter, how many server requests occur on a daily basis, etc. This might benefit someone in an enterprise/university/high traffic environment IMO. Just my two cents.

  • http://www.pdvictor.com Peter Drinnan

    I love what CSS3 is capable of, but like so many other people posting comments here, where and why comes into question. CSS3 is still a sci-fi concept for a lot of us because IE will not allow us to really use it. Microsoft, please give up on IE already! We know IE did a lot of good things in it’s day but it isn’t really helping anymore.

  • John

    Please don’t cheat ‘How to abuse CSS3′ :)

    • John

      teach*

  • http://mustafanamoglu.com/ Mustafa Namoğlu

    Just amazing!

  • bill

    Fails in IE8.

  • http://atinder.com atinder

    wow creative :)

  • http://iamcreative.me Aaron @ iamcreative.me

    Bravo,
    You have given me some real food for thought here Shekhar, well done!

    All the best idea are brilliantly simple!
    Something I have not thought about doing… until now.

  • http://www.callthetricks.com Sudeep Acharya

    Great i will try it

  • http://mokshasolutions.com Moksha

    amazing work really great stuff

    thanks

  • Jarek

    Great work! I wonder that dimensions could be in em’s instead of pixels, so it can be resizable by defining font-size

  • http://twitter.com/#!/notomorrow9 notomorrow9

    great article! i love it and want more )))

  • GorillaWma

    Nice one, well I try something a while ago and here is the result: http://thlclan.com/css3icons/. I made a lot of them and here is magnifier :) I just like CSS3 :D

  • Marcio Almada

    That’s sweet. Sometimes that will-not-work-with-IE thinking can be a cage to our creative minds.

  • Selcuk

    Great article but still takes too many lines to code.
    I would just use a simple image :)

  • http://iuliandita.com Iulian

    Nice coding, but not practical. For now, at least. Check out various sizes:
    http://i52.tinypic.com/28laz68.png

  • http://www.autson.com Sharif Hamdy

    Autson is an outstanding name in the field of web design, web development, and experts in Internet marketing. We are providing innovative and creative web development and designing solutions which ensure your strong online business presence as well as gave you a way to expand your business.

  • marjo

    thanks!

  • zet

    would be better/faster using the svg format…

  • http://thedevelopertuts.com Bratu Sebastian

    Great tutorial!

    The ideea is great, I agree, not many developers would do this on a client website, but when you do it for yourself it’s worth it.

    And nobody saw the advantage: you can always scale this icon, it will never look bad ( even if the css is in pixels )

  • http://www.howtomake.com.ua/ HTM

    Yo! Great lesson! I translated it into Russian!

    How to Create a Beautiful Icon with CSS3 on Russian!

    My wife liked this trick, wants his lessons on such icons:)