HTML5 Zone is brought to you in partnership with:

Paulund is a website dedicated to writing tutorials and code snippets about Web Development, the main subjects are PHP, Wordpress, jQuery, CSS3 and HTML5. Paul is a DZone MVB and is not an employee of DZone and has posted 124 posts at DZone. You can read more from them at their website. View Full User Profile

How To Create A CSS Animated Share Button

10.30.2012
| 3316 views |
  • submit to reddit

Every website now has social network share buttons on their site, if you don't have them then you really should get one. They're a great way to give your visitors easy access to share your content with their friends and followers.

The thing with social network share buttons is that they all look the same on every website, you can take this two ways. It's either a good thing because people will recognise them and understand how to use them or it's a bad thing because now your website looks the same as every other website on the internet.

In this tutorial we are going to create a share button which uses CSS animation to display the links to share the current page on Facebook, Twitter, Google Plus and Pinterest.

First we are going to start off creating the HTML which will display some text such as Share This Page. Then when we hover over this text we want to use CSS animation to display the share buttons.

View the demo to see what we are going to create.

Demo

The HTML

The HTML we are going to use has many different sections which we are splitting up using different HTML elements all of this is going to be wrapped up in the div with a class of share_button.

<div class="share_button">
</div>

The button is going to open up in the middle to display the share buttons so we are going to add 2 sections which we will use to split the text.

<div class="share_button">
<section>
<article>
<h1>Share th</h1>
</article>
</section>
<section>
<article>
<h1>is Page</h1>
</article>
</section></div>

 

Within these 2 sections we are going to show or hide the article tags to display the share buttons. In this example there will be 2 share buttons on each article tag, totally 4 share buttons which will be displayed.

Below is the full HTML code with all of the social buttons in the different sections.

Full HTML Code

The below code is to be used on a WordPress single.php page as it uses the WordPress function the_permalink(). If you are not using this on WordPress remove this function and replace it with the URL you want to share.

<div class="share_button">
<section>
<article>
<h1>Share th</h1>
</article>
<article>
<h2><a href="http://www.facebook.com/sharer.php?u=<?php the_permalink(); ?>" title="Share On Facebook">F</a></h2>
<h2><a href="http://twitter.com/home?status=Share On Twitter <?php the_permalink(); ?>" title="Share On Twitter">T</a></h2>
</article>
</section>
<section>
<article>
<h1>is Page</h1>
</article>
<article>
<h2><a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" title="Share On Google Plus">G</a></h2>
<h2><a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink(); ?>" title="Share On Pinterest">P</a></h2>
</article>
</section></div>

The CSS

The CSS will be done in two stages first we will be creating the button then we will be creating the animation to change the look of the sections from displaying the text to displaying the social buttons.

Use the below CSS to create the look of the button.

.share_button { width: 300px;height: 70px; margin:0 auto; }
.share_button section { width: 50%; height: inherit; float: left; }
.share_button section h1 { margin-top: 13%; overflow: hidden; width: 100%;color: #fff;}
.share_button section article { position: absolute; height: inherit; width: 150px; background: #d4d5d9;
	-webkit-transition: all 600ms;
	-moz-transition: all 600ms;
	-o-transition: all 600ms;
	-ms-transition: all 600ms;
	transition: all 600ms;
	text-align: center;
}
.share_button section article h2 { display: inline-block; width: 40%;	height: 40px; overflow: hidden;	margin-top: 10%; cursor: pointer; }
.share_button section article h2:hover { text-shadow:2px 2px 2px #555; }
.share_button section article h2 a { color:#FFF; text-decoration: none; }
.share_button section:first-child article:first-child{ text-align: right; }
.share_button section:last-child article:first-child{ text-align: left; }

With the button created you can add a hover effect to display the social buttons in the background.

.share_button section:first-child article:last-child,
.share_button:hover section:first-child article:first-child {
	-webkit-transform: rotateY(90deg);
	-moz-transform: rotateY(90deg);
	-o-transform: rotateY(90deg);
	-ms-transform: rotateY(90deg);
	transform: rotateY(90deg);
}
.share_button section:last-child article:last-child,
.share_button:hover section:last-child article:first-child {
	-webkit-transform: rotateY(-90deg);
	-moz-transform: rotateY(-90deg);
	-o-transform: rotateY(-90deg);
	-ms-transform: rotateY(-90deg);
	transform: rotateY(-90deg);
}
.share_button:hover section:first-child article:last-child,
.share_button:hover section:last-child article:last-child {
	-webkit-transform: rotateY(0deg);
	-moz-transform: rotateY(0deg);
	-o-transform: rotateY(0deg);
	-ms-transform: rotateY(0deg);
	transform: rotateY(0deg);
}

View the demo to see what has been created with the above code.

Demo

 

 

 

Published at DZone with permission of Paul Underwood, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Chankey Pathak replied on Wed, 2012/10/31 - 3:32am

Very nice. Gonna use it on my site, thanks :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.