Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
By now, I think we've seen some patterns emerge in terms of best practices when deploying icon fonts. There's one thing we need to really take care of though before we move onto the individual styling properties that we can do with the icon fonts, and that is talking about how to improve the accessibility of the icons on our page. So there are a couple of problems that can crop up in terms of accessibility. The first is if you have generated content that is injected into your HTML, screen readers will read that.
And so if it's a letter, that's really a problem but a lot of it times it can cause a screen reader to read something that the user really doesn't understand what's going on with that. The other use case where accessibility comes into play is if you're thinking about positioning an icon all by itself, obviously that's going to mean something to people looking at the layout, but it's not really going to be visible at all to folks on accessibility devices and screen readers and things like that. So, we need to address both of those. The first thing that we can do, is we can hide content from screen readers that we don't want to be read.
There's a couple of ways that we can do that. One is to use the aria hidden attribute. That's an attribute that can be applied to any HTML element. And it's either, it supports values with true and false, so what you can do is if you want to hide something from a screen reader, for example you can take that element and give it an attribute of aria hidden equals true and that's going to make the content of that element invisible to screen readers. Well, that's great and it works well and that's a perfect solution for what we're doing, but it does apply a little bit of a constraint on us.
Let's say you have a paragraph with some text and you wanted an icon to appear in front of that text. Well you can't put the aria-hidden attribute on the paragraph itself, because if you do that, it's going to hide all the content from screen readers including the icon, which isn't what you want in that instance. So that means that we have to move our icons into individual elements, which we've been doing actually by placing them in span tags. And what that does is it isolates the icon. It allows us to apply that attribute to the span tags themselves.
Of course, in doing that, we have to add a little bit of additional mark up. It's not that big of a deal. There's also a CSS property called speak, which in CSS, you can apply to any selector, and that tells screen readers not to read it as well. It's really not that widely supported, so you're putting that in there more to be future friendly than anything else. Now, the other thing, if you have a stand alone icon and you want to hide it, the best way to do that is to just surround the text, write some descriptive text next to the icon, surround it with a span tag and then just use positioning to move it off the page.
Screen readers will still read it, but people looking at the layout won't see it. So here's an example of using the Speak attribute in your CSS, right here, speak none. Notice it's just right there on the global style that we're applying for our data icons. And then, we're using a span to display the icon and then then we're using aria-hidden=true to hide it as well. Alright so what are we going to be doing for our exercise? We're going to add the user profile icon to this paragraph right here and here's the encoding for that.
And then we're going to add the user profile icon and then we're going to hide the generated content from screen readers. Below that, we want to add a stand alone icon for user profiles. That includes descriptive text that'll be read by screen readers but won't be visible on our layout. So I'm switching back to my HTML file. This is accessibility.htm. You can find this in the 03_05 directory. First thing I want to do is just go over to the CSS really quickly. I'm going to scroll down to the very bottom of this.
Here's the selector that's controlling our icon fronts, which is fine, there's nothing wrong with it. But right after content I'm going to add the speak property, and I'm just going to do speak: none. Again, this is not widely supported yet, but hopefully it will be in the future, and there's nothing wrong with going ahead and putting it in here, because we don't want this generated content to be read by screen readers. So the majority of what we're going to be doing is actually going to be done in the HTML file, so I'm going to go back to the HTML, scroll down to the paragraph down there at the bottom of it. And so here we have a paragraph with the text to User Profile, okay? So inside that I'm going to go ahead and do a span tag, and for the span tag I'm going to do a data-icon attribute.
And I'm going to make that value equal to the encoding that I can see just above that. So I'm going to do ampersand, octothorpe, X, E046, and a semicolon. So after a while it just becomes second nature to you. And of course I'm also going to use the aria hidden attribute. And I want that value to be equal to true. Then I'm going to close that and close the span tag. So, in one fell swoop, i've applied the icon, and i've also hidden the generated content from screen readers.
So that's one. I'm going to save that. So the next thing I need to do is go down into the paragraph below this, and I need to apply a user profile icon to this paragraph that's going to be visible, but I also want text that's going to be read by a screen reader. So we know how to do that. I'm going to go ahead and copy this span tag just to make life easier for myself. I'm going to copy and paste it. Here we go. Even though we want descriptive text for this icon, I'm still going to have the aria-hidden=true for the span tag. That's to prevent a screen reader reading some funky character or something if substitution occurs for that particular encoding.
Alright the next thing I'm going to do is I'm going to add another span tag directly after this one, alright? So I'm going to go ahead and throw in another span tag, and inside the span tag I'm going to type in the text that I want the screen reader to read. In this case the text's is just going to say User Profile. And you might be wondering why I'm actually adding another span tag. Well Im going to this a class, and I'm going to call this class alt-text. So it's pretty descriptive, it's alternate text and then I'm going to save that. Now you might be wondering why I don't use this existing class that we have here called profile.
Well in order to make sure that the text can't be seen on the layout but can be read by the screen reader, it needs to remain in our HTML code but it needs to not be visible. Meaning off to the the side. If I take the entire paragraph and move it off to the side, the icon goes with it. So I only need the text to go off to the side. So I've got this class all text, I'm going to go back into my base CSS. I'm going to scroll down here, and we're just going to go ahead and create a class for alt, text, and we're just going to do some really easy stuff here.
I'm just going to position this with absolute positioning and I'm going to take the left position and I'm just going to push that thing all the way over into the next room. Negative 9,999 ems. A lot of people do it to the top and the left. I figure that's just being cruel. I'm sending it far enough away as it is. So I'm going to save that, go down to my accesibility.htm and check that in the browser. And as I scroll down, indeed, there we have User Profile with the icon beside it. And then, we have the icon standing alone. The good news is, the text User Profile will be read in both instances by screen readers, but in the top version, it won't read whatever the generated content for the icon is, and in the bottom icon, the text, User Profile is not visible in the layout.
Awesome. So there's not a lot of extra work here that's required to make sure the icon fonts are accessible. It's something that you should do every single time you're using icons within your pages. It's just a better overall practice and you're creating a better experience for a large segment of users.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 59927 Viewers
61 Video lessons · 92632 Viewers
82 Video lessons · 104169 Viewers
56 Video lessons · 107126 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.
Your file was successfully uploaded.