How to Create Floating Button in Tailwind CSS?
Last Updated :
07 Oct, 2024
A floating button is a popular UI design feature used to quickly access actions like adding, saving, or navigating to website users. We can easily create a floating button in Tailwind CSS. Tailwind CSS makes it very easy to create a floating button with its utility-first approach, which allows us to style elements directly in your HTML without needing to write custom CSS.
A floating button is a button that stays fixed in a specific position, mainly it is present at the bottom-right corner of the screen, even when the user scrolls the page. It is commonly used in web apps or mobile interfaces to trigger actions like adding a new item, creating a post, or moving to another page.
Prerequisites
Step-By-Step Implementation
Step 1: Create a New Project Folder
First we will create a folder named floating-button-project on our computer system. Inside this folder, we will store all our project files like HTML, CSS, and JavaScript.
Step 2: Install Node.js and npm
- This step is important as we will set up Tailwind CSS in .
- Download Node.js from the official website. After installation:
Open your terminal/command prompt and run the following command to check if Node.js and npm are successfully installed:
node -v
npm -v
Step 3: Initialize a New Project
This is only required when we want to manage dependencies using npm. Now we open a terminal and navigate to your project folder:
cd path-to-your/floating-button
Now, we run the following command:
npm init -y
This creates a package.json file to manage your project dependencies.
Step 4: Install Tailwind CSS
Install Tailwind CSS using npm:
npm install -D tailwindcss
Initializes a Tailwind CSS configuration file (tailwind.config.js) in the root of our project:
npx tailwindcss init
This will create a tailwind.config.js file in our project folder. It allows us to configure and customize Tailwind CSS.
Step 5: Set Up Tailwind CSS
Inside our floating-button-project folder, create a file called styles.css.
Open styles.css and add these lines:
@tailwind base;
@tailwind components;
@tailwind utilities;
Package.json File:
{
"name": "floating-button",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"tailwindcss": "^3.4.13"
}
}
Project Structure:
Project Structure Now we open our terminal and navigate to the project folder. We run the following command:
npx tailwindcss -i ./styles.css -o ./output.css --watch
This watches for changes in styles.css and updates output.css accordingly.
Step 6: Create an HTML File
Inside our project folder, we will create an index.html file and update tailwind.config.js:
HTML
//index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Enhanced Floating Button</title>
<link rel="stylesheet" href="output.css">
<!-- Link the compiled Tailwind CSS file -->
<link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<!-- Link Font Awesome for icons -->
<style>
/* Custom Bounce Animation */
.bounce {
animation: bounce 2s infinite;
}
@keyframes bounce {
0%,
20%,
50%,
80%,
100% {
transform: translateY(0);
}
40% {
transform: translateY(-15px);
}
60% {
transform: translateY(-7px);
}
}
/* Tooltip Styling */
.tooltip {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 100%;
/* Position the tooltip above the button */
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
/* Show Tooltip on Hover */
.floating-btn:hover .tooltip {
visibility: visible;
opacity: 1;
}
</style>
</head>
<body class="bg-gray-100 dark:bg-gray-900
transition-colors duration-500">
<!-- Floating Button with Tooltip -->
<div class="fixed bottom-4 right-4 floating-btn">
<button class="bg-blue-500 hover:bg-blue-600
text-white font-bold py-3 px-4
rounded-full shadow-lg hover:shadow-xl
transition duration-300 ease-in-out
transform hover:scale-110 bounce relative">
<i class="fas fa-plus text-lg"></i>
<!-- Font Awesome icon -->
</button>
<!-- Tooltip -->
<span class="tooltip">
Add Item
</span>
</div>
<!-- Theme Toggle Button -->
<div class="fixed top-4 right-4">
<button id="theme-toggle"
class="bg-gray-800 text-white px-4 py-2 rounded-lg
dark:bg-gray-200 dark:text-black transition duration-300">
Toggle Theme
</button>
</div>
<!-- Script for Theme Toggle -->
<script>
const themeToggle = document.getElementById('theme-toggle');
const body = document.body;
// Toggle between light and dark mode
themeToggle.addEventListener('click', () => {
body.classList.toggle('dark');
});
</script>
</body>
</html>
CSS
/*tailwind.config.js*/
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./index.html",
// Ensure your HTML file is included
"./src/**/*.{html,js}",
// Include all HTML and JS files in src folder if applicable
],
theme: {
extend: {},
},
plugins: [],
}
Step 7: Run the Project
Now, open the index.html file in a browser to see your floating button and theme toggle functionality. Save all the changes to see the output.
Output: Now, we open the index.html file in your browser to see the floating button in action.
OutputConclusion
We follow the given steps above to run the floating button project with Tailwind CSS. We can either use the local setup (via npm) for full control over the tailwind CSS. We can see the output of following code of a floating button in right-down corner point.
Similar Reads
How to Create Floating Label in Tailwind CSS?
A floating window, a "popup window" or "overlay window, " is a UI element that appears on top of a web. It temporarily interrupts the user's interaction with the underlying content to display important messages and information. To create a floating label using Tailwind CSS, you can utilize Tailwind'
2 min read
How to create fading buttons using CSS ?
This article will explore how to create fading buttons using CSS. We can create two types of fading effects in buttons including fadeIn and FadeOut. The CSS property opacity is used to give a fading effect on hovering the button. Fading buttons offer an effective way to provide a pleasing visual eff
3 min read
How to Make Floating Card Effect in Tailwind CSS?
Floating card effect using Tailwind CSS is fun to make and explore. It is basically all about using the classes of Tailwind CSS to create cards when hovered upon have a floating effect, such that they are replaced from their position, either get transformed, or move up or down in their positions. we
2 min read
How to Create Glowing Background Gradient Effects in Tailwind CSS?
Glowing background gradient effects can add a dynamic and visually appealing element to your web design. In Tailwind CSS, achieving this effect involves combining gradient utilities with custom styles. This guide will explore different methods to create glowing gradient effects. These are the follow
2 min read
How to Skew a Button in Tailwind CSS?
Skewed buttons can add a dynamic and modern look to your website, Tailwind CSS provides utility classes to easily apply 2D transformations like tilt, rotation, and scaling, we'll explain how to tilt, buttons using Tailwind CSS and create a simple project to demonstrate this. Approach To Skew A Butto
2 min read
How to Create Animated Loading Button using Tailwind CSS ?
An Animated Loading Button is a button that displays a loading spinner or animation to indicate an ongoing process, such as form submission. Using Tailwind CSS, you can create this by combining utility classes for styling and animations to visually enhance the button during loading. Table of Content
2 min read
How To Create 404 Pages in Tailwind CSS?
Creating a 404 error page using Tailwind CSS is simple and effective. Tailwind CSS allows you to quickly style and layout your page using its utility-first CSS framework. In this article, we'll see the process of designing a custom 404 page with an engaging design to keep users informed and on track
2 min read
How to Create Parallax Effect in Tailwind CSS ?
A parallax website includes fixed images in the background that are kept in place and the user can scroll down the page to see different parts of the image. If you use the parallax effect on our page then it helps the user to interact with the page. Creating Parallax Effect in Tailwind CSS At first
3 min read
Create Animated Scroll-to-Top Button in Tailwind CSS
An animated scroll-to-top button is a user interface element that appears on a webpage when the user scrolls down. It allows the user to quickly return to the top of the page with a single click or tap. This feature is often implemented using CSS animations or transitions, and it can enhance the use
2 min read
How to Fix Button Position in CSS?
Position of elements on a webpage allows us to place elements in any particular position we want. The button is an interactive element that requires a specific placement for better usability. We can place buttons using several Position methods, in CSS each serving different purposes. Below are some
3 min read