Skip to Content

How to make a share button HTML?

How to make a share button HTML?

Sharing content is an essential part of the social web. Adding a share button to your website allows visitors to easily distribute your content across their social networks. This helps increase traffic and engagement. In this comprehensive guide, we’ll walk through how to create a share button using HTML.

What is a share button?

A share button is a web element that lets users broadcast content to their social networks. Clicking the button opens a sharing dialog box where the user can select which platform to share on before posting the content.

Share buttons are commonly found on blogs, news sites, and other publishers who rely on social media for traffic growth. They provide an easy way for readers to spread content they like.

Why add a share button to your website

Here are some of the key benefits of having a share button:

  • Increases reach – Share buttons make it easy for users to share your content to their network. This expands the reach of your content and brings new visitors from social platforms.
  • Drives traffic – Social shares can drive a significant amount of traffic to your site. Adding a share button increases the visibility which leads to more clicks.
  • Improves engagement – Share buttons encourage users to interact with your content. Instead of passively consuming, they actively participate by spreading it.
  • Builds credibility – High social sharing numbers signal that your content is popular and trusted. This improves your site’s reputation.
  • Grows your audience – When users share your content, it puts you in front of new audiences and potential followers.

In short, adding a share button provides huge benefits for improving traffic, engagement and discoverability. It’s a must-have feature for publishers looking to utilze social media.

Types of share buttons

There are a few common types of share button implementations:

1. Single Network Share Button

A button that shares to one specific social network like Facebook or Twitter. For example:

These are simple to implement but limit sharing options for your users.

2. Social Sharing Bar

A horizontal bar with icons for the top social platforms like Facebook, Twitter, Pinterest etc. Clicking each button shares to that network. For example:

Social sharing bars provide visitors with multiple sharing options. But take up valuable space on the page.

3. Popup Share Dialog

A share button that triggers a popup containing icons for sharing networks. The user clicks the network they want to share on. For example:

The popup dialog hides the sharing options until the user clicks to open it. This saves space while still providing sharing flexibility.

Factors to consider when adding a share button

Here are some things to keep in mind when implementing your share button:

  • Placement – test different positions like above title, middle of post, bottom, floating sidebar etc.
  • Creatives – try different button colors, sizes, shapes and copy text (e.g. “share” vs “spread”)
  • Sharing networks – stick to the most popular platforms your audience uses.
  • Popup or dedicated buttons – test what drives more clicks and shares for your site.
  • Mobile optimization – ensure the share button displays well on mobile devices.
  • UX flow – the share action should be seamless for users.

Try different implementations and run A/B tests to determine what works best for conversions.

How to make a share button in HTML

Let’s go over how to code a share button using HTML. We’ll break it down into a few key steps:

1. Create the HTML Button

Use the HTML <button> tag to create the share button element:

<button class="share-btn">Share</button>

This will display a basic button with the text “Share”. The class attribute allows styling the button with CSS.

2. Add Share Button Styling

Next, style the share button using CSS:

.share-btn {
  padding: 10px 15px;  
  background: #007bff;
  color: #fff;
  border: none;  
  border-radius: 3px;
  cursor: pointer;
}

This adds padding, blue background color, white text color, rounded corners and pointer cursor.

3. Trigger a Share Popup

When clicked, the share button should trigger a popup dialog box. This can be achieved by adding a onclick attribute to the button:

<button class="share-btn" onclick="toggleVisibility('share-dialog')">Share</button>

The onclick runs a JavaScript function to show/hide the popup dialog.

4. Create the Share Popup

Next, build out the share dialog popup and hide it with CSS:

<div id="share-dialog" class="share-dialog">
  <a href="https://facebook.com/share">Facebook</a>
  <a href="https://twitter.com/share">Twitter</a>
  <a href="mailto:">Email</a> 
</div>

<style>
  .share-dialog {
    display: none;
    background: #fff; 
    padding: 15px;
    border-radius: 5px;
    position: absolute;
    right: 0;
    top: 50px;
  }  
</style>

This creates a div containing the share links and uses CSS to hide it on page load.

5. Toggle Popup Visibility

Finally, use JavaScript to toggle the visibility of the popup when the button is clicked:

<script>
  function toggleVisibility(id) {
    let e = document.getElementById(id);
    if(e.style.display == 'block')
      e.style.display = 'none';
    else
      e.style.display = 'block';
  }  
</script>

This toggles between display: none and display: block to show/hide the element.

And that covers the basics of creating a popup share dialog in HTML! The key steps are:

  1. Add HTML button
  2. Style button with CSS
  3. Trigger popup onclick
  4. Build out share dialog
  5. Show/hide popup with JavaScript

With these fundamentals, you can build out and customize the share experience further with additional styling and functionality enhancements.

How to customize and optimize your share button

Let’s go over some ways to enhance your share button implementation for better conversions:

Add share counts

Display social share counts next to each network icon. This signals popularity and encourages more sharing.

Facebook (32k)
Twitter (18k)

Use social meta tags

Meta tags allow customizing how content appears when shared on social networks:

<meta property="og:title" content="Page Title">
<meta property="og:description" content="Page description">
<meta property="og:image" content="image.png">

Animate on click

Use CSS animations to make the share popup slide in or fade in:

.share-dialog {
  animation: fadeIn 0.5s;
}

@keyframes fadeIn {
  from {opacity: 0;}
  to {opacity: 1;}
}

Sticky share bar

Make the share bar stick to the screen as the user scrolls using position: sticky:

.share-bar {
  position: sticky;
  top: 20px; 
}

Preview content

Dynamically pull in title, description and image content to preview the share:

<script>
  let title = document.title;
  let desc = document.querySelector("meta[name='description']").getAttribute('content');
  let img = document.querySelector("meta[property='og:image']").getAttribute('content');

  document.getElementById("share-preview").innerHTML = 
  `<b>${title}</b>
   ${desc}
   <img src="${img}" width="120">`; 
</script>

Share follow buttons

Include buttons for following your social profiles to build your audience:

<a href="https://facebook.com/yourpage">Like on Facebook</a>
<a href="https://twitter.com/yourprofile">Follow on Twitter</a>

Responsive design and mobile optimization

It’s important to ensure your share button displays well on mobile devices. Here are some tips:

  • Use relative sizing like rem units for padding/margins.
  • Size share icons appropriately so they are easy to tap.
  • Test tap targets for mobile friendliness.
  • Style the share popup for smaller viewports.
  • Hide share counts on small screens to save space.
  • Ensure share functionality works on mobile browsers.

Mobile usage continues to grow globally, so optimizing the share experience for mobile visitors is essential for performance.

Integrating with social share APIs

For enhanced share counts and insights, integrate with social platforms’ built-in APIs like:

  • Facebook Graph API – get share counts and interactions for a URL.
  • Twitter Counts API – retrieve tweet and retweet stats for content.
  • Pinterest Tag API – track repins, clicks and impressions for Pins.
  • Google URL Shortener API – create short link versions of URLs to share.

These APIs allow pulling dynamic social data directly into your share interface. However, they require authorization and coding to connect with server-side languages.

Tracking social shares

To better understand the impact of your share button, track key analytics events like:

  • Share button clicks – number of times users click the share button.
  • Social shares – number of times content is shared to social networks.
  • Popular sharing channels – which social platforms get the most shares.
  • Pages shared – which content is shared the most.
  • Share locations – where on the page users click to share.

This provides insights to refine your sharing strategy and boost social activity.

Conclusion

Adding a share button is a highly effective way to boost user engagement and increase reach for your website content. With the html basics covered in this guide, you can quickly build and implement share buttons using just HTML, CSS and JavaScript.

Some key takeaways include:

  • Use clean HTML and CSS for coding the button and popup.
  • Utilize JavaScript to toggle popup visibility.
  • Optimize for responsiveness across devices.
  • Enhance with share counts, previews and follow buttons.
  • Leverage social APIs for rich data-driven sharing.
  • Track analytics for actionable insights.

With these tips and code samples, you have everything you need to start boosting social sharing of your content with custom HTML share buttons.