Using html. I have a one page app. The nav links link to various sections of the page…
- User
Using html. I have a one page app. The nav links link to various sections of the page with a simple anchor link. e.g.
<a href="#gallery-link">Gallery</a>It works, but the transition is instant and jarring. Is there a way to soften the transition? - Assistant
Yes! You can smoothly scroll to anchor links using a simple CSS property. Modern browsers support the
scroll-behaviorproperty, which makes anchor link navigation smooth and pleasant.How to do it
Add this CSS to your stylesheet or inside a
<style>tag in your HTML:html { scroll-behavior: smooth; }That’s it! Now, when you click on any anchor link (like
<a href="#gallery-link">Gallery</a>), the page will scroll smoothly to the target section instead of jumping instantly.
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Smooth Scroll Example</title> <style> html { scroll-behavior: smooth; } section { height: 100vh; padding: 2em; border-bottom: 1px solid #ccc; } nav { position: fixed; top: 0; background: #fff; width: 100%; padding: 1em; } </style> </head> <body> <nav> <a href="#gallery-link">Gallery</a> <a href="#about-link">About</a> </nav> <section id="gallery-link">Gallery Section</section> <section id="about-link">About Section</section> </body> </html>
Browser Support
- Works in all modern browsers (Chrome, Firefox, Edge, Safari).
- If you need to support very old browsers (like Internet Explorer), you’d need a JavaScript solution. Let me know if you need that!
Summary:
Just addhtml { scroll-behavior: smooth; }to your CSS, and your anchor links will scroll smoothly!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.