Skip to content

Commit 7008c0a

Browse files
Merge pull request #45 from WebDevSimplified/css-focus-article
Css Focus Article
2 parents 23c50ed + ff0fb3f commit 7008c0a

File tree

10 files changed

+263
-5
lines changed

10 files changed

+263
-5
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
const { focusType } = Astro.props
3+
---
4+
5+
<button class={focusType}><slot /></button>
6+
7+
<style>
8+
button {
9+
border: none;
10+
border-radius: 0.25em;
11+
padding: 0.5em 0.75em;
12+
font-size: inherit;
13+
background: var(--theme-blue);
14+
cursor: pointer;
15+
}
16+
17+
.focus:focus {
18+
background: var(--theme-red);
19+
}
20+
21+
.focus-visible:focus-visible {
22+
background: var(--theme-red);
23+
}
24+
</style>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
const { focusType } = Astro.props
3+
---
4+
5+
<div class={focusType}><slot /></div>
6+
7+
<style>
8+
div {
9+
border: 2px solid var(--theme-blue);
10+
border-radius: 0.25rem;
11+
padding: 1rem;
12+
display: flex;
13+
gap: 1rem;
14+
15+
@media (max-width: 700px) {
16+
flex-direction: column;
17+
}
18+
}
19+
20+
.focus-within:focus-within {
21+
border-color: var(--theme-red);
22+
}
23+
24+
.focus-visible-within:has(:focus-visible) {
25+
border-color: var(--theme-red);
26+
}
27+
</style>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
const { focusType, placeholder } = Astro.props
3+
---
4+
5+
<input placeholder={placeholder} class={focusType} />
6+
7+
<style>
8+
input {
9+
background: var(--theme-code-inline-bg);
10+
border: 2px solid var(--theme-blue);
11+
border-radius: 0.25em;
12+
padding: 0.25em 0.5em;
13+
font-size: inherit;
14+
color: inherit;
15+
outline: none;
16+
}
17+
18+
.focus:focus {
19+
border-color: var(--theme-red);
20+
}
21+
22+
.focus-visible:focus-visible {
23+
border-color: var(--theme-red);
24+
}
25+
</style>

src/components/BlogPost.astro

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import ShareButtons from "/src/components/ShareButtons.jsx"
66
export interface Props {
77
title: string
88
date: string
9+
updatedDate: string
910
url: string
1011
tags: string[]
1112
}
1213
13-
const { title, date, tags, url } = Astro.props
14+
const { title, date, updatedDate, tags, url } = Astro.props
1415
const siteUrl = Astro.site.href
1516
---
1617

@@ -19,7 +20,16 @@ const siteUrl = Astro.site.href
1920
<div>
2021
<header>
2122
<h1 class="title">{title}</h1>
22-
<p class="publish-date">{dateFormatter.format(new Date(date))}</p>
23+
<p class={`publish-date ${updatedDate && "old-date"}`}>
24+
{dateFormatter.format(new Date(date))}
25+
</p>
26+
{
27+
updatedDate && (
28+
<p class="publish-date">
29+
Updated: {dateFormatter.format(new Date(updatedDate))}
30+
</p>
31+
)
32+
}
2333
<div style="margin-bottom: 1rem">
2434
<TagBar
2535
tags={tags.map(tag => {
@@ -75,6 +85,11 @@ const siteUrl = Astro.site.href
7585
color: var(--theme-text-lighter);
7686
}
7787

88+
.publish-date.old-date {
89+
font-size: 0.9rem;
90+
text-decoration: line-through;
91+
}
92+
7893
.title {
7994
font-size: 2.25rem;
8095
font-weight: 700;

src/components/BlogPostPreview.jsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,20 @@ export default function BlogPostPreview({ post }) {
2424
margin: 0,
2525
fontSize: "1rem",
2626
color: "var(--theme-text-lighter)",
27+
display: "flex",
28+
flexDirection: "column",
2729
}}
2830
>
29-
{dateFormatter.format(post.date)}
31+
<span
32+
style={{
33+
textDecoration: post.updatedDate ? "line-through" : "none",
34+
}}
35+
>
36+
{dateFormatter.format(post.date)}
37+
</span>
38+
{post.updatedDate && (
39+
<>Updated: {dateFormatter.format(post.updatedDate)}</>
40+
)}
3041
</p>
3142
<TagBar
3243
marginTop=".25em"

src/layouts/BlogPost.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
title,
1212
description,
1313
date,
14+
updatedDate,
1415
author = "Kyle Cook",
1516
permalink,
1617
tags,
@@ -26,7 +27,7 @@ const url = Astro.request.url
2627
<body>
2728
<BaseTopOfBody />
2829
<div class="wrapper">
29-
<BlogPost {title} {author} {date} {tags} {url}>
30+
<BlogPost {title} {author} {date} {updatedDate} {tags} {url}>
3031
<slot />
3132
</BlogPost>
3233
</div>

src/pages/2022-09/css-has-selector/index.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
layout: "@layouts/BlogPost.astro"
33
title: "CSS :has A Parent Selector And It Is Better Than I Ever Could Have Hoped For"
44
date: "2022-09-26"
5+
updatedDate: "2024-07-29"
56
description: "CSS has been lacking a parent selector for years and now they finally introduced one with the :has pseudo class and it is incredible!"
67
tags: ["CSS"]
78
---
@@ -115,7 +116,7 @@ This selector has an overall specificity of 2 classes and 1 element since the fi
115116

116117
## Browser Support
117118

118-
With every cool CSS feature you always have to consider browser support, but luckily the browser support for this new property is quite good. It may seem low considering it only has [56.2% support](https://caniuse.com/css-has), but this is a bit misleading since every major browser other than Firefox has support for this feature. The reason for the low percentage is because this features just launched at the end of August 2022 in Chrome so people are still in the process of updating Chrome to the latest versions. Firefox also has this feature behind a feature flag which hopefully means it is coming to the browser very soon. In just a few months this feature should be above 90% and able to be used on pretty much every site.
119+
The `:has` selector is supported in all major browsers as of the end of 2023.
119120

120121
## Conclusion
121122

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
layout: "@layouts/BlogPost.astro"
3+
title: "Do You Know All 4 CSS Focus Styles?"
4+
date: "2024-07-29"
5+
description: "Handling focus in CSS is quite a bit more complicated than it seems and in this article I cover the 3 main CSS focus methods as well as a secret fourth focus method."
6+
tags: ["CSS"]
7+
---
8+
9+
import CSSFocusButton from "@blogComponents/cssFocusCrashCourse/CssFocusButton.astro"
10+
import CSSFocusInput from "@blogComponents/cssFocusCrashCourse/CssFocusInput.astro"
11+
import CSSFocusContainer from "@blogComponents/cssFocusCrashCourse/CssFocusContainer.astro"
12+
import Tangent from "@blogComponents/lib/Tangent.astro"
13+
14+
## Introduction
15+
16+
It may seem strange to write an entire article on handling focus in CSS since it is a pretty simple property at first glance. That is until you realize that CSS actually has 3 (plus one secret) focus methods that all behave differently. These properties even have different behaviors depending on what elements you use them on which makes it even more confusing. In this article I will be breaking down all 4 focus methods, showing you when to use each one, and how they differ from each other.
17+
18+
## What Is Focus?
19+
20+
Before we dive into each focus method we need to understand what a focus state is. When an element is focused it means that it is currently selected by the user. This can happen when a user clicks on an element, uses the tab key to navigate through a page, or even when a user interacts with an element using a screen reader.
21+
22+
The focus state is important for accessibility reasons as it helps users understand where they are on a page and what they are interacting with. Imagine trying to fill out a form and not knowing which input you are currently typing in. This is what the web would be like if there were no focus states.
23+
24+
## `:focus`
25+
26+
The main focus method is the `:focus` pseudo-class. This is the what most people think of when they think of focus styles in CSS, but this pseudo-class has quite a few problems which is why I almost never use it.
27+
28+
```css {5}
29+
button {
30+
background-color: blue;
31+
}
32+
33+
button:focus {
34+
background-color: red;
35+
}
36+
```
37+
38+
The styles within the `:focus` pseudo-class will be applied whenever an element is focused. It doesn't matter how the element was focused (clicked on, keyboard navigation, screen readers, etc.) it will always show the focus styles. You can see this in action in the example below.
39+
40+
<CSSFocusButton focusType="focus">Focus Me</CSSFocusButton>
41+
42+
As you can see it doesn't matter how you focus this button it will always turn red. This is generally not ideal since when you click on a button you don't generally want it to show focus styles after you have finished clicking on it, but you do want the focus state to persist when using keyboard navigation since this focus state tells the user where they are on the page.
43+
44+
<Tangent>
45+
If you are having trouble focusing on the button with the keyboard try
46+
clicking on the text just above the button and then clicking the
47+
<kbd>Tab</kbd> key.
48+
</Tangent>
49+
50+
## `:focus-visible`
51+
52+
I mentioned how `:focus` is not always ideal since it shows the focus styles no matter how the element was focused. This is where the `:focus-visible` pseudo-class comes in. This pseudo-class is a bit smarter than `:focus` since it only shows the focus styles when the browser deems the user needs those styles to know where they are on the page and what element they currently have focused. I almost always use `:focus-visible` over `:focus` since it provides a better user experience.
53+
54+
```css {5}
55+
button {
56+
background-color: blue;
57+
}
58+
59+
button:focus-visible {
60+
background-color: red;
61+
}
62+
```
63+
64+
<CSSFocusButton focusType="focus-visible">
65+
Focus Me With Keyboard
66+
</CSSFocusButton>
67+
68+
As you can see the above button above does not show any focus styles when it is clicked, but if you use the keyboard to navigate to this element it will show the focus styles.
69+
70+
<Tangent>
71+
If you look closely you may notice that there is a ring that appears around
72+
the button when you use keyboard navigation to focus it. This is part of the
73+
default browser styles and is called the `outline` property. The browser
74+
applies this `outline` property using the `:focus-within` pseudo-class which
75+
is why it only appears when needed to assist the user in knowing where they
76+
are on the page.
77+
</Tangent>
78+
79+
One thing to note about `:focus-visible` is that it works differently when used on different elements. For example, if you use `:focus-visible` on an input element it will show the focus style no matter what. It doesn't matter if you click the input or use keyboard navigation it will always show the focus styles. This is because the browser deems it important to show the focus styles on input elements no matter how they are focused.
80+
81+
```css {5}
82+
input {
83+
border-color: blue;
84+
}
85+
86+
input:focus-visible {
87+
border-color: red;
88+
}
89+
```
90+
91+
<CSSFocusInput focusType="focus-visible" placeholder="Focus Me" />
92+
93+
## `:focus-within`
94+
95+
The `:focus-within` pseudo-class is a bit different than the previous two focus methods. This pseudo-class is used to apply styles to a parent element based on the `:focus` state of its children. If any of the children would show the `:focus` state then the parent will also show the `:focus-within` state.
96+
97+
```css {5}
98+
.container {
99+
border-color: blue;
100+
}
101+
102+
.container:focus-within {
103+
border-color: red;
104+
}
105+
```
106+
107+
<CSSFocusContainer focusType="focus-within">
108+
<CSSFocusButton focusType="focus">:focus</CSSFocusButton>
109+
<CSSFocusButton focusType="focus-visible">:focus-visible</CSSFocusButton>
110+
<CSSFocusButton>No Focus Styles</CSSFocusButton>
111+
</CSSFocusContainer>
112+
113+
In the above example there is container that will show a red border anytime one of its children is focused. There are 3 buttons in that container. The first has a `:focus` style, the second has a `:focus-visible` style, and the third has no focus styles. As you can see it doesn't matter which button you click the container will always show the `:focus-within` styles since one of its children is focused.
114+
115+
It is important to know that the child elements do not need to have a focus style defined for the parent to show the `:focus-within` state. The parent will show the `:focus-within` state as long as one of its children is focused. This works with any element that can be focused, not just buttons.
116+
117+
## The Secret Fourth Focus Method
118+
119+
The final focus method is a bit different since there is no built in CSS pseudo class for `:focus-visible-within`. Instead we have to write our own custom CSS selector that does the same thing. This custom selector combines hwo `:focus-within` and `:focus-visible` work to create a focus method that only shows focus styles when a child of the element would have its `:focus-visible` styles shown.
120+
121+
```css {5}
122+
.container {
123+
border-color: blue;
124+
}
125+
126+
.container:has(:focus-visible) {
127+
border-color: red;
128+
}
129+
```
130+
131+
<CSSFocusContainer focusType="focus-visible-within">
132+
<CSSFocusButton focusType="focus">:focus</CSSFocusButton>
133+
<CSSFocusButton focusType="focus-visible">:focus-visible</CSSFocusButton>
134+
<CSSFocusButton>No Focus Styles</CSSFocusButton>
135+
<CSSFocusInput focusType="focus" placeholder=":focus" />
136+
</CSSFocusContainer>
137+
138+
I have pretty much the same code in this example as the previous example, but I added a single input element to the container. This input element has a `:focus` style applied to it. This container will only show the custom `:focus-visible-within` styles when I use the keyboard to navigate to any of the buttons or when I focus the input in any way. If I click on any of the buttons the container will not show the custom `:focus-visible-within` styles.
139+
140+
The way this `:focus-visible-within` works is by using the `:has` pseudo element to only select the `.container` if at least one child element has the `:focus-visible` pseudo-class applied to it.
141+
142+
<Tangent>
143+
If you want to learn more about the `:has` selector and why I love it you can
144+
check out my complete [:has selector article](/2022-09/css-has-selector).
145+
</Tangent>
146+
147+
## Conclusion
148+
149+
Hopefully this article has helped you understand how each focus method differs from the others and when each should be used. Personally, I almost never use `:focus` as `:focus-visible` is almost always a better user experience. I don't tend to use `:focus-within` or the custom `:focus-visible-within` as much, but when you need them they are incredibly useful.

src/pages/index.astro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import BaseTopOfBody from "src/components/BaseTopOfBody.astro"
88
99
interface MarkdownFrontmatter {
1010
date: number
11+
updatedDate?: number
1112
title: string
1213
description: string
1314
tags: string[]
@@ -30,6 +31,9 @@ const allPosts = allMarkdownPosts
3031
description: post.frontmatter.description,
3132
tags: post.frontmatter.tags,
3233
date: new Date(post.frontmatter.date),
34+
updatedDate: post.frontmatter.updatedDate
35+
? new Date(post.frontmatter.updatedDate)
36+
: undefined,
3337
url: post.url,
3438
}))
3539
.sort((a, b) => b.date.valueOf() - a.date.valueOf())

src/styles/theme.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
--theme-popup-bg: var(--color-gray-700);
8888
--theme-tangent-bg: var(--color-gray-700);
8989
--theme-tangent-border: var(--color-gray-600);
90+
--theme-tangent-code-inline-bg: var(--color-gray-500);
9091

9192
--theme-red: var(--color-red-dark);
9293
--theme-blue: var(--color-blue-dark);

0 commit comments

Comments
 (0)