I Love This CSS Focus Hack
Key Takeaways
The video covers the CSS :focus state, its variations, and how to use them to improve user experience, including the use of :focus, :focus-visible, :focus-within, and a custom fourth state, as well as the application of these states to custom input elements and dropdowns using CSS selectors like `has` and `focus-within`.
Full Transcript
Focus States may seem incredibly straightforward but there's actually three separate selectors in CSS for different Focus States and even a fourth secret one and knowing which one to use in the right scenario is actually quite difficult so in this video I'm going to explain everything you need to know about the focus State what the different Focus states are and even show you this fourth secret Focus property welcome back to web dev simplified my name is Kyle and my job is to simplify the web for you so you can start building your dream project sooner and to get started with Focus we're going to f focus on the two most simple Focus States and that is the colon focus and the colon Focus visible because these are probably the two most common States you're going to be using Now the default just colon Focus what that's going to do is anytime an element has Focus whether you click on it tab onto it it doesn't matter it's always going to apply those Styles so in our case if we click on this button you can see it has a red outline applied to it same thing here if we click on this input that has the focus attribute applied to it you can see that it has a red outline over around it and if we tab onto that element you can see it has the red outline and same thing here if we tab onto this element you can see it gets that red outline around it so whether you're navigating with the keyboard mouse or some other tool as soon as an element is focused by any means at all it's going to have those Styles be applied now Focus visible is a little bit different because it only shows the focus when it is visibly needed for the user so for example if this button right here has Focus visible when I click on the button you'll notice that there's no red outline applied to it while this this button has a red outline applied to it and that's because if I'm using my mouse to navigate I know that I'm clicking on this button so I don't need to have a red outline around it showing me more information about that button but if I'm using the Tab Key to navigate and I tab onto this element you'll notice I get a red outline around it because I'm using the Tab Key and it's making sure okay you know what I want to focus on where I'm at so the vi focus should be visible to the user in the case of the Tab Key being used this is why when I'm writing out my code for buttons I almost always use Focus visible because clicking on a button and getting the focus style is a little bit jarring and confusing for a user so when I'm writing out buttons I almost always use Focus visible now when it comes to input elements Focus visible and focus actually work exactly the same you'll notice when I click on both of these they give me the focus style and if I use tab to get to them they also give me the exact same Focus style and that's because the browser is smart enough to know that if I'm inside of an input element I probably want to show my focus on that element because for example if I'm on a phone and I click on an input like this it's going to pop up the keyboard which is going to remove things around on my site and it can become hard to follow but if I have that outline showing up around my element it's going to make sure I always know where it's at so it doesn't matter if you use Focus or Focus visible on an input it's going to be outlined in this red at least in Chrome and pretty much any other browser out there so in order to determine which one of these two specific Focus selectors you want to use you just want to determine do I want to show the focus all the time or only when it's visually needed for the user if you want to show it all the time just use the normal focus and it'll always use the focus Styles otherwise use Focus visible which is generally what I prefer to use for pretty much all of my focus Styles now there are two other types of focuses one is built into the browser and the other one's kind of a secret one you need to write yourself and that is going to be Focus within and focus visible within so let me talk about Focus within essentially all that does is allow you to style a parent element based on if it child has Focus so I have this container element which wraps all of my different buttons and inputs and what I can do is I can select that container and I can say focus with within and now it's going to apply these Styles anytime I'm focused on an element that's within this parent container so I could just say like background color is going to be blue doesn't really matter and now you can see when I focus on any of these elements you can see my entire thing has a background color of blue applied to it this is a bit ugly so let's just do a border which is going to be a one pixel solid red border and now you can see when I'm focused on this I have a red border around my container doesn't matter if I use tab to use my focuses or clicking to use my focuses it really doesn't matter and you'll notice even if I'm on an element that doesn't have a focus State that's visible I'm still focused on that element so you can see I get this outline around my container the one problem was Focus within though is that it shows you the focus no matter what whether I'm using clicking in that case I don't actually care about the focus showing up or if I'm using tab where maybe it makes more sense for the focus to show up so The Fourth Kind of secret focus is the focus within visible or Focus visible within but there is no CSS property to do this we need to write our own and luckily it's actually really easy to write your own with the has selector so what you can do is instead change this to has and then put Focus visible just like that now this container will only show that Focus State whenever an element inside of it has the focus being shown visibly to the user so now you can see that if I tab onto this element I get the outline because my focus is visible now you will notice though if I click on this element the focus is not showing up because the focus is not technically visible on that element I can visually see it because I'm manually saying that the focus is going to show that red outline but I don't have any element that has this Focus visible attribute applied to it the tab bar is going to allow me to do this or if I click inside of one of these inputs you'll notice it is essentially Focus visible but if I just click on a specific element like a button you can see that that Focus outline is not showing up so this is a really great way to only show the focus when it should be visually needed when it's a within context for example I want to show it on the container when something within it visually should show that Focus so this cssh has property right here is a really cool property allows you to add in that fourth secret kind of CSS selector that unfortunately doesn't exist in the browser and honestly probably won't because writing out this one align of code is just as easy as writing out that Focus visible within now when it comes to when you would want to use these specific within stylings it's mostly if you have a custom input element that has Focus based on things inside of it maybe you have like a drop down element that you wrote out by hand and you want to have a drop down element where that thing that you click on to open the drop down actually has a focus container around it like an outliner whatever because all the elements inside of it are being focused on really any anytime you have a custom element that you want to show focus and outline of or maybe if you click on something inside of a card you want the card to show a focus State that's another great use case for Focus within and you can write this custom has syntax to make it so that it's only going to show up when it visually is needed for the user now that's really all there is to these Focus dates they're relatively straightforward but if you want to learn more about this has syntax which is quite a bit more complicated and has some really cool use cases I have a full video covering it I'm going to link on the screen for you and with that said thank you very much for watching and have a good day
Original Description
The CSS :focus state is simple on the surface, but there is actually quite a lot going on behind the scenes. There are 3 total :focus states you can use in CSS and even a secret fourth state that make :focus much more complicated than it may first appear. In this video I go over everything you need to know about CSS :focus states.
📚 Materials/References:
CSS :has Selector Video: https://youtu.be/K6CWjkDgQnE
CSS :has Selector Article: https://blog.webdevsimplified.com/2022-09/css-has-selector
🌎 Find Me Here:
My Blog: https://blog.webdevsimplified.com
My Courses: https://courses.webdevsimplified.com
Patreon: https://www.patreon.com/WebDevSimplified
Twitter: https://twitter.com/DevSimplified
Discord: https://discord.gg/7StTjnR
GitHub: https://github.com/WebDevSimplified
CodePen: https://codepen.io/WebDevSimplified
⏱️ Timestamps:
00:00 - Introduction
00:29 - :focus
01:16 - :focus-visible
03:05 - :focus-within
04:19 - :focus-visible-within
05:38 - Use Cases
#CSSFocus #WDS #CSS
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Web Dev Simplified · Web Dev Simplified · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Introduction to Web Development || Setup || Part 1
Web Dev Simplified
Introduction to Web Development || Understanding the Web || Part 2
Web Dev Simplified
Introduction to HTML || Your First Web Page || Part 1
Web Dev Simplified
Introduction to HTML || Basic HTML Elements || Part 2
Web Dev Simplified
Introduction to HTML || Advanced HTML Elements || Part 3
Web Dev Simplified
Introduction to HTML || Links and Inputs || Part 4
Web Dev Simplified
Learn Git in 20 Minutes
Web Dev Simplified
5 Must Know Sites For Web Developers
Web Dev Simplified
10 Best Visual Studio Code Extensions
Web Dev Simplified
Learn CSS in 20 Minutes
Web Dev Simplified
How to Style a Modern Website (Part One)
Web Dev Simplified
How to Style a Modern Website (Part Two)
Web Dev Simplified
3D Flip Button Tutorial
Web Dev Simplified
How to Style a Modern Website (Part Three)
Web Dev Simplified
Animated Loading Spinner Tutorial
Web Dev Simplified
How to Write the Perfect Developer Resume
Web Dev Simplified
Animated Text Reveal Tutorial
Web Dev Simplified
Learn Flexbox in 15 Minutes
Web Dev Simplified
Custom Checkbox Tutorial
Web Dev Simplified
Start Contributing to Open Source (Hacktoberfest)
Web Dev Simplified
JavaScript Shopping Cart Tutorial for Beginners
Web Dev Simplified
Responsive Video Background Tutorial
Web Dev Simplified
1,000 Subscriber Giveaway
Web Dev Simplified
How To Prevent The Most Common Cross Site Scripting Attack
Web Dev Simplified
Transparent Login Form Tutorial
Web Dev Simplified
The Forgotten CSS Position
Web Dev Simplified
How to Code a Card Matching Game
Web Dev Simplified
10 Must Install Visual Studio Code Extensions
Web Dev Simplified
Learn CSS Grid in 20 Minutes
Web Dev Simplified
Learn JSON in 10 Minutes
Web Dev Simplified
10 Essential Keyboard Shortcuts For Programmers
Web Dev Simplified
What Is The Fastest Way To Load JavaScript
Web Dev Simplified
Differences Between Var, Let, and Const
Web Dev Simplified
How To Install MySQL (Server and Workbench)
Web Dev Simplified
Learn SQL In 60 Minutes
Web Dev Simplified
How To Solve SQL Problems
Web Dev Simplified
What Are Design Patterns?
Web Dev Simplified
Null Object Pattern - Design Patterns
Web Dev Simplified
Your First Node.js Web Server
Web Dev Simplified
How To Setup Payments With Node.js And Stripe
Web Dev Simplified
How To Learn Any New Programming Skill Fast
Web Dev Simplified
Asynchronous Vs Synchronous Programming
Web Dev Simplified
JavaScript ES6 Arrow Functions Tutorial
Web Dev Simplified
Are You Too Old To Learn Programming?
Web Dev Simplified
JavaScript Cookies vs Local Storage vs Session Storage
Web Dev Simplified
JavaScript Promises In 10 Minutes
Web Dev Simplified
Builder Pattern - Design Patterns
Web Dev Simplified
JavaScript == VS ===
Web Dev Simplified
JavaScript ES6 Modules
Web Dev Simplified
8 Must Know JavaScript Array Methods
Web Dev Simplified
CSS Variables Tutorial
Web Dev Simplified
JavaScript Async Await
Web Dev Simplified
How To Choose Your First Programming Language
Web Dev Simplified
Easiest Way To Work With Web Fonts
Web Dev Simplified
Singleton Pattern - Design Patterns
Web Dev Simplified
Responsive Navbar Tutorial
Web Dev Simplified
CSS Progress Bar Tutorial
Web Dev Simplified
Learn GraphQL In 40 Minutes
Web Dev Simplified
What is an API?
Web Dev Simplified
Learn How To Build A Website In 1 Hour!
Web Dev Simplified
More on: AI-Assisted Code Review
View skill →Related Reads
📰
📰
📰
📰
The Algorithm of Relevance: Teaching AI Your Client's Niche and Story Angles
Dev.to AI
Code healing AI
Dev.to · Code healing AI
I Thought I Knew Python — Until I Started Using These 8 Features
Medium · Machine Learning
session-indexer: giving Claude Code a memory that doesn't die with the project next door
Dev.to · Valentyn Solomko
Chapters (6)
Introduction
0:29
:focus
1:16
:focus-visible
3:05
:focus-within
4:19
:focus-visible-within
5:38
Use Cases
🎓
Tutor Explanation
DeepCamp AI