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

推荐订阅源

大猫的无限游戏
大猫的无限游戏
K
Kaspersky official blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
aimingoo的专栏
aimingoo的专栏
M
MIT News - Artificial intelligence
小众软件
小众软件
云风的 BLOG
云风的 BLOG
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
Stack Overflow Blog
Stack Overflow Blog
量子位
S
Secure Thoughts
G
GRAHAM CLULEY
C
CXSECURITY Database RSS Feed - CXSecurity.com
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
T
Threat Research - Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
有赞技术团队
有赞技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 聂微东
宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Forbes - Security
Forbes - Security
Engineering at Meta
Engineering at Meta
S
Security Affairs
Help Net Security
Help Net Security
博客园 - 三生石上(FineUI控件)
AWS News Blog
AWS News Blog
博客园 - 叶小钗
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
H
Heimdal Security Blog
W
WeLiveSecurity
C
Check Point Blog

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! 💪