I Love This CSS Focus Hack

Web Dev Simplified · Beginner ·💻 AI-Assisted Coding ·2y ago

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 Introduction to Web Development || Setup || Part 1
Introduction to Web Development || Setup || Part 1
Web Dev Simplified
2 Introduction to Web Development || Understanding the Web || Part 2
Introduction to Web Development || Understanding the Web || Part 2
Web Dev Simplified
3 Introduction to HTML || Your First Web Page || Part 1
Introduction to HTML || Your First Web Page || Part 1
Web Dev Simplified
4 Introduction to HTML || Basic HTML Elements || Part 2
Introduction to HTML || Basic HTML Elements || Part 2
Web Dev Simplified
5 Introduction to HTML || Advanced HTML Elements || Part 3
Introduction to HTML || Advanced HTML Elements || Part 3
Web Dev Simplified
6 Introduction to HTML || Links and Inputs || Part 4
Introduction to HTML || Links and Inputs || Part 4
Web Dev Simplified
7 Learn Git in 20 Minutes
Learn Git in 20 Minutes
Web Dev Simplified
8 5 Must Know Sites For Web Developers
5 Must Know Sites For Web Developers
Web Dev Simplified
9 10 Best Visual Studio Code Extensions
10 Best Visual Studio Code Extensions
Web Dev Simplified
10 Learn CSS in 20 Minutes
Learn CSS in 20 Minutes
Web Dev Simplified
11 How to Style a Modern Website (Part One)
How to Style a Modern Website (Part One)
Web Dev Simplified
12 How to Style a Modern Website (Part Two)
How to Style a Modern Website (Part Two)
Web Dev Simplified
13 3D Flip Button Tutorial
3D Flip Button Tutorial
Web Dev Simplified
14 How to Style a Modern Website (Part Three)
How to Style a Modern Website (Part Three)
Web Dev Simplified
15 Animated Loading Spinner Tutorial
Animated Loading Spinner Tutorial
Web Dev Simplified
16 How to Write the Perfect Developer Resume
How to Write the Perfect Developer Resume
Web Dev Simplified
17 Animated Text Reveal Tutorial
Animated Text Reveal Tutorial
Web Dev Simplified
18 Learn Flexbox in 15 Minutes
Learn Flexbox in 15 Minutes
Web Dev Simplified
19 Custom Checkbox Tutorial
Custom Checkbox Tutorial
Web Dev Simplified
20 Start Contributing to Open Source (Hacktoberfest)
Start Contributing to Open Source (Hacktoberfest)
Web Dev Simplified
21 JavaScript Shopping Cart Tutorial for Beginners
JavaScript Shopping Cart Tutorial for Beginners
Web Dev Simplified
22 Responsive Video Background Tutorial
Responsive Video Background Tutorial
Web Dev Simplified
23 1,000 Subscriber Giveaway
1,000 Subscriber Giveaway
Web Dev Simplified
24 How To Prevent The Most Common Cross Site Scripting Attack
How To Prevent The Most Common Cross Site Scripting Attack
Web Dev Simplified
25 Transparent Login Form Tutorial
Transparent Login Form Tutorial
Web Dev Simplified
26 The Forgotten CSS Position
The Forgotten CSS Position
Web Dev Simplified
27 How to Code a Card Matching Game
How to Code a Card Matching Game
Web Dev Simplified
28 10 Must Install Visual Studio Code Extensions
10 Must Install Visual Studio Code Extensions
Web Dev Simplified
29 Learn CSS Grid in 20 Minutes
Learn CSS Grid in 20 Minutes
Web Dev Simplified
30 Learn JSON in 10 Minutes
Learn JSON in 10 Minutes
Web Dev Simplified
31 10 Essential Keyboard Shortcuts For Programmers
10 Essential Keyboard Shortcuts For Programmers
Web Dev Simplified
32 What Is The Fastest Way To Load JavaScript
What Is The Fastest Way To Load JavaScript
Web Dev Simplified
33 Differences Between Var, Let, and Const
Differences Between Var, Let, and Const
Web Dev Simplified
34 How To Install MySQL (Server and Workbench)
How To Install MySQL (Server and Workbench)
Web Dev Simplified
35 Learn SQL In 60 Minutes
Learn SQL In 60 Minutes
Web Dev Simplified
36 How To Solve SQL Problems
How To Solve SQL Problems
Web Dev Simplified
37 What Are Design Patterns?
What Are Design Patterns?
Web Dev Simplified
38 Null Object Pattern - Design Patterns
Null Object Pattern - Design Patterns
Web Dev Simplified
39 Your First Node.js Web Server
Your First Node.js Web Server
Web Dev Simplified
40 How To Setup Payments With Node.js And Stripe
How To Setup Payments With Node.js And Stripe
Web Dev Simplified
41 How To Learn Any New Programming Skill Fast
How To Learn Any New Programming Skill Fast
Web Dev Simplified
42 Asynchronous Vs Synchronous Programming
Asynchronous Vs Synchronous Programming
Web Dev Simplified
43 JavaScript ES6 Arrow Functions Tutorial
JavaScript ES6 Arrow Functions Tutorial
Web Dev Simplified
44 Are You Too Old To Learn Programming?
Are You Too Old To Learn Programming?
Web Dev Simplified
45 JavaScript Cookies vs Local Storage vs Session Storage
JavaScript Cookies vs Local Storage vs Session Storage
Web Dev Simplified
46 JavaScript Promises In 10 Minutes
JavaScript Promises In 10 Minutes
Web Dev Simplified
47 Builder Pattern - Design Patterns
Builder Pattern - Design Patterns
Web Dev Simplified
48 JavaScript == VS ===
JavaScript == VS ===
Web Dev Simplified
49 JavaScript ES6 Modules
JavaScript ES6 Modules
Web Dev Simplified
50 8 Must Know JavaScript Array Methods
8 Must Know JavaScript Array Methods
Web Dev Simplified
51 CSS Variables Tutorial
CSS Variables Tutorial
Web Dev Simplified
52 JavaScript Async Await
JavaScript Async Await
Web Dev Simplified
53 How To Choose Your First Programming Language
How To Choose Your First Programming Language
Web Dev Simplified
54 Easiest Way To Work With Web Fonts
Easiest Way To Work With Web Fonts
Web Dev Simplified
55 Singleton Pattern - Design Patterns
Singleton Pattern - Design Patterns
Web Dev Simplified
56 Responsive Navbar Tutorial
Responsive Navbar Tutorial
Web Dev Simplified
57 CSS Progress Bar Tutorial
CSS Progress Bar Tutorial
Web Dev Simplified
58 Learn GraphQL In 40 Minutes
Learn GraphQL In 40 Minutes
Web Dev Simplified
59 What is an API?
What is an API?
Web Dev Simplified
60 Learn How To Build A Website In 1 Hour!
Learn How To Build A Website In 1 Hour!
Web Dev Simplified

The video teaches how to use CSS focus states to improve user experience and accessibility, including the use of custom CSS selectors and properties to style focus states. By applying these concepts, developers can create more accessible and user-friendly interfaces.

Key Takeaways
  1. Use the :focus state to apply styles whenever an element has focus
  2. Use the :focus-visible state to show the focus only when it is visibly needed for the user
  3. Select a container element and use `focus-within` to apply styles to the container when a child element has focus
  4. Use the `has` selector to create a custom `focus-visible-within` property
  5. Style the container element to show focus when an element within it has focus
💡 The use of custom CSS focus states and selectors can significantly improve the accessibility and user experience of web applications, especially for custom input elements and dropdowns.

Related Reads

📰
The Algorithm of Relevance: Teaching AI Your Client's Niche and Story Angles
Learn to teach AI about your client's niche and story angles to improve relevance and engagement
Dev.to AI
📰
Code healing AI
Learn how Code Healing AI helps developers find bugs and security issues faster, and how to apply similar AI-powered tools to your own development workflow
Dev.to · Code healing AI
📰
I Thought I Knew Python — Until I Started Using These 8 Features
Discover 8 hidden Python features to boost automation project efficiency and take your coding skills to the next level
Medium · Machine Learning
📰
session-indexer: giving Claude Code a memory that doesn't die with the project next door
Improve project continuity with session-indexer, a tool that saves Claude Code's memory for future use
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
Up next
How to Create Presentations in Microsoft Cowork (Copilot) with Templafy MCP
Templafy
Watch →