Icons. They’re popular. They help complement content (most of the time). And yet they can be a pain in the neck.

Once you’ve decided on a format (SVG or icon font?) and designed the set, there are still other considerations, many of which pop up while you’re coding.

Here’s an excellent question that comes up often along those lines:

Tricky, right? Aligning icons can be a tough task especially if you are not the one charged with designing the icons. I have to be honest and say that I thought this would be a simple question to answer and one that could be done in a tweet. However, there’s much more to finding the perfect alignment between icons and text.

Thankfully, there are a few universal tips we can put to use to ensure our icons always line up beautifully with text.

Tip 1: Decide on an icon format and stick to it

I take for granted that there are other ways to do icons other than SVG. There are actually quite a few methods, if you include ones that might be considered old, as in, “That’s soooo 2011!” A few examples:

Regardless of your preference (cough, SVG, cough!), the format of your icons will have a lot to do with how you align them to text. Let’s say all your icons are contained in a sprite. That means the icons are pretty much set at fixed dimensions (unless you start large and use background-size to scale down) and it would be much easier to adjust the size and line-height of your content to match that of your icons.

The beauty of something like SVG is that it is scalable by nature and affords you the ability to reply more on CSS to help align things rather than the design process.

The lesson here: choose one format for all icons and use that as the basis for how you will approach aligning them to text. Your code will much more maintainable and your designer (or your inner designer) will thank you.

Tip 2: Use similarly sized icons

Here’s an ideal situation: all icons are the exact same size. Let’s say all the icons in a set are 100px square. That tells us exactly how much space we’re dealing with and makes alignment a breeze.

We also know that is not always a practical rule. Icons can take up varying amount of space depending on what we’re communicating.

The map icon is narrower than the avatar icon which is narrower than the cart icon. If you’re like me, then you will eliminate the white space around an icon to ensure the smallest file size possible. That’s great, but now we have icons with different dimensions. That’s a-ok if we’re working with SVG but less desirable for non-vector formats, like PNG. We can tell SVG to occupy a fixed amount of space without losing resolution, but non-vector will stretch and distort the image if we are working with inconsistent dimensions.

The right thing to do here is to match your icon design specs to the format being used. If using SVG, then the icon can be any size, but try to use a consistently sized art board in Illustrator (or your design tool of choice). This will help keep future icons in the set to scale.

If using non-vector, then always use the same dimensions. The icons may differ in how much space they occupy, but it will ensure your icons are always crisp and proportional, regardless of the text next to it.

Tip 3: Match your font-size to the size of the icon set

Icons ought to be treated a lot like typography. They’re either used in conjunction with text or in place of it, so setting the size of the icon to the size of the text next to it is important for the same reason we like consistently sized letters: legibility.

This doesn’t mean icons need to be 16px if your content font size is also 16px. What it means is being aware of the font size and making sure your icons complement it nicely, whether it is larger, the same size, or smaller than the the text.

Here’s an illustration of the difference between an icon that has been aligned nicely to the size of the content versus one that has not.

See the Pen MyxBrQ by CSS-Tricks (@css-tricks) on CodePen.

The example on the left is much more consistent with the content font size and has better alignment than the right, despite both examples technically being aligned.

Tip 4: Use CSS to fine tune the appearance

When all is said and done, there is a solid and simple recipe for getting icons exactly where you want to line up with text:

.icon { position: relative; /* Adjust these values accordingly */ top: 5px; left: 5px; }

That is the difference between icons that look in perfect alignment with text and icons that look slightly off.

See the Pen YqBdxx by CSS-Tricks (@css-tricks) on CodePen.

Tip 5: Use class names for specific icons

The example above is great but you may have noticed that the location icon looks so gosh darn narrow and even further from the text labels than the music note. How annoying!

This is where CSS on a per-icon basis can bail us out.

<ul> <li><svg class="icon-user"><use xlink:href="#icon-user"></use></svg> Weezer</li> <li><svg class="icon-location"><use xlink:href="#icon-location"></use></svg> Santa Monica</li> <li><svg class="icon-music"><use xlink:href="#icon-music"></use></svg> Discography</li> <li><svg class="icon-sphere"><use xlink:href="#icon-sphere"></use></svg> weezer.com</li> <li><svg class="icon-cart"><use xlink:href="#icon-cart"></use></svg> Artist Store</li> </ul>

/* Match any class name that starts with icon */ [class^="icon"] { width: 50px; height: 50px; position: relative; top: 15px; margin-right: 10px; fill: #fff; } /* Reposition the music note icon */ .icon-music { right: 5px; }

Phew, that’s much nicer!

See the Pen QNoBVq by CSS-Tricks (@css-tricks) on CodePen.

Wrapping Up

If there’s a lot more to aligning icons to text than you expected, then you are not alone. The simple answer is that it really depends on what method you choose to create the icons. The CSS is pretty straightforward from there.

The answer to this question inly pertained to text but there are other elements where aligning icons comes into play, such as buttons, form fields, navigation and probably many other things. I mean, what about responsive designs, right?! Regardless of the scenario, I find that the five tips we covered here get me out of the weeds.