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:
@chriscoyier #smashingconf any recommendations on how to align #svg icons to text?
— Andreas Sahle (@pixelmord) October 20, 2015
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:
- SVG icons
- Icon fonts
- CSS image sprites
- Inline images
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.
I use my own custom font icons and the best tip I’ve found to date is to design all icons with the same canvas size, however when exporting individually, if some are narrower than others export the icon only, the not full canvas.
This ensures two things, it avoids uneccesery padding and keeps padding even left and right of the icon.
Dave Gandy has some great articles on Medium about this.
Hm… with svg you can basically display them as inline blocks and then align properly. I prefer using baseline alignment. Also, you can just make them to take size from current font size context, so: width: 1em and height: 1em.
You may need to adjust positioning of some particular icons — for that you can set vertical alignment to something like -0.2em — you can use it to adjust icon position relatively to baseline, which also should scale with font size
I use this approach, although I didn’t do any research or anything –
vertical-align: -0.Xem
just seemed like the cleanest way do to it, off the top of my head.I’m taking lately a mixture of old and new, making sprites in svg. These are the reason for which I got to use this technique.
Today easier icon model is to use sources. That when we use a generic model available as FontAwsome. This method will meet the need in many cases. However, possess several disadvantages as aliasing in S.O, foreign sources if you use any extension that changes the source browser, in addition to being able to use only one color.
PNG format with sprites is still used in famous sites like Google, YouTube, Facebook and Instagram. For most people tell me that is to reach the largest possible number of users, I also see the advantage of consistency. In addition to being supported by all browsers, the icons in PNG will always be presented in a clear way navigator with the default setting. This method also possess disadvantages as retina screen, browser zoom.
Online SVG would be great, but I’ve realized two things that bother me enough, the first is the excess code. A complex icon may contain hundreds of lines quite dirty code. This is the price we pay to edit the element via CSS. Another disadvantage is when I realized that for some reason the user does not use the default site css or disables the case of people with disabilities or low bandwidth internet, icons appear huge on the screen, being well discomforting.
In 90% of the time the icon is accompanied by a description, the example was very well built for then I also agree with the direct element to the html. For example, as in the case below, we can hide a text using the icon, and even then it will be readable by screen readers.
User </ icon> Weezer
But most of the time what we see is:
icon browser </ icon> Browser Google Chrome is the fastest
In the second example, I believe there is redundant information. In such cases, when the icon will serve only for the aesthetics of the site, I recommend using the background-image.
</ div> Browser Google Chrome is the fastest
With the inclusion of a div, perfect via CSS handling without compromising the content itself. Because these causes I have adopted the method of sprites svg, is a relative size, some case the css icon is disabled. Like all methods he possess the disadvantage of having to duplicate the image in the case of some specific effect.
I generally use icons with background-image, is there anything wrong with this approach? I find it makes positioning a lot easier and it requires less css. It also removes some of the cross-browser issues with SVG.
About Tip 4, I think you NEVER want to add margins in
px
when you are working with icons. You most likely want to useem
to make your style scalable with different font sizes.just wanted to say the same
+1 for that
You forgot the ligatures method! My fav!
It makes easier the position of the icon, compared to the
:before
pseudo-element method, because with the ligature you have just one box (thei
) instead of two (thei
and the:before
).It’s also good for screen readers.
The
i
element isn’t for icons (even though a lot of people seem to think it fits (I guess because it starts with “i”)). Read this for more: http://html5doctor.com/i-b-em-strong-element/. Also, if you’re displaying only an icon thetitle
attribute works just fine.Finally; how is styling ‘:before` harder than styling any other element? They all behave the same (except for a few form related elements).
Personally I use gulp-fontello. Alignment is rarely an issue.
#tip3 : Instead of using top left or margins in icons size to match font size , i used to put line-height from font-size and vertical align to center its position..
I combine this technique (line-height + font-size) with ::before and use the background image technique (mentioned earlier) to have the ultimate flexibility for size and positioning.