Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
css scroll-snap: focusing on the element which got snapped to

I implemented a horizontal grid with some cards in them. The grid uses CSS scroll-snap and it works nicely when navigating with mouse/touchscreen.

The problem occurs when the grid is navigated using a keyboard. Pressing tab after navigating through the grid with arrow keys causes the view to jump back to the element that got the focus, not the card which is current snapped to.

My ideal behaviour when pressing tab is, to focus on the card which is currently snapped to.

Any suggestions to make this possible?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

As far as I can tell there is currently no way to handle this natively. Nils Schwebel's answer is not going to be very elegant, but it looks like the best way to go.

Here's a working example:

Note: I've added quite a bit of pure decoration to make it easier to understand, so you may need to pick out the relevant parts after some testing.

const main = document.getElementById("Main"),
  sections = document.getElementsByClassName("section");

main.addEventListener('scroll', (e) => {
  // Grab the position yo are scrolled to (the top of the viewport)
  let pos = main.scrollTop;
  for (let i = 0, l = sections.length; i < l; i++) {
    // Since our stap-align is centered, get the position of the middle of the viewport relative to the current section's top (if your snap items are not full-height, it might require using half the viewport's height instead)
    let relativePos = sections[i].offsetTop - pos + (sections[i].offsetHeight / 2);
    // Check if the point we found falls within the section
    if (relativePos >= 0 && relativePos < sections[i].offsetHeight) {
      sections[i].focus();
      break;
    }
  }
});
body {
  margin: unset;
}

main {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  justify-content: center;
  width: 100%;
  height: 100vh;
  background-color: #222;
  overflow-y: auto;
  scroll-snap-type: y mandatory;
}

section {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
  scroll-snap-align: center;
}

section:focus {
  border: none;
  outline: none;
}

#s1 {
  background-color: #d72748;
}

#s2 {
  background-color: #b51f7e;
}

#s3 {
  background-color: #e64869;
}

#s4 {
  background-color: #e79946;
}

section h2 {
  color: white;
}
<main id="Main">
  <section class="section" id="s1" tabindex="1" aria-labelledby="a1">
    <h2 id="a1">AREA 1</h2>
  </section>
  <section class="section" id="s2" tabindex="1" aria-labelledby="a3">
    <h2 id="a2">AREA 2</h2>
  </section>
  <section class="section" id="s3" tabindex="1" aria-labelledby="a2">
    <h2 id="a3">AREA 3</h2>
  </section>
  <section class="section" id="s4" tabindex="1" aria-labelledby="a4">
    <h2 id="a4">AREA 4</h2>
  </section>
</main>

about 4 years ago · Juan Pablo Isaza Report

0

I would add a scroll listener and just check if the element is at the top of the scroll view. You may be able to modify one of these solutions: How to check if element is visible after scrolling?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!