Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Having to add a letter or something like an HTML character entity to your page, in order to get your icons to display, is actually pretty inefficient. It also places needless content in the page itself, and that can be a problem for things like screen readers and other assistive devices. So, to avoid having to add content to the page to get your icons to show up, you can actually use CSS-generated content to help display your icons. Generated content uses the before and after pseudo selectors to inject content, either before or after the content of whatever element you're targeting.
And that means you can do something like, you could have an empty span and then inject the icon inside of it or you can take something like an existing paragraph and place an icon before or after its content. So, that's pretty cool. Let's take a look at how this works, and the first thing I want to do is just go over some of the syntax of it for those of you who might not be familiar with it, so. We're looking in the browser right now at the generated.htm file from a 03_02 directory and this is the file that we'll be using as our exercise file as well. So, we got a little bit of a sample of the syntax here, and you can see that I'm targeting right now a class of icon.
This could be anything you want it to be. It could be a paragraph, a span tag, a class, an ID, whatever. And then we have the pseudo-class selector before. So colon before. Now, sometimes you might see that with two colons instead of one. When we actually start typing that out itself, I'll kind of go over what's going on there. So, then inside the declaration, we have a font-family being defined, and that, of course, would be the icon font. And then we have this property, and this is the important one. Content. Content basically is, here's what I want to go inside this. Now, this could be a literal string, but in this case, we're sending along the hex value of the icon we want to use.
The escape character here tells the browser, hey, I need the Unicode value of this. I need you to go find this character for me. Now, we could also use the HTML entity there as well. For content, either of those values will work just fine. And then we have font size, and that's equal to 1em, and all that's doing is just sizing it, that's it. This is the really important one right here. So, down below this example, I've got a little thing down there that says, let's use generated content. And I have a notation that says, display the camera icon then supply us a Unicode value of e007 for that.
And so, we are back to using our version of chunky mobile that is encoded in the private use area. So, here I'm in my Code Editor and I'm going to scroll down here and find, here we go, here is our paragraph, I'm just going to apply this class directly to the paragraph. I give class of icon, alright, and we'll save that. And now, I'm going to switch over to our CSS file and I've scrolled all the way down here towards the bottom. You can see I've got an existing style here for icon. And take a look at what this is doing, it's assigning the font family of chunky mobile and then it's normalizing it.
So, this is just sort of a generic font assignment and normalization style, this really isn't displaying anything quite yet. So, if I go down to the next line, I'm going to do a class selector of icon, and then colon before. Now, I mentioned earlier that I was going to talk about the, sort of, two colons versus one. Sometimes it's common for you guys, especially in recent tutorials, to see it like this. Other times, you'll see it like that. And I've had a lot of people ask me, hey, what's the difference? Well, essentially, there are a lot of different psuedo-class selectors. And there are four specific selectors that apply to links.
That will be link, hover, active, and visited. And then, now there's a bunch of other psuedo-class selectors. Before and after being two of them. And so, they were looking for a way to sort of differentiate between the ones that were associated with links versus sort of the other ones, and essentially, that associates that as another one. Now, what's really interesting about this is all modern browsers support either syntax, but older browsers only support this syntax. So, that's why you'll never see me really using that, even thought it is semantically and syntactically, I should say, correct.
This gets a little bit more broad support, so that's why I do it like that. So, inside here, let's go ahead and start using the content property. I'm just going to type in content and if we remember, in quotation marks, I'm going to do the escape character and then e007, because we remember that was our camera. A little semicolon there and then I want to do just a couple of other things here to help it display a little bit better. I'm going to give it a display property of inline block, and the reason I want to do inline block is because I'd like to add a little bit of space between the icon and the text.
Since we're using before, it's going to put the camera at the beginning of this. So now, I can do a margin-right, and I'll just do eh, 0.2ems, that'll give me a little bit of space. So, I'm going to save that, and then go back to my generated.htm, check that out in the browser, and there is our camera icon being displayed with the before pseudo-class selector. So, if you really think about this, you'll come to the conclusion that this is a really flexible way to display icon fonts. You can insert them into any element that you want.
And that pseudo-selector rule allows you to apply styling that's specific to just that icon. Now, I'm also equally sure that if you were thinking about this. A couple of you are probably thinking that is incredibly inefficient. Right now, we're relying on a single class for every single icon. Meaning we'd have to replicate the styling over and over again for each icon we want to use. And that's why I want to take some time in the next exercise to show you how to create a more efficient, class-based solution.
Get unlimited access to all courses for just $25/month.Become a member