Create and Code Your Own Juicy Tag Cloud!

Create and Code Your Own Juicy Tag Cloud!

Tutorial Details
  • Program: Adobe Photoshop
  • Difficulty: Beginner
  • Estimated Time: 30 minutes

Final Product What You'll Be Creating

Today we’re firing it up with a tutorial on how to design and code your own juicy tag cloud using Photoshop, CSS, and a few tiny images! We’ll be using the age-old sliding doors technique, but we’ll include some advanced CSS selector methods towards the end in case you want to push the envelope and use this inside WordPress. Ready, set, go!


The Written Tutorial

A little bit of polish in the right spots can really spice up an otherwise standard page design. Today, we’re going to walk through how to create your own custom variation of the “tag cloud” element that you can find in pretty much any blogging system nowadays.

We’ll quickly skim through the design (it’s easy peezy), and then dig into the CSS that makes this all come together. Use it in your own projects, drop it into a personal blog, it’s totally up to you… let’s dive in!


Step 01: Design The Tags

The CSS purists in the audience can cringe at this point because we’re going to open up Photoshop. Why? Because we want to create a completely custom tag design. Can this particular tag that we’ll be using today be achieved in CSS? Probably – I’m sure some CSS ninjas out there will be quick to point out that these are just basic shapes… but what if you wanted to add texture? Or a completely different shape (scalloped edges anyone)?

Aside from the browser headaches of trying to get this working with CSS alone, the bottom line is that it’s good to know how to use images and CSS together in unison, so that’s precisely what we’ll be doing.

Go ahead and fire up Photoshop. Open a new document sized 71px by 29px.

The width doesn’t actually matter much here… but the height is going to be something you’ll want to remember.

Next, let’s draw a rounded rectangle on our document with a 2px radius. Leave a bit of room for a drop shadow later on… in our case, I’ve left 2px at the bottom and sides.

Now it’s time to create our custom shape. We’ll be using Photoshop’s Add Point and Convert Point tools. There’s not much to this beyond just tinkering with the points until you’ve got the shape that you want, so play around a bit. Here’s ours:

We need a little hole in our tag though… you know, so people can loop a virtual string through it. Ok, maybe not, but it adds a little extra flare for us that wouldn’t be possible with CSS. Use the elliptical marquee tool and draw a circular selection (hold the Shift key while you draw to keep it perfect).

Once you have your selection made, select the inverse of the selection (Select > Inverse) and turn it into a vector mask over your tag shape using the Add Layer Mask button at the bottom of the layer inspector.

For the sake of brevity (we do want to get to the coding part, right?!), let’s skim through the layer styles here. You can add your own adjustments, but here’s what ours look like:

The end result should look like this:

You’ll notice in the PSD (inside the Download folder for this tutorial) that I’ve also added in our text. Feel free to do the same in case you need to use this inside any design mockups.

Note: You can reverse the orientation of the tag easily on your own. We’ll actually show you how to create the left-facing and right-facing tag in the coding phase.


Step 02: Slicing and Markup

Woot! So we have our tag designed… but how are we going to use it in an actual web page? We’ll start by slicing up our tag into three pieces:

  1. The left side
  2. The center gradient
  3. The right side

Save these all as PNG graphics to preserve the transparency and store them inside a folder called “images”.

Now we need to create our basic markup. We’re going to start this out using a simple, logical approach using layers of DIVs, but as with all things code-related, we’ll be able to clean this up (and reduce the amount of code required) in the later steps.

Here’s our basic markup for each tag:

	<div class="tag tag-left">
		<div class="left"></div>
		<div class="center"><a href="#">Design</a></div>
		<div class="right"></div>
	</div>

Let’s review this quickly: We essentially have a main tag wrapper DIV (.tag), with three nested DIVs:

  • Left DIV (for our left-side graphic)
  • Center DIV (for our tag text and background gradient)
  • Right DIV (for our right-side graphic)

You can go ahead and repeat that block of code a few times to test out multiple tags stacked up next to each other.

Now it’s time to add our CSS.


Step 03: The Basic CSS

We’ll start the CSS by applying some basic rules to each of our divs. I’ll show you the code for each, then explain what it’s doing:

.tag {
	font-size: 11pt;
	text-shadow: 0 1px 1px rgba(255, 255, 255, 0.4);
	float: left;  /* Makes each tag stay together in one piece */
	margin: 5px;
}
.tag .center{
    background: url("images/gradient.png") repeat-x scroll left top transparent;
    float: left;
    height: 29px;  /* Same as your image height */
    width: auto;
    padding: 0px 6px;  /* Give the text a little room to breathe */
}
.tag .center a{
	vertical-align: middle; /* Important for fitting the text in perfectly */
	line-height: 21pt; /* Important for fitting the text in perfectly */
	color: #0f2d39;
	text-decoration: none;
}

This block of code sets up the element that will wrap around our tag; Note that we’re adding some type rules here, a simple float, and a margin between each tag to space them out.

The second two rules in the above CSS cover the center portion of the tag – establishing the background gradient and some spacing.

At this point, you should have some tags that look like this:

Now let’s look add some styling for the left and right side elements.

.tag-left .left{
    background: url("images/tagleft_left.png") no-repeat scroll left top transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 15px;  /* Same as your image width */
}
.tag-left .right{
    background: url("images/tagleft_right.png") no-repeat scroll right top transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 4px;  /* Same as your image width */
}

The above block of code does three primary things:

  1. Targets the background images.
  2. Determines the height/width of the left and right sides of the tag. This should match the size of your images ;)
  3. Tells the elements to float, which makes them line up properly.

Now your tags should be pretty close to complete:

But what if we want to include another type of tag… say one that faces the opposite direction? That’s where the “tag-left” and “tag-right” selectors come into play. By adding these at the top level, we can actually add just a few more rules to our CSS to allow for a completely different tag:

.tag-right .left{
    background: url("images/tagright_left.png") no-repeat scroll left top transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 4px;  /* Same as your image width */
}
.tag-right .right{
    background: url("images/tagright_right.png") no-repeat scroll right top transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 16px;  /* Same as your image width */
}

Which should give you the proper “right-tag” style:


Notice the right-facing tag on the top right of the tag cloud.

Done! Well, sorta… to be honest, this is a really heavy handed way of handling this effect. In the next step, we’ll take you into some more advanced CSS selectors for a more graceful approach.

Google Fonts Note: You may have noticed that I’m using a custom font here (Droid Sans). Adding your own custom font is easy, just visit the Google Fonts site, find the font that you like, and add in the embed code and CSS rule for the font.

The great thing about the Google fonts (or anything like it) is that you can actually apply most CSS font styling to the text… this means that we can use attributes like text-shadow to create the subtle light effect.


Step 04: The Advanced CSS

Anyone who’s been coding for any length of time will tell you there is ALWAYS a way to improve a piece of code. That’s exactly what we’ll be doing in this last step.

Let’s start by reviewing what’s ungraceful about the previous approach.

  • Too much markup! We shouldn’t need to use four different DIV elements to achieve this effect.
  • Heavy handed CSS! It works, sure, but there are more graceful ways of writing the CSS so that it doesn’t actually need the extra markup.
  • It’s not flexible! Requiring lots of markup and gnarly CSS means that it won’t work (easily) within a CMS like WordPress

Here’s another approach using CSS :before and :after selectors to lighten up the load.

Let’s start by looking at the original CSS in it’s entirety, and then re-write it using our selectors:

The Original CSS

/* Generic Tag Styling / Typography +++++++++++++++++++++++++++++++++++++++++++++++++++ */
.tag {
	font-size: 11pt;
	text-shadow: 0 1px 1px rgba(255, 255, 255, 0.4);
	float: left;  /* Makes each tag stay together in one piece */
	margin: 5px;
}
.tag .center{
    background: url("images/gradient.png") repeat-x scroll left top transparent;
    float: left;
    height: 29px;  /* Same as your image height */
    width: auto;
    padding: 0px 6px;  /* Give the text a little room to breathe */
}
.tag .center a{
	vertical-align: middle; /* Important for fitting the text in perfectly */
	line-height: 21pt; /* Important for fitting the text in perfectly */
	color: #0f2d39;
	text-decoration: none;
}
/* Left-tag Rules ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
.tag-left .left{
    background: url("images/tagleft_left.png") no-repeat scroll left top transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 15px;  /* Same as your image width */
}
.tag-left .right{
    background: url("images/tagleft_right.png") no-repeat scroll right top transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 4px;  /* Same as your image width */
}
/* Right-tag Rules +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
.tag-right .left{
    background: url("images/tagright_left.png") no-repeat scroll left top transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 4px;  /* Same as your image width */
}
.tag-right .right{
    background: url("images/tagright_right.png") no-repeat scroll right top transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 16px;  /* Same as your image width */
}

The New CSS

/* Generic Tag Styling / Typography +++++++++++++++++++++++++++++++++++++++++++++++++++ */
.tag {
	float: left;
	margin: 5px;
    height: 29px;  /* Same as your image height */
    width: auto;
}
.tag a{
    background: url("images/gradient.png") repeat-x scroll left top transparent;
    float: left;
    height: 29px;
    padding: 5px 6px;
    color: #0F2D39;
    font-size: 11pt;
    text-decoration: none;
    text-shadow: 0 1px 1px rgba(255, 255, 255, 0.4);
}
.tag:before{
	content: '';
    background: url("images/tagleft_left.png") no-repeat scroll 0 0 transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 15px;  /* Same as your image width */
}
.tag:after{
	content: '';
    background: url("images/tagleft_right.png") no-repeat scroll 0 0 transparent;
    display: block;
    float: right;
    height: 29px;  /* Same as your image height */
    width: 4px;  /* Same as your image width */
}
/* Flip-tag Rules +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
.tag.flip:before{
	content: '';
    background: url("images/tagright_left.png") no-repeat scroll left top transparent;
    display: block;
    float: left;
    height: 29px;  /* Same as your image height */
    width: 4px;  /* Same as your image width */
}
.tag.flip:after{
	content: '';
    background: url("images/tagright_right.png") no-repeat scroll right top transparent;
    display: block;
    float: right;
    height: 29px;  /* Same as your image height */
    width: 16px;  /* Same as your image width */
}

What’s different? For starters, we’ve eliminated the need for the left and right elements inside the DIV.

Here, just compare the old markup and the new markup:

Old HTML Markup

	<div class="tag tag-left">
		<div class="left"></div>
		<div class="center"><a href="#">Design</a></div>
		<div class="right"></div>
	</div>

New HTML Markup

	<span class="tag">
		<a href="#">Design</a>
	</span>

By using the :before and :after selectors, we’re able to effective inject our “sliding doors” into the markup without any extra load. The result is a much cleaner piece of markup, and one that actually works on all modern browsers! We’ve also switched from using DIV elements to simple SPAN elements (SPANs make a bit more sense in this context).


Bonus Step 01: Adding the :Hover State

So what if we want to have a hover state for our tags? It’s pretty simply actually – just add a couple new rules with the :hover selector inserted in between our class-selector and the :before and :after selectors.

For instance:

.tag:hover:before{}

That handles the selection problem – from here you can basically add your own variation of what happens upon the mouse-hover. You can do anything from change the text color, to the opacity, or even change the actual background graphics. Here’s what we used in the demo:

/* Mouse Hover Rules +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
.tag a:hover{
    background: url("images/gradient_hover.png") repeat-x scroll left top transparent;
}
.tag:hover:before{
    background: url("images/tagleft_left_hover.png") no-repeat scroll 0 0 transparent;
}
.tag:hover:after{
    background: url("images/tagleft_right_hover.png") no-repeat scroll 0 0 transparent;
}
.tag.flip:hover:before{
    background: url("images/tagright_left_hover.png") no-repeat scroll left top transparent;
}
.tag.flip:hover:after{
    background: url("images/tagright_right_hover.png") no-repeat scroll right top transparent;
}

Note that swapping out the image can create a quick flicker effect the first time the images load up… there are ways of dealing with this, but that’s another tutorial ;)


Bonus Step 02: Using it Inside WordPress

Using this little trick inside WordPress is also pretty easy. Check out the WordPress Codex page on “the_tags” function that you would ordinarily use inside one of the template files… we’ll be looking to see how we can change the markup that’s used to spit out the tags.

Here’s one example of how you might turn the rendered tags into an unordered list:

<?php the_tags('<ul><li>','</li><li>','</li></ul>'); ?>

We would simply change that to match our own system:

<?php the_tags('<span class="tag">','</span><span class="tag">','</span>'); ?>

That’s it! Just include the associated CSS inside your style.css file (or whichever .css file you’re using in your theme), and you should have some awesome little custom tags ready to go!

Thanks for following along everyone :) Comments on how we can improve this or do it different are always welcome!

Brandon Jones is MDNW on Themeforest
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Karyl

    First! Hehe!…Just kidding.

    Great work. I was just working on some UI elements–I could really use the Photoshop part of this tut.

    Thanks.

    • http://fuseloop.com Dave Thompson

      Totally, Photoshop is immensely helpful even with the tiniest details of UI work. For more UI stuff, you should check out Fuseloop (http://fuseloop.com)

  • Igor Everydaycake

    Just finished working on a mock-up containing those very elements and was wondering how to code them efficiently! Thanks!

  • 2eux

    nice share, hmm, when i’m using chrome , when i hit the sharp with hole in it , they just highlight in that area, without covering all button .. any idea why?

    • Lukas

      Same here. That’s because the hover is set 2 (actually 3, but I count :before and :after as 1) times, so when you hover only :before or :after, only those will be highlighted and not the a-element itself.

      • Patrick

        Its better to use span-elements inside an a-tag, all of them with a “display: inline-block” rule. Should be valid and working in all browsers above IE7.

        Or?

        And Lukas is right, please use css-sprites. Everything else is oldschool.

      • http://themeforest.net/user/epicera/portfolio?ref=epicera Brandon Jones
        Author

        Thanks for the ideas Lukas – I think what I’ll do is a followup “advanced part 2″ version of this tutorial where we’ll look at using jQuery + Sprites as the best possible solution. Like I mentioned in the article, there’s ALWAYS a way to improve a piece of code, and often it takes someone else to look at it to see an even better way :)

  • http://ashish.bogawat.com Ashish Bogawat

    Thanks for demonstrating both those methods, Brandon. It is worth mentioning that IE7 does not support the :before & :after pseudo classes, so those who need universal compatibility should be careful using the method.

    Also, the bug 2eux mentioned is true in all browsers.

  • http://www.adipurdila.com AdiPurdila

    Nice one Brandon, love how they look :D

  • http://www.webstudions.be/ Nick Scheynen

    Hey awesome tutorial! I especially love the short CSS, looks very handy, but will it work in IE7 ?

  • http://www.umitkaraosmanoglu.com/blog/ Umit

    Really amazing design and code! Thanks for sharing it…

  • Pkav

    A jQuery solution might be best for the hover state, i notice a strange effect when i hover the left & right elements. I wonder if it might be possible to hover the parent element and then use a mask or fake mask effect on the children. Other than that great job!

  • ronka

    the Advanced CSS is supported by all the browsers?

    • http://www.umbraprojekt.pl mingos

      Yes, it’s supported by all browsers, just not all versions ;). It will work flawlessly with anything except IE7 and earlier. Which are a blight anyway, so no big surprise.

  • Lukas

    Nice tags! I would use CSS Sprites though, since the hover images now still need to load before you can see they’re highlighted..

    • http://themeforest.net/user/epicera/portfolio?ref=epicera Brandon Jones
      Author

      Hi Lukas, That’s actually not a bad idea at all – we already have a couple Sprite tutorials coming up in the next week or two, but I don’t see any reason why I can’t extend this tutorial to include sprites as well… thanks for the comment :)

  • she-codes

    I would also prefer the whole tag to be clickable. Right now the left and the right images are not part of the link. For me a tag is a whole button, rather than background for the link.

    No offense, this is just me, and sometimes I am too picky. I really enjoyed the article.

    • Musukwa Simposya

      I agree with she-codes. The whole tag should be clickable.

      • http://themeforest.net/user/epicera/portfolio?ref=epicera Brandon Jones
        Author

        Thanks Musukwa – I’ll be covering this in the followup post for this later in the week :) Thanks for all of the comments everyone!

  • Pingback: Egonomik » Web sitenizde kullanabileceğiniz hoş bir etiket (tag) tasarımı

  • http://cmtstudio.com CMT Studio

    I think an “active” state is very necessary for those buttons (and all kind of links, in general). It improves the usability a lot by providing visual feedback.

    At the same time the “active” state is very underestimated (and overlooked) feature by many web designers, probably because of lack of information, or the “client doesn’t pay enough for such small details” syndrome.

    Common “active” techniques include darkening the button / reversing the gradient (http://soundcloud.com/) , or “position:relative; top:1px” (http://365psd.com/).

  • the shortened version doesn’t work well in ie

  • http://www.roundvision.nl Tim

    Hi Brandon,

    Thanks for sharing this tag cloud. Ill hope you will find a nice way in your followup post that fixes the hover state for the whole button.

    Screenshot what happens now when you hovering left or right on the button:
    http://imageshack.us/f/822/schermafbeelding2011061.png/

    Btw, i like the looks of the button.

  • http://www.3rddesign.com 3rddesign

    Thanks for this tutorial. I’ve been looking for this kind of tutorial how to make a tag cloud. I am excited to start this tutorial and hope I could get it right.

  • Pingback: Great List of Articles from June 2011

  • http://www.hiseamarine.com hiseamarine

    slicing up the tag into three pieces, just what i need.

  • lee

    Thank you!
    but how can i use id in the tags of widgets of wordpress, in the function? it has only ‘a’ mark, not span, thanks.

    • lee

      oh ,i am sorry ,the function if !function_exists( ‘dynamic_sidebar’ ) || !dynamic_sidebar( “sidebar” )):

  • Pingback: Web sitenizde kullanabileceğiniz hoş bir etiket (tag) tasarımı

  • http://scxlive.com SCX Live

    this is important for me… good explanation, +1
    thanks

  • Pingback: Tagtastic Tag Cloud with CSS Transformations | Webdesigntuts+

  • Pingback: Tagtastic Tag Cloud with CSS Transformations | How to Web

  • http://www.nolaa.org/coppermine/displayimage.php?album=4&pos=8 captcher

    When I initially commented I clicked the “Notify me when new comments are added”
    checkbox and now each time a comment is added I get several emails
    with the same comment. Is there any way you can remove people
    from that service? Bless you!

  • http://www.longunlimited.com/ Fifty Shades of Grey

    Excellent article. I am dealing with many of these issues as well.
    .