Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 833 of the free egghead.io lessons, plus get CSS content delivered directly to your inbox!

Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.

Focus management using CSS, HTML, and JavaScript

8:34 CSS lesson by

Something important to consider when coding a web application is managing the user's focus. For keyboard and screen reader users, we must ensure their focus is not dropped as content is deleted or shown in new contexts. Skip links also provide a way for users to get past a lot of content. In this lesson, you'll learn HTML, CSS and JavaScript techniques for focus management that can be applied to many situations.

Get the Code Now
click to level up

egghead.io comment guidelines


Something important to consider when coding a web application is managing the user's focus. For keyboard and screen reader users, we must ensure their focus is not dropped as content is deleted or shown in new contexts. Skip links also provide a way for users to get past a lot of content. In this lesson, you'll learn HTML, CSS and JavaScript techniques for focus management that can be applied to many situations.


I wish this video included an explanation of what tabindex="-1" is about. I appreciate the practical tip, but I had to go research why this works elsewhere. I'm just beginning to think seriously about web accessibility, so I don't have any context for how these attributes work.


That is great feedback. I'll do a follow-up on tabindex!

In reply to

...something you should really consider if you're coding a web application is focus management, making sure that keyboard and screen reader users are guided through your application as elements are removed or new contexts are added such as modal windows.

You want to make sure that you're not ever dropping their focus or leaving them in the wrong context as new things appear so that you're making a totally inaccessible experience.

I'll show you a couple techniques today that you can use in web applications. Keep in mind, this is a really simple demo. These techniques become more useful as you have more content, larger applications that become really critical that the user is kept up to date with what's happening, and that their focus is never dropped.

In my demo page, I have a couple of headings, I've got some links, and I've got a list of items with a delete button on each one. If I hit the delete button, it will remove that item. I can do a refresh since it's a soft delete.

But each item, when I focus on that button and I delete the item that I'm focused on, my focus is dropped. It's somewhere in between. If I tab, I can get to the next button, which isn't too far off. It didn't kick me all the way to the top. However, I'm not handling the focus. For a screen reader, this becomes really problematic. I'll show you why.

Turn on voiceover with command F5.

Voiceover on Chrome, focus management window. Focus internal link. A link may delete item one. Button left three items. You are currently on a button. To click this button, press control option space.

If I hit the inner key to delete this item, let's see what happens. It shows an outline, it looks like I'm focused on the next button, but it didn't announce anything. If I hit the inner key again, nothing happens. If I hit tab...

Delete item two. Button left two items.

Now, you can see I am on that button.

To click this button, press control option space.

When I'm on the button, because it's the focusable element and it has JavaScript event attached to it, I can hit enter and it will delete the item. But if I'm not on the actual item, it won't do it. The focus not being handled makes this unusable as is.

What we're going to do...

Voiceover off.

...is go back to our JavaScript and add a little bit of focus management. I have a really simple jQuery script. It looks for a button with a class of button delete, and then on click, it will remove it based on its parent.

What I need to do is get a hold of a remaining list item, look for one that's still there that has a focusable button in it to delete that item, that way I can send focus to that button after this one is deleted.

I'm going to grab the list items that I've stored in a variable. I'm going to find the closest button delete just with find. Then, I'll make it the first one.

Just in case we're on the left one and we don't know which item we're on, we're just going to send it back to the first item in the list by doing the programmatic .focus event. Now, if we go back to the browser and I fire up voiceover again, we'll see what happens.

Voiceover on Chrome. Focus internal link. A link, item one. Button left, three items. Delete item two. Button left, two items. Delete item three. Button left, one item. Voiceover off.

As you can see, when I delete each item, it sends focus into a button that's nearby so that I can keep deleting items if I want or at least I am not being left in no man's land on some element that both isn't announced and isn't actionable.

It is important if you're doing a list like this where you're deleting items, that you make some sort of an announcement to the user with the screen reader that the item has been deleted.

It would be good to look at Aria live regions. That's one way to do that. But by sending focus to the next item, we are making them somewhat aware that the item is being deleted, so that's good. This is a real good start.

The next thing we're going to do is make these regions of the page easier to navigate with skip links. I'm going to go over to our HTML. At the top, I'm going to add an unordered list and give it a class of skip links. This is a really common way to make your site much easier to use with a keyboard and a screen reader.

I'm going to add an anchor with an internal link and point it to main so we can skip past the header and get to the main content. Then, I'm going to add another anchor with an internal link of footer and say global footer. This list has two items in it. It will let me skip past the header in case I've got a ton of navigation I want to get by and I want to get to the content.

To make these skip links actually work, we need to give an ID that matches that internal link. I'm going to say an ID of main and then I'm going to say ID of footer on the footer. That will bind these two together, but there is one more thing that we need to make sure that the focus is caught when we use these skip links.

That is adding a tab index of negative one. I'm going to put that on the main and on the footer. That way when we go to use these skip links, they will send our focus to the appropriate area.

Now, I know what you're going to say. That blue focus line if hideous. How do I get rid off it? You could argue both ways. I think this focus outline is really useful to know where you are and, depending on the site and how accepting the designers are of accessibility, you may want to leave that enabled.

But I will tell you a trick that...it is OK to do this. You shouldn't ever remove the focus outline for buttons and anchors and things that you need to see where you are.

However, for focus management, it is generally accepted to add a CSS selector for the tab index attribute when it matches negative one. That means if you've added negative one to something to make it programmatically focusable, it is technically OK to add the outline of none. I'll show you what happens.

When I tab onto the link that says main content, it still has the blue focus outline so I can see where I am. But if I hit enter, I'm now focused on the main element, but the blue focus outline was suppressed. You can argue about that all day if you want.

I just wanted to tell you that that is OK to do, and it makes people a little bit happier. That way when they're clicking around the screen, they don't see that blue outline when they click on that area.

The skip links are handy, but we want to hide them from people until they're focused on them. We're going to add a little bit of CSS. I had a skip links class on that UL. I'm going to add a position of absolute to pull them out of the page order. Then for each list item inside of it, I think those are actually fine.

We'll go straight to the anchor. We're going to style each anchor giving it a display of blocks so we can position it. We'll give it a position of absolute. Then, I'm going to do a little hack that we like to do sometimes to put things off the screen with negative 99,999, however many you want.

Now, we've got these skip links. I'm also going to give them a background color so that they stand out on top of the content, and I will give them a little bit of padding. We've got these skip links that, if we go back to the page, now, they are missing.

Ah, we did need one more thing, the skip link list items. We can do this right on the...we don't need another selector. I can say on the UL list style "none." I'll add a UL to the selector just so it's not ambiguous.

We have an unordered list of skip lines. They're now pulled out of the page flow, but we want to make them visible on focus. I'm going to say UL skip links A focus, get that focus pseudo element. Then, we'll give it a left of zero.

With this CSS only solution, we now have skip links that show up. You could play with the position and the style. But you now have skip links that will get you through the page and the focus is handled much better.

Joel's Head
Why are we asking?