Custom Social Media Share Buttons: A Step-by-Step Guide

Social media share

Table of Contents

Blog Title: Creating a Custom Social Media Share Button for Your Website

Introduction

In today’s digital age, social media sharing is a powerful tool to drive traffic and engagement on your website. Custom social media share buttons can make sharing content seamless and visually appealing, encouraging visitors to spread the word about your content. This tutorial will guide you through the process of creating custom social media share buttons using HTML, CSS, and JavaScript.

Why Custom Share Buttons?

While there are many plugins available for adding social media buttons, creating custom buttons gives you control over their design and functionality. Custom buttons can be styled to match your brand, improve loading times by avoiding third-party scripts, and enhance user experience with tailored interactions.

Requirements

  • Basic knowledge of HTML, CSS, and JavaScript
  • A text editor (e.g., VSCode, Sublime Text)
  • Access to your website’s code

Step 1: HTML Structure

First, create the HTML structure for your share buttons. In your HTML file, add the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Share Buttons</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="share-buttons">
        <button class="share-btn" id="facebook-share">Share on Facebook</button>
        <button class="share-btn" id="twitter-share">Share on Twitter</button>
        <button class="share-btn" id="linkedin-share">Share on LinkedIn</button>
    </div>
    <script src="scripts.js"></script>
</body>
</html>

Step 2: Styling with CSS

Next, style your buttons with CSS. In your styles.css file, add the following code:

body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f4f4f4;
}

.share-buttons {
    display: flex;
    gap: 10px;
}

.share-btn {
    padding: 10px 20px;
    border: none;
    cursor: pointer;
    font-size: 16px;
    border-radius: 5px;
    transition: background-color 0.3s;
}

#facebook-share {
    background-color: #3b5998;
    color: white;
}

#facebook-share:hover {
    background-color: #2d4373;
}

#twitter-share {
    background-color: #1da1f2;
    color: white;
}

#twitter-share:hover {
    background-color: #0c85d0;
}

#linkedin-share {
    background-color: #0077b5;
    color: white;
}

#linkedin-share:hover {
    background-color: #005582;
}

Step 3: Adding JavaScript Functionality

Now, let’s add functionality to these buttons. In your scripts.js file, add the following code:

document.getElementById('facebook-share').addEventListener('click', function() {
    const url = encodeURIComponent(window.location.href);
    const shareUrl = `https://www.facebook.com/sharer/sharer.php?u=${url}`;
    window.open(shareUrl, '_blank');
});

document.getElementById('twitter-share').addEventListener('click', function() {
    const url = encodeURIComponent(window.location.href);
    const text = encodeURIComponent(document.title);
    const shareUrl = `https://twitter.com/intent/tweet?url=${url}&text=${text}`;
    window.open(shareUrl, '_blank');
});

document.getElementById('linkedin-share').addEventListener('click', function() {
    const url = encodeURIComponent(window.location.href);
    const shareUrl = `https://www.linkedin.com/sharing/share-offsite/?url=${url}`;
    window.open(shareUrl, '_blank');
});

This JavaScript code sets up event listeners on each button, which open the corresponding social media sharing URL in a new tab.

Conclusion

Creating custom social media share buttons can enhance the user experience and ensure that the buttons align with your website’s branding. This tutorial provided a simple way to implement these buttons using HTML, CSS, and JavaScript. Customize the styles further to match your site’s design, and encourage your visitors to share your content effortlessly.

Try
RecurPost

Schedule and Publish your posts on multiple social accounts, read and reply to incoming messages with social inbox, and collaborate with your team and clients with ease.

Scroll to Top