Skip to main content
आपका ब्राउज़र पुराना है, इसलिए यह साइट ठीक से नहीं दिखेगी। कृपया Chrome, Edge, Firefox या Safari का नया संस्करण इंस्टॉल करें।

Accessibility Quick Fixes: Copy-Paste Code Snippets

Free, copy-paste HTML, CSS, JavaScript, and React snippets for common WCAG 2.2 accessibility fixes

इमेजेज़ में ऑल्ट टेक्स्ट जोड़ें

html

इमेजेज़ की कंटेंट का वर्णन करने के लिए ऑल्टरनेटिव टेक्स्ट प्रदान करें

<!-- Decorative image -->
<img src="decoration.png" alt="" />

<!-- Informative image -->
<img src="chart.png" alt="Sales increased by 25% in Q4" />

<!-- Functional image (button) -->
<img src="search-icon.png" alt="Search" />
html

फॉर्म फील्ड्स में लेबल्स जोड़ें

html

स्क्रीन रीडर उपयोगकर्ताओं के लिए फॉर्म इनपुट्स के साथ लेबल्स जोड़ें

<!-- Explicit label -->
<label for="email">Email Address</label>
<input type="email" id="email" name="email" />

<!-- Implicit label -->
<label>
  Email Address
  <input type="email" name="email" />
</label>

<!-- Using aria-label -->
<input type="search" aria-label="Search products" />
html

बटन्स को एक्सेसिबल बनाएं

html

सुनिश्चित करें कि बटन्स के पास एक्सेसिबल नाम हैं और वे कीबोर्ड से एक्सेसिबल हैं

<!-- Good: Native button with text -->
<button>Submit Form</button>

<!-- Good: Button with aria-label -->
<button aria-label="Close dialog">
  <span aria-hidden="true">×</span>
</button>

<!-- Good: Icon button -->
<button aria-label="Delete item">
  <svg aria-hidden="true">...</svg>
</button>
html

सही हेडिंग हायरार्की

html

लेवल्स स्किप किए बिना हेडिंग्स को क्रम में उपयोग करें (h1 → h2 → h3)

<h1>Page Title</h1>

<h2>Main Section</h2>
<p>Content...</p>

<h3>Subsection</h3>
<p>Content...</p>

<h2>Another Main Section</h2>
<p>Content...</p>
html

मुख्य कंटेंट पर जाने के लिए स्किप लिंक जोड़ें

html

कीबोर्ड उपयोगकर्ताओं को रिपिटेटिव नेविगेशन स्किप करने की अनुमति दें

<a href="#main-content" class="skip-link">
  Skip to main content
</a>

<nav>...</nav>

<main id="main-content" tabindex="-1">
  <!-- Main content here (tabindex="-1" lets focus move here) -->
</main>

<style>
.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  background: #000;
  color: #fff;
  padding: 8px;
  text-decoration: none;
}

.skip-link:focus {
  top: 0;
}
</style>
html

विज़िबल फोकस इंडिकेटर्स

css

सुनिश्चित करें कि कीबोर्ड फोकस हमेशा विज़िबल रहे

/* Modern approach using :focus-visible */
button:focus-visible,
a:focus-visible,
input:focus-visible {
  outline: 3px solid #4A90E2;
  outline-offset: 2px;
}

/* Custom focus style */
.custom-focus:focus-visible {
  outline: 2px solid transparent; /* still shows in Windows High Contrast, where box-shadow is dropped */
  box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.5);
  border-color: #4A90E2;
}
css

पर्याप्त कलर कंट्रास्ट सुनिश्चित करें

css

ऐसे रंगों का उपयोग करें जो WCAG AA मानकों को पूरा करें (सामान्य टेक्स्ट के लिए 4.5:1)

/* Bad: Low contrast */
.bad-text {
  color: #777777;
  background-color: #FFFFFF;
  /* Ratio: 4.48:1, fails AA (needs 4.5:1) */
}

/* Good: High contrast */
.good-text {
  color: #595959;
  background-color: #FFFFFF;
  /* Ratio: 7.0:1, passes AAA */
}

/* Large text (24px, or 18.66px bold) needs only 3:1 */
.large-text {
  font-size: 24px;
  color: #949494;
  background-color: #FFFFFF;
  /* Ratio: 3.03:1, passes AA for large text only */
}
css

डायनामिक कंटेंट की घोषणा करें

javascript

डायनामिक अपडेट्स की घोषणा करने के लिए ARIA लाइव रीजन्स का उपयोग करें

<!-- Polite announcement (waits for user to finish) -->
<div aria-live="polite" aria-atomic="true">
  <p id="status-message"></p>
</div>

<!-- Assertive announcement (interrupts immediately) -->
<div aria-live="assertive" role="alert">
  <p id="error-message"></p>
</div>

<script>
// Update status
document.getElementById('status-message').textContent = 
  'Form submitted successfully';

// Show error
document.getElementById('error-message').textContent = 
  'Error: Please fill in all required fields';
</script>
html

कीबोर्ड ट्रैप्स रोकें

javascript

सुनिश्चित करें कि उपयोगकर्ता हमेशा कीबोर्ड से बाहर निकल सकें

// Modal dialog with keyboard trap prevention
function openModal() {
  const modal = document.getElementById('modal');
  const closeBtn = modal.querySelector('.close-button');
  
  modal.style.display = 'block';
  closeBtn.focus();
  
  // Trap focus within modal
  modal.addEventListener('keydown', (e) => {
    if (e.key === 'Escape') {
      closeModal();
    }
    
    // Handle Tab key
    if (e.key === 'Tab') {
      const focusableElements = modal.querySelectorAll(
        'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
      );
      const firstElement = focusableElements[0];
      const lastElement = focusableElements[focusableElements.length - 1];
      
      if (e.shiftKey && document.activeElement === firstElement) {
        lastElement.focus();
        e.preventDefault();
      } else if (!e.shiftKey && document.activeElement === lastElement) {
        firstElement.focus();
        e.preventDefault();
      }
    }
  });
}
javascript

एक्सेसिबल React बटन कंपोनेंट

react

React में एक पुन: उपयोग योग्य एक्सेसिबल बटन बनाएं

import { ButtonHTMLAttributes, ReactNode } from 'react';

interface AccessibleButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
  children: ReactNode;
  variant?: 'primary' | 'secondary';
  isLoading?: boolean;
}

export function AccessibleButton({ 
  children, 
  variant = 'primary',
  isLoading = false,
  disabled,
  ...props 
}: AccessibleButtonProps) {
  return (
    <button
      {...props}
      disabled={disabled || isLoading}
      aria-disabled={disabled || isLoading}
      aria-busy={isLoading}
      className={`btn btn-${variant} ${disabled ? 'btn-disabled' : ''}`}
    >
      {isLoading ? (
        <>
          <span className="spinner" aria-hidden="true" />
          <span className="sr-only">Loading...</span>
        </>
      ) : children}
    </button>
  );
}
typescript

Pro tip

These templates are starting points. Always test your implementations with real screen readers and keyboard navigation. Use our Contrast Checker tool to validate colors, then run a free scan on AccessiSight to track improvements.

Frequently Asked Questions