← Back to CSS Course | Chapter 9: Modern CSS Features | Lesson 21 of 25

CSS Accessibility

खूबसूरत play zones और picnic tables वाला एक public park बनाने की कल्पना करें, लेकिन जब आप entrance देखते हैं, तो सिर्फ steep, rocky stairs हैं—कोई ramps, paths, या साफ़ signs नहीं। wheelchairs या strollers इस्तेमाल करने वाले visitors आपके park का आनंद लेने से पूरी तरह block हो जाएँगे। Web accessibility (A11y) आपकी website के लिए वही ज़रूरी ramps और signs बनाना है। यह अपने page को इस तरह style करने की कला है कि हर कोई, screen readers से navigate करने वाले visually impaired users या mouse इस्तेमाल न कर पाने वाले लोगों सहित, आसानी से आपकी site browse कर सके। सही focus indicators, high color contrast, और hidden labels इस्तेमाल करना आपके page को inclusive और सबके लिए accessible रखता है।

Keyboard Navigation और Focus States

Keyboard-only और visually impaired users webpages में chronologically navigate करने के लिए Tab key पर निर्भर करते हैं। आपको सुनिश्चित करना चाहिए कि हर interactive element का एक visible focus indicator (जैसे एक border outline) हो ताकि users देख सकें कि वे page पर कहाँ हैं।

Note:
  • सिर्फ तभी 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 के साथ
Warning: browser focus outlines को custom focus styles से replace किए बिना उन्हें hide करने के लिए कभी भी CSS outline: none इस्तेमाल न करें।

उदाहरण: Keyboard Navigation and Focus States

css
<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 पढ़ सकें।

Note:
  • 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 किए जाने चाहिए
Warning: white backgrounds पर light gray text इस्तेमाल करना आपके page को inaccessible बना देता है, search engine bots को आपका quality score घटाने पर मजबूर करते हुए।

उदाहरण: Color Contrast and Text Legibility

css
<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 रखती है।

Note:
  • 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 रोकता है
Warning: accessibility labels hide करने के लिए display: none इस्तेमाल न करें, क्योंकि screen readers उन्हें पूरी तरह नज़रअंदाज़ कर देंगे।

उदाहरण: The Screen Reader Only (.sr-only) Hack

css
<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 या धीमी करने देती है।

Note:
  • तेज़ 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 हटाता है
Warning: motion settings का सम्मान न करना आपके कुछ visitors के लिए physical discomfort पैदा कर सकता है।

उदाहरण: Respecting Motion Settings with prefers-reduced-motion

css
<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 कर सकते हैं।

Note:
  • अपनी 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
Warning: standard accessibility ARIA roles के replacement के रूप में कभी भी custom data attributes इस्तेमाल न करें।

उदाहरण: Styling ARIA States

css
<style>
[aria-expanded="true"] {
  background: #eee;
}
</style>
<button aria-expanded="true">Open</button>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. browser focus outlines को CSS (outline: none) से hide करना बिना उन्हें custom focus styles से replace किए।
  2. low-contrast color combinations इस्तेमाल करना जो mobile screens पर आपका text unreadable बना देते हैं।
  3. 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 हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.