惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
雷峰网
雷峰网
Recent Announcements
Recent Announcements
月光博客
月光博客
G
Google Developers Blog
腾讯CDC
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
T
Tenable Blog
云风的 BLOG
云风的 BLOG
W
WeLiveSecurity
博客园 - 【当耐特】
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
P
Privacy International News Feed
MyScale Blog
MyScale Blog
K
Kaspersky official blog
T
The Blog of Author Tim Ferriss
Attack and Defense Labs
Attack and Defense Labs
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
aimingoo的专栏
aimingoo的专栏
I
Intezer
Vercel News
Vercel News
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
Latest news
Latest news
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tor Project blog
S
Security Affairs
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
博客园 - Franky
C
Cyber Attacks, Cyber Crime and Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
美团技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
Security @ Cisco Blogs
L
LINUX DO - 热门话题
Know Your Adversary
Know Your Adversary
Project Zero
Project Zero
D
Docker
L
Lohrmann on Cybersecurity
F
Full Disclosure

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience How Remix makes CSS clashes predictable Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super simple start to Netlify functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode Profile a React App for Performance
Please stop building inaccessible forms (and how to fix them)
2019-02-04 · via Kent C. Dodds Blog

Note, today's blog post is very heavily inspired by the Labeling Controls tutorial from w3.org.

HTML is accessible by default. This is true, with the important caveat that when you use semantic HTML properly, what you've built will be accessible. For the most part... There are definitely some UI elements that can be a real challenge and you need a combination of CSS and JavaScript with that HTML to make it truly accessible. But that's not the subject of this article. We're just talking about semantic HTML which makes most built-in UI elements (like forms) accessible. Now, there are lots of ways that you can mess this up. Today I'm going to focus on <label> and how to ensure that your form controls (inputs) are properly labeled.

Before I get into that, allow me to just take a quick tangent and say this: PLEASE USE YOUR FORM WITH NOTHING BUT THE KEYBOARD AND SEE IF IT IS POSSIBLE. So many forms are impossible to use without a mouse and it drives me crazy. Lucky for me I can use a mouse, but so many people in the world cannot. For them, it is impossible to fill out your form.

Ok, so let's talk about labels.

I'm regularly confronted with a form control that's written like this:

Username:
<input />

To a seeing user, this looks fine, but to a blind user, they'll need to use a screen reader and without a label the user is left to their best guess as to what the input is expecting when they focus on the input.

But it takes more than just putting the label text in a <label> input. So this wont work:

<label>Username</label>
<input />

⚡️ You can know that the input has a label associated with it by checking the input's labels property or the label's control property.

Ok, here are four ways to associate the label with the input (in order of personal preference):

label[for] ➡ input[id]

<label for="username">Username</label>
<input id="username" />

input[aria-labelledby] ➡ label[id]

<label id="username">Username</label>
<input aria-labelledby="username" />

label 🤗 input

I like to think of this one as the label hugging the input

<label>
	Username
	<input />
</label>

This one works fine and it's nice because it means you don't have to make globally unique IDs (typically in a client-rendered app I'll generate those randomly anyway), but it's not my favorite mostly because it can be harder to style things the way I want them to, and getByLabelText requires you provide a selector when you do this.

input[aria-label]

<input aria-label="Username" />

I don't really like this approach because it removes a visible label which has other accessibility implications.

Conclusion

You can actually also use the title attribute, but apparently screen readers are inconsistent in considering this a label, so just stick with one of the above methods.

As a bonus, when you properly associate a label to a form control, the clickable area of the form control includes the label which is especially useful for checkboxes and people on mobile devices.

Another bonus: you'll be able to use getByLabelText and that'll make your tests resemble the way your software is used more closely which is great!

You can read more about this here: Using label elements to associate text labels with form controls.

I hope that's helpful to you. Again, in addition to making sure that your form controls are properly labeled, please please try using your form with a keyboard only. They're both very low hanging fruit and can make a big difference in the accessibility of your forms.

Install and use eslint-plugin-jsx-a11y

Good luck! 💪