Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using generated content

From: Deploying Icon Fonts for the Web

Video: Using generated content

Having to add a letter or something like an HTML character entity to And now, I'm going to switch over to our CSS file

Using generated content

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.

Show transcript

This video is part of

Image for Deploying Icon Fonts for the Web
Deploying Icon Fonts for the Web

21 video lessons · 2156 viewers

James Williamson
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Deploying Icon Fonts for the Web.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.