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
Often, icon fonts will have icons that represent the various states of UI elements or widgets. For example, the chunky mobile font that we've been using throughout our course has several sets of icons that are designed to indicate UI state of elements. For example, we have the battery here that indicates the charger of the battery. We have Wi-Fi and signal strength. And right down here we've got the icons that we're going to use in our example which is the state of the volume. So mute all the way to, to loud.
Well, you know, one of the things that we have to think about. When we start trying to display the states of various UI elements with these icons is how's that going to work with the existing deployment strategy that we've got for our icons. What I mean by that is it's really common when people are. Working with UI elements to use certain frameworks or have certain structures built in. It's really incompatible with the way you're already displaying your icons. Now, I'm not talking about using things like the hover pseudo class selector.
You can certainly do that and that's going to change the stay of an icon based on user interaction, I'm talking more about widgets like the slider that you're seeing here. Or certain types of form elements and you want to make some changes to those. Often those have built in constraints on styling and functionality. So, sometimes you actually have to change deployment strategies, and use multiple deployment strategies for displaying icons in your site. You might have one for interactive widgets. And then you might have another one for displaying just normal icons on the page.
So in this exercise what we're going to do, is we're going to take this slider widget right here, and we're going to indicate the changes in states by showing the volume icon on the right hand side. Okay, so here I am in my code editor. And I'm in the state.htm file, in the 04_03 directory. The first thing I want to do is, give you guys an idea of, of what's going on here in terms of the HTML page, and the structure, and what we're working with. So I'm using Google's CDN Code CDN Network, to bring in J Query, ant the J Query U-I libraries.
I know they recommend you doing it without the protocol, but I have found that if I am testing locally, and I don't have that protocol, and I'm not working with a local server, then it fails. So I always put the protocol on there for exercise files in case people are testing locally. All right, if I scroll down I can see that I've got a div with an ID of slider UI. That's a wrapper div for this interior slider right there. Below that I've got a function written. And this one is, it's a really simple little function.
What it does is it's it's initiating the slider. It's passing some parameters in terms of, you know, it starts with zero. It increments in values of 25 and it goes up to 75. You know it's volume so if I were to really have been thinking about it I would of made it go up to 11, but it is what it is. And then every time somebody interacts with the slider it's firing off this function. What it's doing is it's going to go out and find the element with an ID of icon. It's going to remove any classes that it has and then it's going to add whatever class That matches the UI value in the classes variable.
So if I look up at the classes variable, you can see that matching the the UI value is zero, 25, 50, and 75. And then we have classes for volumes one, two, three and four. So what should stand out to you here is that, you know, prior to this we've used a deployment strategy pretty much that is using the data icon attribute to display icons. Well the way that this particular function is set up that's not going to be possible. Now we could certainly probably modify this function to enable that but what I'm trying to illustrate here is that a lot of times you have to come up with an alternate strategy.
Okay so I'm going to scroll up a little bit. And in order to display our icon I'm still going to use a span tag. So I'm just going to go ahead and create a new span tag. Right after the slider div. And we still want it to conform to our accessibility requirements. So I'm going to give it an aria hidden value equal to true. And then I want to give it, an ID of icon, and remember down here, we are targeting the element of icon, so this is, this is the expand tag that we were talking about, and then finally I'm going to give it an initial class, although this will be striped out, when, somebody starts interacting, of volume one.
All right, I'm going to close my span tag out and save that. Now, right now, if I go back into my browser and refresh this and nothing's really going on here. And that's because we haven't styled this at all or assigned any type of an icon to this value. So what I'm going to do is open up the base.css And I'm going to scroll down towards the bottom where you guys will notice some slight differences from the way that we've been doing things normally. Okay, the first thing you're going to notice is we actually have two normalized blocks here.
The first one is for spans that are using the data icon attitude. The second one is for ones using the aria hidden attribute because, regardless of whether we're using data-icon or not, we are always going to use aria-hidden. And so, because of that I can just go ahead and just do the regular normalization through that, whereas some of these specifics like displaying it block and 64 pixels, those'll be reserved for displaying icons using the data-icon attribute. All right the only other styling we've got going on here at this time or our slider elements, the slider UI and the slider itself.
And that's just basically to make sure that it's going to play nice within the area that have defined for it. All right, so now what we need to do is display the icons. And to do this, we're going to go back to more of a class based approach. So, if we recall, the function is going to assign a new class every single time somebody interacts with it. So, that's what we're going to take advantage of we going to type in span.vol1. So that's the initial class. And we're going to use again generated content to display our icons. We're not changing that part of the strategy.
But this time we can be explicit in what we're setting. And what we're going to do, is we're going to set the volume, which is found at, and I need to do an escape here. I could do the HTML entity, but I actually like doing this one better when I, when I'm able to do it. And it's e069, that's the muted version of the volume icon. And then we're just going to do some basic styling of this. We're going to do font-size of 32 pixels. We're going to do a line-height of one just to sort of normalize that out.
And we'll do a display of in-line block. All right, I'm going to go ahead and save this. And then because, you know, I always like to save time when I can, I'm going to copy and paste this, one, two, three more times. And I'm going to go through and change the class name to vol2, vol3 and vol4, and then one at a time I'm going to go in, to the content declaration, and I'm going to lower that by one. So, from 69 to 68, 68 to 67, 67 to 66, and if I go back into my font, you can see why we're doing that.
There's 69, 68, 67 and 66, okay, I'm going to save this. Go back out to my browser and indeed now I can see there is my mute icon showing up. Then I grab this slider and move it. The states change and because the icon font was designed with these to be the exact same position and the exact same size. It does a really nice job of representing the state change. Now what's really nice about this is you can get as creative with this as you want. For example, I could go back into each of these guys and assign a, a slightly different color to them.
So, for the first one, for example, I could come in and do a color of, say, pound 888 which is just a real neutral grey so maybe that's you know, sort of disabled if you will, and then because that's the mute,. With the next one I might start increasing the amount of color that's in this, so maybe this one is like A06 four, six, five I'd love to let you guys think that, you know, that I'm just making up those colors on the fly, but the truth of the matter is that I kind of played around with this before I started recording.
I know that's a big shock to you guys. The next one, I'm going to give the color of CF4649. Essentially what I'm doing is I'm making it get a little bit more red. Each time, finally color for the last one. I'm going to make that FF0004, save that, go back out to my browser and now you'll notice that color adds a little bit of an extra element to changing the icon states. So once you've mapped out a deployment strategy, it's really just up to you to decide exactly how you want to style and represent those different icon states.
You know, I'm not recommending what I did there as a best practice of interacting with Widgets, or even as a best practice of displaying the icon states at various levels What I really want to get you thinking about is the fact that occasionally, you're going to run into times within an application or a project where you have constraints that fall outside of the strategy that you've developed for deploying your icon fonts. When that happens, you need to sit down and really organize your thoughts. Think about how you're going to approach this and how you're going to write those styles that aren't going to overwrite or interfere with anything else you're doing within your site regarding your icons.