I Helped Build the AI That Replaced Meβ: The Meta Layoffs, H-1B Nightmares, and the New AI Arms Race
Let me tell you about a story that has been living rent-free in my head for the past 48 hours. It s...
Read moreLet me show you something interesting. Most tutorials will tell you to install a plugin, add a bloated library, or sign up for yet another SaaS tool just to put a simple WhatsApp chat button on your website. You do not need any of that.
A WhatsApp floating button is just a link wrapped in some CSS. That is it. Everything else is decoration. And you can build the whole thing yourself in about ten minutes with basic HTML, CSS, and maybe three lines of JavaScript.
This guide walks you through exactly how to build one from scratch. No third party dependencies. No external APIs. Just clean code that you own completely.
Before writing any code, you need to understand the foundation. WhatsApp has an official click-to-chat feature that works through a special URL structure. When someone clicks this link, WhatsApp opens directly to a chat with your number.
The format is simple:
https://wa.me/<phone_number>?text=<prefilled_message>
Here is what each part means. The phone number must include the country code. No plus sign, no spaces, no dashes. For a US number, you write 1 then the area code and number. For a UK number, 44 then the rest. The ?text= parameter is optional. If you include it, the message field will be pre-filled with whatever text you URL-encode.
This link format is official and supported by WhatsApp. It does not require the user to save your number as a contact. It works on both mobile and desktop. And it is completely free to use.
A newer, shorter format also exists: wa.me/<number> instead of the older api.whatsapp.com/send?phone= format. Both work, but the wa.me version is cleaner and recommended.
Before coding, decide on a few basic details. Where on the screen do you want the button? Most websites place it in the bottom right corner because that is where users expect chat widgets. Bottom left works too if your design calls for it.
What color should it be? The classic WhatsApp green is #25D366. You can use that or match your brand colors. Just make sure the button is visible against your website's background.
Should it have a message that appears when someone hovers over it? Many sites add a small tooltip that says "Chat with us" or "Message us on WhatsApp". This increases click-through rates because users understand what the button does before clicking.
Will it be always visible or only after scrolling? Most implementations keep it fixed on the screen so users can access it from anywhere on the site.
Open your website's HTML file. You will add the button code somewhere inside the <body> tag, preferably at the end just before the closing </body> tag. This ensures the main content loads first.
Here is the basic structure:
<div class="whatsapp-float">
<a href="https://wa.me/1234567890?text=Hello%21%20I%20have%20a%20question%20about%20your%20products" target="_blank" rel="noopener noreferrer">
<svg width="28" height="28" viewBox="0 0 24 24" fill="white" xmlns="http://www.w3.org/2000/svg">
<path d="M12.032 2.008C6.586 2.008 2.158 6.436 2.158 11.882c0 1.824.496 3.566 1.364 5.056L2.002 22l5.32-1.422c1.462.824 3.134 1.29 4.878 1.29 5.446 0 9.874-4.428 9.874-9.874 0-5.446-4.428-9.874-9.874-9.874z" fill="#25D366"/>
<path d="M12.032 2.008C6.586 2.008 2.158 6.436 2.158 11.882c0 1.824.496 3.566 1.364 5.056L2.002 22l5.32-1.422c1.462.824 3.134 1.29 4.878 1.29 5.446 0 9.874-4.428 9.874-9.874 0-5.446-4.428-9.874-9.874-9.874z" fill="none" stroke="white" stroke-width="1"/>
<path d="M16.856 14.078c-.274-.138-1.626-.804-1.878-.894-.252-.09-.436-.138-.62.138-.184.276-.714.894-.874 1.076-.16.184-.32.206-.594.068-.274-.138-1.156-.426-2.202-1.36-.814-.728-1.364-1.626-1.524-1.9-.16-.276-.018-.426.12-.564.124-.124.276-.32.414-.48.138-.16.184-.276.276-.46.092-.184.046-.344-.024-.482-.07-.138-.62-1.494-.848-2.046-.224-.542-.452-.468-.62-.476-.16-.008-.344-.008-.528-.008-.184 0-.482.068-.734.344-.252.276-.962.94-.962 2.294 0 1.354.986 2.662 1.124 2.846.138.184 1.94 2.962 4.7 4.152.656.282 1.168.45 1.568.576.658.21 1.258.18 1.732.11.53-.078 1.626-.664 1.856-1.306.23-.642.23-1.192.16-1.306-.07-.114-.23-.184-.504-.322z"/>
</svg>
<span class="tooltip-text">Chat with us</span>
</a>
</div>
Replace 1234567890 with your actual WhatsApp number including country code. The example message says "Hello! I have a question about your products". Customize this to match your business.
The target="_blank" opens WhatsApp in a new tab. The rel="noopener noreferrer" is a security best practice for external links.
Now for the styling. This makes the button float in a fixed position and look attractive. Add this CSS to your site's stylesheet or inside a <style> tag in the <head>:
.whatsapp-float { position: fixed; bottom: 20px; right: 20px; z-index: 9999; cursor: pointer;}
.whatsapp-float a { display: flex; align-items: center; justify-content: center; background-color: #25D366; border-radius: 50%; width: 60px; height: 60px; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); transition: all 0.3s ease; text-decoration: none;}
.whatsapp-float a:hover { transform: scale(1.1); background-color: #20b359; box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);}
.tooltip-text { position: absolute; right: 70px; background-color: #333; color: white; padding: 8px 12px; border-radius: 6px; font-size: 14px; font-family: sans-serif; white-space: nowrap; opacity: 0; visibility: hidden; transition: all 0.3s ease; pointer-events: none;}
.whatsapp-float a:hover .tooltip-text { opacity: 1; visibility: visible;}
Let me explain what each section does. The position: fixed keeps the button in the same spot while the user scrolls. The bottom and right values control its distance from the edges. z-index: 9999 ensures it stays above other content.
The button itself uses border-radius: 50% to become a perfect circle. The transition property creates smooth animations. On hover, the button grows slightly and changes to a darker green.
The tooltip sits to the left of the button (right: 70px) and only becomes visible when someone hovers over the button. The pointer-events: none ensures the tooltip does not interfere with clicking.
On mobile devices, a 60 pixel button might be too large or too small. Add a media query to adjust the size for smaller screens:
@media (max-width: 768px) {
.whatsapp-float a {
width: 50px;
height: 50px;
}
.tooltip-text {
font-size: 12px;
padding: 6px 10px;
right: 60px;
}
}
This reduces the button size on tablets and phones while keeping it usable.
Pure CSS works perfectly for most cases. But you might want a few extra features. Here are some optional JavaScript additions.
Show button only after scrolling:
window.addEventListener('scroll', function() { const button = document.querySelector('.whatsapp-float'); if (window.scrollY > 300) { button.style.opacity = '1'; button.style.visibility = 'visible'; } else { button.style.opacity = '0'; button.style.visibility = 'hidden'; }});
Add this to make the button fade in after the user scrolls down 300 pixels. Set the initial CSS to opacity: 0; visibility: hidden; to hide it at the top of the page.
Track clicks with Google Analytics:
document.querySelector('.whatsapp-float a').addEventListener('click', function() { if (typeof gtag !== 'undefined') { gtag('event', 'click', { 'event_category': 'WhatsApp', 'event_label': 'Floating Button' }); }});
This sends an event to Google Analytics whenever someone clicks your WhatsApp button. Useful for tracking how many people initiate chats.
Prefill different messages based on the page:
const currentPage = window.location.pathname;let message = 'Hello! I have a question.';
if (currentPage.includes('/products')) { message = 'I have a question about your products.';} else if (currentPage.includes('/pricing')) { message = 'I would like to know more about pricing.';}
const whatsappLink = document.querySelector('.whatsapp-float a');const encodedMessage = encodeURIComponent(message);whatsappLink.href = `https://wa.me/1234567890?text=${encodedMessage}`;
This changes the pre-filled message depending on which page the user is on. Someone on your products page gets a different message than someone on your pricing page.
Before publishing, test thoroughly. Open your HTML file in a browser. Check that the button appears in the correct position. Hover over it to see the tooltip. Click it. WhatsApp should open with your number and pre-filled message.
Test on different devices. Use Chrome DevTools to simulate mobile screens. Resize your browser window to confirm the responsive styles work.
Test with different browsers. Chrome, Firefox, Safari, and Edge should all display the button correctly. The WhatsApp link will work on any device that has WhatsApp installed. On desktop, it will open WhatsApp Web.
The link does not open WhatsApp. Make sure your phone number is correct. Remove any plus signs, spaces, or dashes. The number should be pure digits with the country code first.
The button overlaps with other content. Check your z-index values. The button needs a higher z-index than any overlapping elements. Also verify that position: fixed is applied correctly.
The tooltip is cut off on mobile. The media query reduces the tooltip size. If it still cuts off, adjust the right value or consider hiding tooltips entirely on mobile devices.
The button appears in the wrong position. Change the bottom and right values. Some websites need bottom: 30px or right: 15px depending on their padding and margins.
Some developers prefer creating the button entirely in JavaScript to keep HTML clean. Here is a self-contained version you can copy and paste anywhere:
(function() {
const phoneNumber = '1234567890';
const message = 'Hello! I have a question.';
const div = document.createElement('div');
div.className = 'whatsapp-float';
div.innerHTML = `
<a href="https://wa.me/${phoneNumber}?text=${encodeURIComponent(message)}" target="_blank" rel="noopener noreferrer">
<svg width="28" height="28" viewBox="0 0 24 24" fill="white" xmlns="http://www.w3.org/2000/svg">
<path d="M12.032 2.008C6.586 2.008 2.158 6.436 2.158 11.882c0 1.824.496 3.566 1.364 5.056L2.002 22l5.32-1.422c1.462.824 3.134 1.29 4.878 1.29 5.446 0 9.874-4.428 9.874-9.874 0-5.446-4.428-9.874-9.874-9.874z" fill="#25D366"/>
<path d="M12.032 2.008C6.586 2.008 2.158 6.436 2.158 11.882c0 1.824.496 3.566 1.364 5.056L2.002 22l5.32-1.422c1.462.824 3.134 1.29 4.878 1.29 5.446 0 9.874-4.428 9.874-9.874 0-5.446-4.428-9.874-9.874-9.874z" fill="none" stroke="white" stroke-width="1"/>
<path d="M16.856 14.078c-.274-.138-1.626-.804-1.878-.894-.252-.09-.436-.138-.62.138-.184.276-.714.894-.874 1.076-.16.184-.32.206-.594.068-.274-.138-1.156-.426-2.202-1.36-.814-.728-1.364-1.626-1.524-1.9-.16-.276-.018-.426.12-.564.124-.124.276-.32.414-.48.138-.16.184-.276.276-.46.092-.184.046-.344-.024-.482-.07-.138-.62-1.494-.848-2.046-.224-.542-.452-.468-.62-.476-.16-.008-.344-.008-.528-.008-.184 0-.482.068-.734.344-.252.276-.962.94-.962 2.294 0 1.354.986 2.662 1.124 2.846.138.184 1.94 2.962 4.7 4.152.656.282 1.168.45 1.568.576.658.21 1.258.18 1.732.11.53-.078 1.626-.664 1.856-1.306.23-.642.23-1.192.16-1.306-.07-.114-.23-.184-.504-.322z"/>
</svg>
<span class="tooltip-text">Chat with us</span>
</a>
`;
const style = document.createElement('style');
style.textContent = `
.whatsapp-float {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 9999;
}
.whatsapp-float a {
display: flex;
align-items: center;
justify-content: center;
background-color: #25D366;
border-radius: 50%;
width: 60px;
height: 60px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
transition: all 0.3s ease;
text-decoration: none;
}
.whatsapp-float a:hover {
transform: scale(1.1);
background-color: #20b359;
}
.tooltip-text {
position: absolute;
right: 70px;
background-color: #333;
color: white;
padding: 8px 12px;
border-radius: 6px;
font-size: 14px;
font-family: sans-serif;
white-space: nowrap;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
pointer-events: none;
}
.whatsapp-float a:hover .tooltip-text {
opacity: 1;
visibility: visible;
}
@media (max-width: 768px) {
.whatsapp-float a { width: 50px; height: 50px; }
.tooltip-text { font-size: 12px; padding: 6px 10px; right: 60px; }
}
`;
document.head.appendChild(style);
document.body.appendChild(div);
})();
Copy this entire block and paste it anywhere in your HTML. It creates the button automatically without adding any markup manually. Just change the phoneNumber and message variables at the top.
Use the correct link format. Always use wa.me/ followed by the number with country code. Avoid the older api.whatsapp.com format as it is deprecated.
Do not store phone numbers in variables that could expose them. If you are using the JavaScript approach, keep the phone number in your code, not in a database that users could manipulate.
Add the noopener noreferrer attribute. This prevents the new tab from having access to your page's window.opener object, which is a security best practice for external links.
Consider using a temporary number for testing. Before deploying to production, test with a test number to ensure everything works correctly. You can change it to your real number afterward.
Respect user privacy. Do not automatically send messages or track users without their consent. The floating button should be passive until clicked.
Building your own button is the right choice for most websites. It is lightweight, free, and gives you complete control. But there are scenarios where a third party tool makes sense.
If you need analytics about who clicked, when they clicked, and what happened after, a service like SleekFlow or similar provides those insights. WhatsApp's native links do not offer analytics.
If you need to send automated replies or manage multiple agents, the WhatsApp Business API becomes necessary. That requires setting up a Meta app and getting approval.
If you need to initiate conversations with customers who have not messaged you first, you must use approved message templates through the WhatsApp Business Platform.
For a simple "chat with us" button that opens WhatsApp when clicked, none of that complexity is required. The wa.me link works perfectly.
A WhatsApp floating button is one of the simplest yet most effective additions to any business website. Customers can reach you instantly without forms, without email threads, without phone trees.
The best part is that you do not need to install anything. No WordPress plugins that slow down your site. No third party scripts that track your users. No monthly subscriptions for basic functionality.
Just a few lines of HTML and CSS. That is it. Your own code that you control completely.
Take the code from this guide. Change the phone number to yours. Adjust the colors if you want. Add it to your website today. Your customers will thank you for making it easy to reach you.
And if you ever need more advanced WhatsApp features like automated responses or multiple agent support, you will understand the foundation well enough to make an informed decision about upgrading. For now, the simple button is all you need.
Let me tell you about a story that has been living rent-free in my head for the past 48 hours. It s...
Read moreI'm going to be honest with you. I've been covering Google I/O for over a decade. I've seen the Goo...
Read moreI need to confess something that might get me kicked out of certain design circles. Last week, I op...
Read moreWhether you need a custom website, SEO optimization, or mobile app β we're here to help.
π¬ Get Free Consultation on WhatsAppOr email us: info@syntaxcrow.com | Call/WhatsApp: +91-956-251-5924