Try Tuts+ Premium, Get Cash Back!
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://www.youtube.com/user/izvarzone AntoxaGray

    Not very practical, however.

    • http://www.jeffrey-way.com Jeffrey Way

      Why?

      • jacked

        Perhaps because a lot of work in css, you can quickly make the icon in photoshop. Weight of the sprite is not that big:)
        Waste of time to create this in css. Wait for a program that will allow us to create icons in CSS like in photoshop:)

  • Barry

    Agree, not very practical, CSS is there for styling… not to create graphics. However, the techniques that have been used are useful for other stuff.

  • Designer

    Why use CSS where it not necessary?

    • Rodrigo

      CSS over images is desired to minimize server traffic. Any connect that you can save by removing the necessity of an image is always a win for your site’s performance. CSS icons are a great alternative and browser support for the features that you need is constantly increasing.

      • rookie

        Hi,

        Won’t the additional weight of this css included as either inline text or as a separate file be comparable to the weight of an image icon ?

        How does it then save traffic over the network ?

      • http://designbyadrian.com Adrian von Gegerfelt

        You get less traffic, but larger CSS files.

        You strain the browser more if you let it render images on itself. Might as well use CSS sprites.

        Nice work, though :)

        PS. A folded corner is not that rounded. Border-radius can actually take 8 parameters. Try adjusting the radius to make it flatter. DS.

  • http://www.ricardofilipe.com Ricardo Magalhães

    What I think is that is a bit pointless to try to reach a conclusion on whether or not this is “practical”.
    It will always be context dependant, maybe you won’t creating CSS based icons for your client’s project anytime soon, sure. But in a personal project of yours, why not?

    This is a nice exercise. Just by looking at the icon I wouldn’t have imagined that it was created using only CSS, and I’m sorry but that’s the kind of stuff I live for in this field of work. To be pleasantly surprised even with minor details, such as icons!

    Chris Coyiers talks about “Aha!” moments, and I live for the “Wait, what?!” moments. “That’s just a PNG file. Wait, what, it’s CSS alone?!” :)

    Thanks for the exercise. I’ll be sure to try and think of some other icons I can recreate using only CSS!

    • http://www.celwinfrenzen.com Celwin Frenzen

      I was just about to write the exact same reply mate. Good point. This tutorial is more about practise (what are the possibilities with CSS3) then function. It’s still easier to create a PNG file to use as an icon, but the fact this can be done in CSS3 is very cool. And a good way to practise with rounded corners and gradients, which IS something I use a lot nowadays.

    • Margalus

      I also second your comment. I mean, you can’t always be sure wether something has a sense or not, and this is just trying to show some techniques to make a great-looking icon.
      Of course i’ll still be doing it in Photoshop but now i’ve added some new tricks to my personal portfolio :)

  • Jose L. Salinas

    For those who say this is not useful, just imagine you’ve got fileype icons for downloadable content (acrobat, ms word, etc). You could think “I would like file-like icons, not only the app icon, but I don’t feel like editing all of them in Photoshop”.

    Why not use this technique? You just have to replace the gradient with the corresponding png image for each filetype you want to use (of course, you could use a default one).

    So you have something like this in a template:

    Download as PDF
    Download as Word

    And you change it to:

    Download as PDF
    Download as Word

  • Jose L. Salinas

    For those who say this is not useful, just imagine you’ve got fileype icons for downloadable content (acrobat, ms word, etc). You could think “I would like file-like icons, not only the app icon, but I don’t feel like editing all of them in Photoshop”.

    Why not use this technique? You just have to replace the gradient with the corresponding png image for each filetype you want to use (of course, you could use a default one).

    So you have something like this in a template:

    <a href=”#”><img src=”filetype_pdf.png” /> Download as PDF</a>
    <a href=”#”><img src=”filetype_doc.png” /> Download as Word</a>

    And you change it to:

    <a class=”docIcon typePdf” href=”#”>Download as PDF</a>
    <a class=”docIcon typeDoc” href=”#”>Download as Word</a>

  • Charbs

    This is pretty cool. But realistically it’s probably easier to just create a png icon. CSS is pretty freaking powerful, so many things can be created with it to match a particular design, but sometimes i’d just cut to the chase and use an icon instead.

    Cool article though, thanks for sharing.

  • Mark Rivera

    Both are good results for us. But remember, use those things wisely.

  • http://www.konto-osobiste.biz konto osobiste

    Cant belive capabilities of this new CSS!

    Its rly has a potential.

  • http://www.inhimak.com Tiffany John

    This is useful for every one. Now you don’t need to make picture based icon and wasting you time. Just get this code and make your code library and use it when ever you need. Amazing!
    Good Work

  • http://www.футболмастер.рф alex

    Thanks for the tutorial. Will help me a lot!

  • http://www.tktlc.com Kyle Lanning

    Practical or not. This is way cool and creative. Just another example of how creative minds push the envelope. Awesome!

  • Prodyot

    Shekhar, this is a beautiful creation.
    I was looking for such a tutorial and I found yours.
    A line or two for the skeptics-
    In school and colleges students are taught math- they are not taught how to mathematically launch a Rocket.
    The same student will one day join space agencies and launch Rockets.
    Just because Rocket Science Math is not taught in schools and colleges we can’t say that the school and college math curriculum are useless.
    You have shown us what we can do with CSS3.
    We need to take your icon example as the motivating factor and try to navigate our ways with CSS3.
    Thanks for the tutorial and thanks for such a beautiful gift.

  • http://www.preduzeca.me Poslovni adresar Crna Gora

    I really like this

  • http://panghaidar.wordpress.com panghaidar

    This tutorial is really cool I will try this in some of my project. Thanks.

  • http://amyself.com Makarand

    Thanks For letting us know the method to create a Icon using css3. I will definitely use it in my future projects

  • Rakesh

    i think this code is very useful to us. Thanks

  • http://www.istudio.com.mx Alfredo Ramirez

    Very surprised of the power of CSS3, looks so innocent but packs a punch!! Thank you!!

  • http://www.web-mate.gr webmate

    nice tutorial:)

  • http://www.html5xcss3.com Kimmy | Html5xcss3.com

    Wooo, Cool Css3, but I like png !!!

  • http://www.synergeio-vrontos.gr Συνεργείο

    Very nice , thanks a lot

  • http://www.stuffmadeby.me stu

    nice but pointless

    a million lines of code which wont work in most browsers, or 10 mins in photoshop and it works everywhere

    • http://oldek.com Marcus

      Oh, gosh, people say pointless, well try a retina display to view your pretty images in the browser becomes ugly… Actually doing it in CSS turns it into a typography kind of. So the more you zoom, the better it looks kind of. It’s like designing logos for a company, and you choose gif as format 10×20 px. The company wouldn’t be happy when printing it on their skyscraper wall. So if you find a use case where the image can be used in several sizes and zoomed in I would say that this is a really cool feature.

      There is a reason why vector graphics became a hit.

      /Marcus

      • http://twitter.com/StuffMadeByMe Stuff Made By Me

        still, doesnt work in ie7, 8, or 9 tho.. so pointless :)

      • http://twitter.com/shekhardesigner Shekhar Sharma

        Awesome reply. Thanks

      • Patrick Joannisse

        Vector graphics is something..using CSS to create pictures is another.

  • Nguyễn hải ninh

    good job! thank for sharing

  • http://www.facebook.com/css.nvsu2009 Cee Es Es

    Nice tuts..i love this..visit me back at http://www.tutgeeks.com