How to Create Floating Button in Tailwind CSS?
Last Updated :
23 Jul, 2025
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
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read
CSS Introduction CSS (Cascading Style Sheets) is a language designed to simplify the process of making web pages presentable.It allows you to apply styles to HTML documents by prescribing colors, fonts, spacing, and positioning.The main advantages are the separation of content (in HTML) and styling (in CSS) and the
4 min read
CSS Syntax CSS is written as a rule set, which consists of a selector and a declaration block. The basic syntax of CSS is as follows:The selector is a targeted HTML element or elements to which we have to apply styling.The Declaration Block or " { } " is a block in which we write our CSS.HTML<html> <h
2 min read
CSS Selectors CSS Selectors are used to target HTML elements on your pages, allowing you to apply styles based on their ID, class, type attributes, and more. There are mainly 5 types of selectors.Basic CSS Selectors: These are used to target elements by tag, .class, or # ID for fundamental styling needs.Combinato
7 min read
CSS Comments CSS comments are used to add notes or explanations to your code, helping you and others understand it better. They start with /* and end with */ and can be used for both single-line and multi-line comments. Note: Comments are ignored by browsers, so they wonât affect how your webpage looks or works.
2 min read
CSS Colors CSS colors are used to set the color of different parts of a webpage, like text, background, and borders. This helps make the page look more attractive and easier to read. You can define colors using names, hex codes, RGB values, and more.You can try different formats of colors here- #content-iframe
5 min read
CSS Borders Borders in CSS are used to create a visible outline around an element. They can be customized in terms ofWidth: The thickness of the border.Style: The appearance of the border (solid, dashed, dotted, etc.).Color: The color of the border.You can try different types of borders here- #custom-iframe{ he
5 min read
CSS Margins CSS margins are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and readability.Syntax:body { margin: value;}HTML<html> <head>
4 min read
CSS Height and Width Height and Width in CSS are used to set the height and width of boxes. Their values can be set using length, percentage, or auto.Width and HeightThe width and height properties in CSS are used to define the dimensions of an element. The values can be set in various units, such as pixels (px), centim
4 min read
CSS Outline CSS outline is a property used to draw a line around an element's border. It does not affect the layout, unlike borders. It's often used to highlight elements, providing a visual emphasis without altering the dimensions of the element.Syntaxselector{ outline: outline-width outline-type outline-color
4 min read