Published on 26 July 2024

2 min read

246 words

Smooth Scrolling with Tailwind CSS

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

Tailwind CSSCSS
Cover image for Smooth Scrolling with Tailwind CSS

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:

Terminal window
1
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):

Terminal window
1
<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.

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

Add an element with an id to your website:

Terminal window
1
<div id="top"></div>

Then add a link to that id:

Terminal window
1
<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

Ready to start your project?
Boost your online pressence today.

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.

Our work

When we build websites for our clients, we believe in delivering high quality work that is accessible, performant and easy to use.

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
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
Owner of CW Enterprises

About Us

Owned by a married couple in Houston, TX, we are a small business just like you. We are experienced software engineers with over a decade of industry experience building web applications for companies. We are passionate about helping small business with their technological needs.

Downtown Houston, Texas