Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Web and personalized recommendations.Start Your Free Trial Now
- View Offline
Want to create your own icon fonts? Check out James' companion course, Creating Icon Fonts for the Web.
- Finding icon fonts
- Ensuring consistent styling
- Exploring class-based solutions for deployment
- Deploying with the data-icon attribute
- Aligning icons
- Animating icons
- Styling multicolored glyphs
Skill Level Intermediate
You never really know where you're going to need to use an icon font. Some icons might appear inside of a paragraph, others might appear inside of something like a heading or a list, and you might use it in a stand alone element like a div or some type of other black level element. Well that type of flexibility is really one of the strengths of icon fonts. But it also means that you have to be aware of the apparent element that it's inside of. You have to be aware of that apparent elements styling that could cause your icons to display incorrectly.
That's why it's a really good idea to do what we call normalizing our icon styling by going ahead and setting explicit values for things like font weight and font style. And so, to demonstrate that, I'm going to use the normalize HTM file, found in the 02_03 directory. I'm just going to scroll down, I'm previewing it in the browser right now. And I've got this particular icon used in three different places. One is inside of a heading, another is inside of a italicized paragraph, and another one is just sort of standing off all by itself inside of a div tag.
And as you can see, it's actually affecting it slightly differently in each instance. It's italicized over in the paragraph, it's a little bolder that the actual icon inside the heading because the heading is bold as well, and there's a weird spacing issue going on inside the div tag down there, that isn't really being controlled through margins. So there is a lot of styling going on to our icons that aren't driven by the icons, themselves, but rather the parent elements. So if I go back into my code editor, I've got the normalize file here open. And you can see kind of how this is constructed.
So we're assigning the icon through the use of the data icon attribute. We'll talk about how to do this a little bit later on, but I just wanted to point out the fact that this particular version of icon font that we're using, Chunky Mobile, is actually encoded to the private use area, so that's what you're seeing there. Here, we have it inside of the heading. Here, we have it inside of the heading, here we have it inside paragraph, and the, here we have it all by its little lonesome. So, three separate places. I'm going to switch over to the CSS file for normalize.htm and that would be base.css. You can find this in the _css directory in the same folder.
And I'm going to scroll all the way down towards the bottom, just saving myself a little bit of time by just shooting right down there. Okay. So I have a little area in here called icon font styles. And these are the only things that I really need to do right here to get my font to display. But I'm going to create some blank lines up here so that I can control the styling. Now there are a lot of different ways to normalize them, and a lot of how you write the selector that's going to do the normalization is based on how you're deploying it. For this example, I'm going to write a global selectory using the universal selector.
That's going to target anything that might have that data icon attribute. That's kind of a really big, broad selector, and it's not something that I might use every single time I depoy icon font, so I don't want you to watch this exercise and think oh, this is the way that I should do this from now on. It's about memorizing the properties that I want you to sort of take with you, not the selector that I'm writing here. So I'm going to do a star, and then I'm going to do attribute-selector, where I'm targeting any element that has the attribute of data-icon. Because that is, in this instance, how we're assigning the icon font.
Open up more curly braces, and then inside that now I'm going to go in and start normalizing it. So these are the properties that I want you to be aware of. So the first thing i'm going to do is font weight. And I'm going to assign this a font weight of 400 I could have said normal, but I really like being explicit with that value 400, because that is what my font was mapped to, in terms of being a regular font. So that's very explicitly saying that you want the default weight for that. Alright, so a font-weight of 400. I'm going to do a font-style of normal. I'm going to do text-transform of none.
I'm going to do font-variant of normal. I'm going to set the letter spacing to normal. And I'm going to set line height to one. Now let's talk about these individually. Okay, so, font weight and font style, those are kind of obvious. I want them to not appear bold. I don't want it to italicize my icons, so both of those things are going to override any parent styles that might be affecting it. Now, text transform and font variant, it's probably really doubtful that I'm going to have too many icons in parents that have small caps applied to it.
But I'm sort of covering my bases here. As far as text transform goes, capitalize or upper-case probably won't affect an icon font. But I don't want to take the chance. So, for different various browsers and devices out there, I'm just going to go ahead and say don't do it at all. Letter spacing, this could really affect my icons if I am trying to control the positioning beside other pieces of text. So I'm going to go ahead and set that to normal. And for my line height, a lot of times you can use line height to control the vertical alignment or vertical placement of an icon within a parent. So I'm just going to go ahead and set that to one as the default, and then I would override that anywhere that I would need to.
What I'm going to do is save this, then go back out to my browser. Okay, so here I am in the browser, and this is the original one. So you can see everything's still italicized. We've got the standalone one that's awkwardly positioned down there. And then we have to bold one here. And if I click over to our normalized version of this, you can see a couple of changes occur right away. Obviously, this icon is not italicized anymore, this one has moved up because of the line height isn't tied to whatever it was inside of before. And, although this is a little bit more subtle, if I toggle back and forth, you can see that the bold versus normal is actually causing that to display maybe a little but crisper, maybe not as fat in certain areas.
It's a really subtle difference but there's a definite difference there in image quality. So keep in mind that even though we used that sort of global selector, that you can normalize your icon fonts either that way through the use of a global selector or you can do like individual selectors as you assign the icon font elements. It's really up to you. You're just going to want to use the most efficient styling possible, but in large part how you normalize your icon fonts is going to be driven by how you choose to deploy them.