Why add a share bar?
When visitors share your page with their friends and followers, it brings new people to your site for free. A share bar makes that easy: a small set of buttons that let readers pass your page on with a single click.
In this tutorial we'll build an animated share bar. At first it shows a simple "Share this page" label. When you hover over it, it flips over in 3D to reveal the share buttons underneath.
Hover over the bar below, or press Tab to move to it with the keyboard:
Share or follow? A share bar helps visitors share your page on their own accounts. If you want people to visit your social media profiles instead, that's a "follow us" set of links. The design in this post works for both; only the link addresses change.
Step 1: the HTML
The share bar has three layers: an outer container, a "card" inside it that does the flipping, and two faces on the card, one on the front and one on the back:
<div class="share">
<div class="share__card">
<div class="share__front" aria-hidden="true">Share this page</div>
<div class="share__back">
<a href="mailto:?subject=..." aria-label="Share by email">
<svg ... aria-hidden="true">...</svg>
</a>
<button type="button" class="share__copy" aria-label="Copy link">
<svg ... aria-hidden="true">...</svg>
</button>
<button type="button" class="share__native" aria-label="Share using your device">
<svg ... aria-hidden="true">...</svg>
</button>
</div>
</div>
</div>
| Part | What it's for |
|---|---|
.share | The container. It sets up the 3D space the card flips in. |
.share__card | The part that actually rotates, carrying both faces with it |
.share__front | What visitors see first: the "Share this page" label |
.share__back | The share buttons, revealed when the card flips |
Icons and labels
Each button contains a small inline SVG icon, a drawing written as code that stays sharp at any size and needs no image file. The icons have aria-hidden="true" so screen readers skip them, and each button has an aria-label instead, such as "Share by email". That gives screen reader users (people who have pages read aloud) a clear name for every button.
The front label is marked aria-hidden="true" too. It's a visual cue only; the buttons on the back already describe what they do.
Step 2: setting the 3D stage
CSS can rotate elements in three dimensions, but for the rotation to look realistic, the browser needs to know how far away the viewer is. That's what the perspective property does:
.share {
width: 240px;
height: 60px;
perspective: 800px;
}
.share__card {
position: relative;
height: 100%;
transform-style: preserve-3d;
transition: transform 0.6s ease;
}
perspective: 800pxplaces the viewer 800px away from the screen. Smaller numbers put you closer, making the 3D effect more dramatic; larger numbers make it flatter.transform-style: preserve-3dtells the card to keep its two faces in 3D space, so they turn together like the two sides of a real card.transitionmakes the flip animate smoothly over 0.6 seconds.
Correction: the original code used perspective: 1000 with no unit. Without px, the value is invalid and browsers ignore it, which flattens the 3D effect. Always include the unit.
You can read more about these properties on our transform property and transform-style property pages.
Step 3: the front and back faces
Both faces sit in exactly the same spot, stacked on top of each other. The back face is turned around 180 degrees so it faces away from you, like the back of a playing card:
.share__front,
.share__back {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
border-radius: 12px;
backface-visibility: hidden;
}
.share__front {
background: #1d4ed8;
color: #ffffff;
font-weight: 600;
}
.share__back {
background: #ffffff;
border: 2px solid #bfdbfe;
transform: rotateY(180deg);
}
The key property: backface-visibility
Every element has a back side, and by default you can see through to it, like text showing through thin paper. backface-visibility: hidden hides each face whenever it's turned away from you. Without it, you'd see both faces at once, one of them mirrored.
Here's the same bar with backface-visibility left visible and the two faces slightly see-through. Hover over it and you'll see the mirrored text showing through as it turns:
inset: 0 is shorthand for setting top, right, bottom and left all to 0, so each face fills the card exactly.
Step 4: the flip
Finally, we turn the whole card over when someone hovers over the bar:
.share:hover .share__card,
.share:focus-within .share__card {
transform: rotateY(180deg);
}
rotateY() spins the card around its vertical (Y) axis, like a door on its hinges. Turning it 180 degrees brings the back face round to the front, while the front face turns away and disappears.
The second selector, :focus-within, matches when anything inside the bar has keyboard focus. So when a keyboard user presses Tab to reach the first share button, the bar flips to show it.
What changed since the old version? The original rotated each face separately in opposite directions, with matching transitions on both. Rotating a single card that holds both faces is simpler and keeps them perfectly in sync. It also removed the need for the Internet Explorer workaround mentioned in the original post.
Styling the buttons
The buttons on the back are round, with a pale background that fills with colour on hover:
.share__back a,
.share__back button {
display: grid;
place-items: center;
width: 40px;
height: 40px;
padding: 0;
border: 0;
border-radius: 50%;
background: #eff6ff;
color: #1d4ed8;
cursor: pointer;
transition: background-color 0.2s ease, color 0.2s ease;
}
.share__back a:hover,
.share__back button:hover {
background: #1d4ed8;
color: #ffffff;
}
.share__back a:focus-visible,
.share__back button:focus-visible {
outline: 3px solid #f59e0b;
outline-offset: 2px;
}
The SVG icons use stroke="currentColor", which means "draw in the current text colour". So when the button's color changes to white on hover, the icon changes with it.
Making the share buttons work
The flip is all CSS, but the buttons need real destinations. Here are the most useful options.
Share by email
A mailto: link opens the visitor's email app with a message ready to send. Add a subject and body after a question mark:
<a href="mailto:?subject=A%20page%20worth%20reading&body=https%3A%2F%2Fexample.com%2Fpage"
aria-label="Share by email">...</a>
Spaces and special characters must be encoded, which means swapped for codes like %20 for a space. JavaScript's encodeURIComponent() function does this for you.
Copy the link
A "copy link" button lets people paste your address anywhere: a chat, a forum or a social post. A few lines of JavaScript copy the current page address and confirm it worked:
<script>
document.querySelector('.share__copy').addEventListener('click', function () {
navigator.clipboard.writeText(location.href);
this.setAttribute('aria-label', 'Link copied');
});
</script>
Use the device's own share menu
The Web Share API opens the share menu built into the visitor's phone or computer, listing every app they have installed: messaging apps, email and their social networks. One button covers them all:
<script>
const shareButton = document.querySelector('.share__native');
if (navigator.share) {
shareButton.addEventListener('click', function () {
navigator.share({ title: document.title, url: location.href });
});
} else {
shareButton.hidden = true; // hide it where it isn't supported
}
</script>
Good to know: the Web Share API works on most phones and in many desktop browsers, but not all. The code above hides the button where it isn't available, so visitors never see a button that does nothing.
Sharing to specific social networks
If you'd like buttons for particular networks, each one has a special web address that opens a share window. Replace PAGE_URL with your page's encoded address:
| Network | Share address |
|---|---|
https://www.facebook.com/sharer/sharer.php?u=PAGE_URL | |
| X (Twitter) | https://x.com/intent/post?url=PAGE_URL&text=TITLE |
https://www.linkedin.com/sharing/share-offsite/?url=PAGE_URL | |
https://pinterest.com/pin/create/button/?url=PAGE_URL&media=IMAGE_URL |
For the icons, use each network's official logo, downloaded from its brand or press resources page. These pages also explain how the logos may be used, such as minimum sizes and colours. Drop the SVG into the button in place of our generic icons.
Tip: social networks change over time. Google+, which appeared in the original version of this post, closed in 2019, so its links stopped working. It's worth checking your share links every so often.
Touch screens, keyboards and motion
Touch screens can't hover
Phones and tablets have no mouse pointer, so a bar that only flips on hover could leave the buttons hidden for phone users. The hover media query detects devices that can't hover. On those, we simply show the back face all the time:
@media (hover: none) {
.share__card {
transform: rotateY(180deg);
transition: none;
}
}
This is what touch screen users see, with the buttons ready to tap straight away:
Keyboard users
Because the buttons are real links and buttons, keyboard users can reach them with Tab. The :focus-within rule from step 4 flips the bar as soon as focus arrives, and the :focus-visible outline shows which button is selected.
Reduced motion
A spinning 3D flip can be uncomfortable for people sensitive to motion. Respect their device setting by making the change instant:
@media (prefers-reduced-motion: reduce) {
.share__card {
transition: none;
}
}
The complete code
Here's all the CSS together:
.share {
width: 240px;
height: 60px;
perspective: 800px;
font-family: system-ui, sans-serif;
}
.share__card {
position: relative;
height: 100%;
transform-style: preserve-3d;
transition: transform 0.6s ease;
}
.share:hover .share__card,
.share:focus-within .share__card {
transform: rotateY(180deg);
}
.share__front,
.share__back {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
border-radius: 12px;
backface-visibility: hidden;
}
.share__front {
background: #1d4ed8;
color: #ffffff;
font-weight: 600;
}
.share__back {
background: #ffffff;
border: 2px solid #bfdbfe;
transform: rotateY(180deg);
}
.share__back a,
.share__back button {
display: grid;
place-items: center;
width: 40px;
height: 40px;
padding: 0;
border: 0;
border-radius: 50%;
background: #eff6ff;
color: #1d4ed8;
cursor: pointer;
transition: background-color 0.2s ease, color 0.2s ease;
}
.share__back a:hover,
.share__back button:hover { background: #1d4ed8; color: #ffffff; }
.share__back a:focus-visible,
.share__back button:focus-visible { outline: 3px solid #f59e0b; outline-offset: 2px; }
@media (hover: none) {
.share__card { transform: rotateY(180deg); transition: none; }
}
@media (prefers-reduced-motion: reduce) {
.share__card { transition: none; }
}
And the three icons used in the buttons, ready to paste inside each link or button:
<!-- Email -->
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 6 9-6"/></svg>
<!-- Copy link -->
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10 13a5 5 0 0 0 7.5.5l3-3a5 5 0 0 0-7-7l-1.7 1.7"/><path d="M14 11a5 5 0 0 0-7.5-.5l-3 3a5 5 0 0 0 7 7l1.7-1.7"/></svg>
<!-- Share -->
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><path d="M8.6 13.5l6.8 4M15.4 6.5l-6.8 4"/></svg>
Wrapping up
With perspective, transform-style: preserve-3d and backface-visibility, a simple label flips over to reveal your share buttons. Add :focus-within for keyboard users, show the buttons straight away on touch screens, and use email, copy link and the device share menu for buttons that work everywhere.