Managing focus in dynamic content
Description: #
Managing focus effectively in dynamic content is essential for accessibility. When content changes dynamically—such as with modals, pop-ups, accordions, or dynamically loaded sections—focus management helps keep users oriented and ensures they interact with the content as intended.
Good Practices in Focus Management for Dynamic Content
Focus Managed in a Modal Dialog When a modal dialog appears, the focus should move to the first interactive element in the modal and remain within the modal until it is closed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Good Focus Management in Modal</title>
<style>
/* Modal styling */
.modal {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
border: 1px solid #ddd;
padding: 20px;
width: 300px;
box-shadow: 0 5px 10px rgba(0,0,0,0.3);
}
.modal.active {
display: block;
}
.focusable:focus {
outline: 3px solid #0056b3;
}
</style>
<script>
function openModal() {
const modal = document.getElementById('modal');
modal.classList.add('active');
modal.setAttribute('aria-hidden', 'false');
modal.querySelector('button').focus(); // Set focus to the close button
}
function closeModal() {
const modal = document.getElementById('modal');
modal.classList.remove('active');
modal.setAttribute('aria-hidden', 'true');
document.getElementById('open-modal').focus(); // Return focus to the trigger button
}
// Trap focus within modal
function trapFocus(event) {
const modal = document.getElementById('modal');
const focusableElements = modal.querySelectorAll('button, a, input, textarea');
const firstElement = focusableElements[0];
const lastElement = focusableElements[focusableElements.length - 1];
if (event.key === 'Tab') {
if (event.shiftKey && document.activeElement === firstElement) {
event.preventDefault();
lastElement.focus();
} else if (!event.shiftKey && document.activeElement === lastElement) {
event.preventDefault();
firstElement.focus();
}
}
}
</script>
</head>
<body>
<button id="open-modal" onclick="openModal()" class="focusable">Open Modal</button>
<!-- Modal Dialog -->
<div id="modal" class="modal" role="dialog" aria-hidden="true" aria-labelledby="modal-title" onkeydown="trapFocus(event)">
<h2 id="modal-title">Modal Dialog</h2>
<p>This is a modal dialog.</p>
<button onclick="closeModal()" class="focusable">Close Modal</button>
</div>
</body>
</html>
Bad Practice Example #
No Focus Management in Modal Dialog In this example, the modal opens, but focus remains outside the modal, making it difficult for screen reader and keyboard users to interact with the modal content.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bad Focus Management in Modal</title>
<style>
/* Modal styling */
.modal {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border: 1px solid #ddd;
width: 300px;
}
.modal.active {
display: block;
}
</style>
</head>
<body>
<button onclick="document.getElementById('modal').classList.add('active')">Open Modal</button>
<!-- Modal Dialog -->
<div id="modal" class="modal">
<h2>Modal Dialog</h2>
<p>This is a modal dialog, but focus is not managed.</p>
<button onclick="document.getElementById('modal').classList.remove('active')">Close Modal</button>
</div>
</body>
</html>
No Focus Management on Dynamic Content #
In this example, the accordion opens without moving focus to the newly loaded content, creating confusion for screen reader and keyboard users
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bad Focus Management in Accordion</title>
<style>
.accordion-section {
display: none;
}
.accordion-section.active {
display: block;
}
</style>
</head>
<body>
<button onclick="document.getElementById('accordion-content').classList.toggle('active')">Show More</button>
<div id="accordion-content" class="accordion-section">
<p>This content is loaded dynamically, but focus is not managed.</p>
</div>
</body>
</html>
Best Practices #
- Move Focus to Newly Opened Content: When opening modals, accordions, or other dynamic sections, move focus to the new content to notify users immediately.
- Trap Focus Within Modals: In modal dialogs, trap focus within the modal until it is closed, then return focus to the element that triggered the modal.
- Return Focus After Closing: When dynamic content closes, return focus to the element that triggered the action to maintain logical navigation.
- Use ARIA Attributes: Use attributes like
aria-expandedfor expandable elements to communicate state changes to assistive technologies.