Pagination Links

Demo

				
					
<div class="pagination-container">
  <div class="results">
    <!-- Results will be dynamically generated here -->
  </div>
  <div class="pagination">
    <ul role="tablist" aria-label="Pagination">
      <li role="presentation"><a aria-label="navigate to results page 1" href="#" role="tab" aria-selected="true">1</a></li>
      <li role="presentation"><a aria-label="navigate to results page 2" href="#" role="tab">2</a></li>
      <li role="presentation"><a aria-label="navigate to results page 3" href="#" role="tab">3</a></li>
      <li role="presentation"><a aria-label="navigate to results page 4" href="#" role="tab">4</a></li>
    </ul>
  </div>
</div>
				
			
				
					
@import url("https://fonts.googleapis.com/css2?family=Barlow&family=Oswald&display=swap");

.pagination-container {
  width: 75%;
  font-family: 'Barlow', sans-serif;
}

.pagination ul {
  list-style: none;
  display: flex;
  justify-content: center;
  padding: 0;
}

.pagination li {
  margin: 0 5px;
}

.pagination a {
  font-family: 'Oswald', sans-serif;
  text-decoration: none;
  color: #005e86;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.pagination a:hover {
  background-color: #99b4c0;
}

.pagination a[aria-selected="true"] {
  background-color: #211224;
  color: white;
}

.results {
  width: 75%;
  margin: auto;
  border: 2px #211224 solid;
  border-radius: 10px;
}

.results h3, .results a, .results p {
  margin-left: 15px;
}

.pagination {
  margin-top: 40px;
}
				
			
				
					
const resultsContainer = document.querySelector('.results');
const paginationLinks = document.querySelectorAll('.pagination a');

// Function to generate example results
function generateResults(pageNumber) {
  // Simulating results for the page
  const results = [];
  for (let i = 1; i <= 5; i++) {
    results.push({
      title: `Example Result ${i + (pageNumber - 1) * 5}`,
      text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
      link: '#'
    });
  }
  return results;
}

// Function to display results
function displayResults(pageNumber) {
  resultsContainer.innerHTML = '';
  const results = generateResults(pageNumber);
  results.forEach(result => {
    const resultElement = document.createElement('div');
    resultElement.innerHTML = `
      <h3><a href="${result.link}">${result.title}</a></h3>
      <p>${result.text}</p>
    `;
    resultsContainer.appendChild(resultElement);
  });
}

// Event listeners for pagination links
paginationLinks.forEach((link, index) => {
  link.addEventListener('click', event => {
    event.preventDefault();
    // Update aria-selected attribute for all links
    paginationLinks.forEach((link) => link.setAttribute('aria-selected', 'false'));
    // Set aria-selected attribute to 'true' for the clicked link
    link.setAttribute('aria-selected', 'true');
    // Display results for the selected page
    displayResults(index + 1);
  });
});

// Set aria-selected attribute on the first pagination link initially
paginationLinks[0].setAttribute('aria-selected', 'true');

// Initial display of results for the first page
displayResults(1);
				
			

Why use pagination links?

Pagination links in web development serve as navigational aids, breaking down content into manageable segments and allowing users to navigate through various pages of information, search results, or listings. They provide a structured and organized way to access content, preventing information overload by displaying a limited set of items per page. However, considerations for accessibility are pivotal when implementing pagination links. Users relying on assistive technologies or those with motor impairments might face challenges when interacting with pagination. Accessibility concerns revolve around ensuring keyboard operability, providing clear and descriptive link text, and conveying the pagination’s structure and current location to screen reader users. Incorporating appropriate HTML markup, utilizing ARIA attributes for conveying roles and relationships, and designing for keyboard navigation aid in creating accessible pagination links.

What are some accessibility concerns?

  1. Keyboard Accessibility:
    • Concern: Users relying on keyboards may face difficulties navigating pagination links.
    • Mitigation: Ensure pagination links are keyboard accessible by allowing users to navigate and activate them using keyboard controls (e.g., Tab and Enter). Use proper HTML semantics (<a> tags) and ensure focus visibility on active links.
  2. Focus Indication:
    • Concern: Inadequate focus indication may confuse users with visual impairments regarding the active link.
    • Mitigation: Ensure clear and visible focus styles on active pagination links, providing a distinct visual cue for users navigating with keyboards or screen readers.
  3. Consistent Identification:
    • Concern: Users might struggle to understand the pagination’s purpose and sequence.
    • Mitigation: Provide clear and consistent labeling for pagination links. Use ARIA attributes (role=”navigation”, aria-label, or aria-labelledby) to convey the pagination’s purpose and sequence to assistive technologies.
  4. Parsing and Comprehension:
    • Concern: Screen reader users might not comprehend the relationship between pagination links and the content structure.
    • Mitigation: Use semantic HTML markup (<nav>, <ul>, <li>) to structure pagination links. Ensure proper labeling of the links and convey their association with the content using ARIA attributes or landmarks.
  5. Responsive Design and Reflow:
    • Concern: Pagination links might not adapt well to different screen sizes or orientations.
    • Mitigation: Implement responsive design principles, ensuring that pagination links are accessible and usable across various devices and screen sizes without loss of functionality or information.
Tested using
Chrome
with NVDA
Firefox
with NVDA
  • W3C SVG
    1.3.1
    Info and Relationships ( level A ) :
    Information, structure, and relationships conveyed through presentation can be programmatically determined or are available in text.
  • W3C SVG
    2.1.1
    Keyboard ( level A ) :
    All functionality of the content is operable through a keyboard interface without requiring specific timings for individual keystrokes, except where the underlying function requires input that depends on the path of the user's movement and not just the endpoints.
  • W3C SVG
    2.4.7
    Focus Visible ( level AA ) :
    Any keyboard operable user interface has a mode of operation where the keyboard focus indicator is visible.
  • W3C SVG
    4.1.2
    Name, Role, Value ( level A ) :
    For all user interface components (including but not limited to: form elements, links and components generated by scripts), the name and role can be programmatically determined; states, properties, and values that can be set by the user can be programmatically set; and notification of changes to these items is available to user agents, including assistive technologies.