- Published on
The Ninja Guide To How To Make A Top button With Tailwind CSS Better

- What is Tailwind CSS?
- The description of Top button ui component
- Why use Tailwind CSS to make a Top button ui component?
- The preview of Top button ui component
- The source code of Top button ui component
- How to make a Top button with Tailwind CSS?
- Install tailwind css of verion 3.0.18
- All the unility class needed to make a Top button component
- 17 steps to make a Top button component with Tailwind CSS
- Conclusion
What is Tailwind CSS?
Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.
The description of Top button ui component
Button used to go back to the top. if the "scroll-smooth" class doesn't work, copy paste this code and run it. it should work"
Why use Tailwind CSS to make a Top button ui component?
- It can make the building process of Top button ui component faster and more easily.
- Enables building complex responsive layouts and components freely.
- Minimum lines of CSS code in Top button component file.
The preview of Top button ui component
Free download of the Top button's source code
The source code of Top button ui component
<html class="scroll-smooth">
<head>
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
</head>
<body>
<!-- first page -->
<section class="flex w-full min-h-screen bg-gray-900 justify-center items-center">
<p class="text-3xl font-bold text-gray-100 text center md:text-6xl">Start Scrolling Down</p>
</section>
<!-- second page -->
<section class="w-full min-h-screen bg-gray-600"></section>
<!-- button -->
<button x-data="topBtn" @click="scrolltoTop" id="topButton"
class="fixed z-10 hidden p-3 bg-gray-100 rounded-full shadow-md bottom-10 right-10 animate-bounce">
<svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M5 10l7-7m0 0l7 7m-7-7v18">
</path>
</svg>
</button>
<script>
document.addEventListener('alpine:init', () => {
Alpine.data('topBtn', () => ({
scrolltoTop() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
}));
});
const topBtn = document.getElementById("topButton");
window.onscroll = () => {
(document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) ?
topBtn.classList.remove("hidden"): topBtn.classList.add("hidden");
}
</script>
</body>
</html>
How to make a Top button with Tailwind CSS?
Install tailwind css of verion 3.0.18
Use the script
html tag to import the script of Tailwind CSS of the version 3.0.18
<script src="https://cdn.tailwindcss.com"></script>
All the unility class needed to make a Top button component
flex
w-full
min-h-screen
bg-gray-900
text-3xl
text-gray-100
md:text-6xl
bg-gray-600
fixed
z-10
hidden
p-3
bg-gray-100
bottom-10
right-10
w-8
h-8
17 steps to make a Top button component with Tailwind CSS
Use
flex
to create a block-level flex container.Use
w-full
to set an element to a 100% based width.Set the minimum width/height of an element using the
min-h-screen
utilities.Control the background color of an element to gray-900 using the
bg-gray-900
utilities.Control the text color of an element to 3xl using the
text-3xl
utilities.Control the text color of an element to gray-100 using the
text-gray-100
utilities.Control the text color of an element to 6xl at only medium screen sizes using the
md:text-6xl
utilities.Control the background color of an element to gray-600 using the
bg-gray-600
utilities.Use
fixed
to position an element relative to the browser window.Control the stack order (or three-dimensional positioning) of an element to 10 in Tailwind, regardless of order it has been displayed, using the
z-10
utilities.Use
hidden
to set an element to display: none and remove it from the page layout.Control the padding on all sides of an element to 0.75rem using the
p-3
utilities.Control the background color of an element to gray-100 using the
bg-gray-100
utilities.Use the
bottom-10
utilities to set the bottom position of a positioned element to 2.5rem.Use the
right-10
utilities to set the right position of a positioned element to 2.5rem.Use
w-8
to set an element to a fixed width(2rem).Use
h-8
to set an element to a fixed height(2rem).
Conclusion
The above is a step-by-step tutorial on how to use Tailwind CSS to make a Top button components, learn and follow along to implement your own components.