Skip to main content
Il tuo browser è troppo vecchio per visualizzare correttamente questo sito. Aggiorna all'ultima versione di Chrome, Edge, Firefox o Safari.

Accessibility Quick Fixes: Copy-Paste Code Snippets

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

Aggiungi Testo Alternativo alle Immagini

html

Fornisce testo alternativo per descrivere il contenuto delle immagini

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

Aggiungi Etichette ai Campi del Modulo

html

Associa etichette agli input del modulo per gli utenti di screen reader

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

Rendi i Pulsanti Accessibili

html

Assicura che i pulsanti abbiano nomi accessibili e siano accessibili da tastiera

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

Corretta Gerarchia dei Titoli

html

Usa i titoli in ordine (h1 → h2 → h3) senza saltare livelli

<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

Aggiungi Link per Saltare al Contenuto Principale

html

Permette agli utenti da tastiera di saltare la navigazione ripetitiva

<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

Indicatori di Focus Visibili

css

Assicura che il focus da tastiera sia sempre visibile

/* 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

Assicura Contrasto Colore Sufficiente

css

Usa colori che soddisfano gli standard WCAG AA (4,5:1 per testo normale)

/* 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

Annuncia Contenuto Dinamico

javascript

Usa regioni ARIA live per annunciare aggiornamenti dinamici

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

Previeni Trappole da Tastiera

javascript

Assicura che gli utenti possano sempre uscire con la tastiera

// 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

Componente Pulsante Accessibile in React

react

Crea un pulsante accessibile riutilizzabile in 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