Smooth Scrolling with Tailwind CSS

by Michael G.

2 min read
Featured image for Smooth Scrolling with Tailwind CSS

Learn how to add smooth scrolling to your website using Tailwind CSS without a line of Javascript!

You ever wonder how to add smooth scrolling to links a website? Well I am going to show you how I add them to my websites using only Tailwind CSS. No Javascript required! I am going to assume you have Tailwind CSS installed in your project, so we will skip that part. However, if you need to help on how to install Tailwind CSS, you can check out the official installation guide.

The Javascript Way

The way I normally add smooth scrolling to a website is by writing something simple like this:

const scrollTop = () => window.scrollTo({ top: 0, behavior: 'smooth' })

Then I would just simply call this function when needed with a click event (I'm using React):

<button onClick={scrollTop}>Scroll to Top</button>

It's really easy to do with Javascript but what if you don't want to use Javascript? Well lucky for us, Tailwind CSS has a super simple way to add smooth scrolling to your website without writing any Javascript.

The Tailwind CSS Way

Simple add scroll-smooth to your html element on your website.

<html lang="en" class="scroll-smooth"></html>

Add an element with an id to your website:

<div id="top"></div>

Then add a link to that id:

<a href="#top">Scroll to Top</a>

That's it! You can now smooth scroll to any element with an id on your page.

Resources

Tailwind CSS Scroll Behavior


Michael G.

Michael G.

Michael, together with his wife, own Space City Web Designs. He is a software engineer with years of experience working at companies of various sizes. In his spare time, he likes to code, play video games and travel.

Read all articles by Michael G.

What Our Clients Said

“Mary with Space City Web Designs is amazing! Her communication is outstanding and she makes sure you love everything about your website. If there is anything you don't like, they will fix it immediately! The website is everything I have thought of. Working with Mary was great and I highly recommend Space City Web Designs.”

Whitney Evans avatar
Whitney Evans
Owner of Paw Balm by KW Walkers

“Space City Web Designs was easy to work with. My finished website looked very professional. I am very happy with the end result.”

Jessica Will avatar
Jessica Will
Owner of CW Enterprises
guy on computer with rocket launch in background

Let's start your journey with us

Ready to start your project?

We can make your brand look online without breaking the bank. Your website is the first impression you make on your customers. Make it count.