CSS Accessibility
In this page:
Keyboard Navigation और Focus States
Keyboard-only और visually impaired users webpages में chronologically navigate करने के लिए Tab key पर निर्भर करते हैं। आपको सुनिश्चित करना चाहिए कि हर interactive element का एक visible focus indicator (जैसे एक border outline) हो ताकि users देख सकें कि वे page पर कहाँ हैं।
- सिर्फ तभी prominent outlines लागू करने के लिए :focus-visible selector इस्तेमाल करें जब elements keyboard से focused हों।
- Accepted values:
- :focus-visible — keyboard-only focus ring
- outline: <width> <style> <color> — एक visible ring
- outline-offset: <length> — element से spacing
- outline: none — सिर्फ एक visible replacement के साथ
उदाहरण: Keyboard Navigation and Focus States
<style>
a:focus-visible {
outline: 3px solid orange;
}
</style>
<a href="#">Tab to me to see a visible focus outline</a>
Color Contrast और Text Legibility
Web Content Accessibility Guidelines (WCAG) माँग करती हैं कि आपके text colors background fill के against पर्याप्त contrast रखें। यह सुनिश्चित करता है कि visual impairments वाले users या bright sunlight में screens पढ़ने वाले लोग आपका content पढ़ सकें।
- regular paragraph text के लिए कम से कम 4.5:1 का contrast ratio बनाए रखने का लक्ष्य रखें।
- Accepted values:
- WCAG AA normal text — कम से कम 4.5:1 का contrast ratio
- WCAG AA large text — कम से कम 3:1
- WCAG AAA — normal text के लिए 7:1
- color और background-color — दोनों check किए जाने चाहिए
उदाहरण: Color Contrast and Text Legibility
<style>
.good { color: #111; background: #fff; }
.poor { color: #ccc; background: #fff; }
</style>
<p class="good">4.5:1 contrast, easy to read</p>
<p class="poor">Poor contrast, hard to read</p>
Screen Reader Only (.sr-only) Hack
कभी-कभी आपको specifically screen-reader users के लिए extra instructions या labels देने की ज़रूरत होती है बिना उन्हें screen पर visually दिखाए। .sr-only class text को visually hide करती है जबकि उसे screen readers के लिए पूरी तरह accessible रखती है।
- icon-only buttons के action को साफ़ तरीके से describe करने के लिए .sr-only class इस्तेमाल करें।
- Accepted values:
- position: absolute — इसे flow से हटाता है
- width: 1px; height: 1px — visible size collapse करता है
- overflow: hidden — content को clip करता है
- clip-path: inset(50%) — visually hide करता है
- white-space: nowrap — wrapping रोकता है
उदाहरण: The Screen Reader Only (.sr-only) Hack
<style>
.sr-only {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0,0,0,0);
}
</style>
<button>🔍<span class="sr-only">Search</span></button>
prefers-reduced-motion से Motion Settings का सम्मान करना
कुछ users motion sickness या vestibular disorders से पीड़ित होते हैं जो तेज़, bouncing, या spinning web animations से trigger हो सकते हैं। prefers-reduced-motion media query आपको उनकी system settings के आधार पर automatically अपनी animations disable या धीमी करने देती है।
- तेज़ moving animations को automatically साफ़, instant transitions से बदलने के लिए prefers-reduced-motion इस्तेमाल करें।
- Accepted values:
- @media (prefers-reduced-motion: reduce) — user ने कम motion माँगा
- @media (prefers-reduced-motion: no-preference) — default
- animation: none — animations हटाता है
- transition: none — transitions हटाता है
उदाहरण: Respecting Motion Settings with prefers-reduced-motion
<style>
.spin { width: 200px; padding: 20px; background: #cfe8ff; border: 2px solid #1e6fd9; animation: pulse 1s infinite alternate; transition: all 0.3s; }
@keyframes pulse { to { transform: scale(1.1); } }
@media (prefers-reduced-motion: reduce) {
* {
animation: none;
transition: none;
}
}
</style>
<div class="spin">Motion off if you prefer reduced motion</div>
ARIA States को Style करना
interactive components (जैसे custom dropdown tabs या modals) बनाते समय, आपको screen readers को उनकी active states describe करने के लिए standard ARIA attributes (जैसे aria-expanded="true") इस्तेमाल करने ही चाहिए।
आप अपनी active states के आधार पर custom styles लागू करने के लिए इन attributes को सीधे अपनी stylesheets के अंदर भी target कर सकते हैं।
- अपनी styles और accessibility states को साफ़ तरीके से aligned रखने के लिए अपनी CSS में ARIA state selectors इस्तेमाल करें।
- Accepted values:
- [aria-expanded="true"] — expanded state
- [aria-disabled="true"] — disabled state
- [aria-current="page"] — current item
- [aria-selected="true"] — selected item
- [aria-hidden="true"] — assistive tech से hidden
उदाहरण: Styling ARIA States
<style>
[aria-expanded="true"] {
background: #eee;
}
</style>
<button aria-expanded="true">Open</button>
- browser focus outlines को CSS (outline: none) से hide करना बिना उन्हें custom focus styles से replace किए।
- low-contrast color combinations इस्तेमाल करना जो mobile screens पर आपका text unreadable बना देते हैं।
- screens से elements hide करने के लिए display: none इस्तेमाल करना जबकि उम्मीद करना कि screen readers उन्हें पढ़ेंगे।
- Web accessibility सुनिश्चित करती है कि आपकी website को कोई भी navigate कर सके, physical या cognitive abilities चाहे जो भी हों।
- :focus-visible selectors इस्तेमाल करके अपने interactive elements पर हमेशा distinct focus indicators शामिल करें।
- decorative text hide करने के लिए .sr-only class, तेज़ animations disable करने के लिए prefers-reduced-motion, और active layout states style करने के लिए ARIA selectors इस्तेमाल करें।
Standard CSS accessibility selectors और prefers-reduced-motion queries हर modern web browser द्वारा natively supported हैं।
Chapter Quiz — Complete all 25 topics to unlock
0/25 topics done
Complete these topics first:
- CSS Container Queries
- CSS Subgrid
- CSS Logical Properties
- CSS Logical Sizing
- CSS Writing Modes
- CSS Aspect Ratio
- CSS Object Fit
- CSS object-position
- CSS Masking
- CSS Math Functions
- CSS Motion Path
- CSS @supports
- CSS @property
- CSS At-Rules
- CSS Cursor
- CSS Will Change
- CSS User Interface
- CSS Print Styles
- CSS Pagination (Print)
- CSS Dark Mode
- CSS Accessibility
- CSS Performance
- CSS Preprocessors
- CSS Frameworks Overview
- CSS Interview Prep