Create GitHub-Style Buttons with CSS and jQuery, MooTools, or Dojo JavaScript

I'm what you would consider a bit of a GitHub fanboy. We all know that GitHub is the perfect place to store repositories of open source code, but I think my love of GitHub goes beyond that. GitHub seems to understand that most repo sites are usually boring so they've spiced their site up with some catchy CSS and great JavaScript features. One tiny piece of the GitHub design I love are the basic buttons. Lets examine how we can create our own GitHub-style buttons with a bit of HTML, CSS, and JavaScript.
The HTML
<!-- button 1:"plain" --> <a href="javascript:;" class="minibutton"><span>Basic Button</span></a> <!-- button 2:"icon" --> <a href="javascript:;" class="minibutton btn-download"><span><span class="icon"></span>Button With Icon</span></a>
Below are two styles of buttons: one basic button and one with an icon to the left of the text.
The CSS
/* button basics */
a.minibutton {
display:inline-block;
height:23px;
padding:0 0 0 3px;
font-size:11px;
font-weight:bold;
color:#333;
text-shadow:1px 1px 0 #fff;
background:url(http://github.com/images/modules/buttons/minibutton_matrix.png) 0 0 no-repeat;
white-space:nowrap;
border:none;
overflow:visible;
cursor:pointer;
text-decoration:none;
}
a.minibutton>span {
display:block;
height:23px;
padding:0 10px 0 8px;
line-height:23px;
background:url(http://github.com/images/modules/buttons/minibutton_matrix.png) 100% 0 no-repeat;
}
a.minibutton:hover, a.minibutton:focus {
color:#fff;
text-decoration:none;
text-shadow:-1px -1px 0 rgba(0,0,0,0.3);
background-position:0 -30px;
}
a.minibutton:hover>span, a.minibutton:focus>span {background-position:100% -30px;}
a.minibutton.mousedown{background-position:0 -60px; }
a.minibutton.mousedown>span{background-position:100% -60px; }
/* with icon */
a.btn-download .icon {
float:left;
margin-left:-4px;
width:18px;
height:22px;
background:url(http://github.com/images/modules/buttons/minibutton_icons.png?v20100306) 0 0 no-repeat;
}
a.btn-download .icon {background-position:-40px 0;}
a.btn-download:hover .icon, a.btn-download:focus .icon {background-position:-40px -25px;}
The CSS is bountiful but it usually is when trying to achieve perfection. And just like most buttons, the GitHub button is an A element with a series of SPAN elements inside of it. You'll also note more CSS code is needed to accommodate for the button...obviously.
The MooTools, Dojo, or jQuery JavaScript
/* MooTools (FTW) */
window.addEvent('domready',function() {
$$('a.minibutton').addEvents({
mousedown: function() {
this.addClass('mousedown');
},
blur: function() {
this.removeClass('mousedown');
},
mouseup: function() {
this.removeClass('mousedown');
}
});
});
/* Dojo Toolkit */
dojo.addOnLoad(function() {
var buttons = dojo.query('a.minibutton');
buttons.connect('onmousedown',function() { dojo.addClass(this,'mousedown'); });
buttons.connect('onblur',function() { dojo.removeClass(this,'mousedown'); });
buttons.connect('onmouseup',function() { dojo.removeClass(this,'mousedown'); });
});
/* jQuery */
jQuery.ready(function() {
jQuery('a.minibutton').bind({
mousedown: function() {
jQuery(this).addClass('mousedown');
},
blur: function() {
jQuery(this).removeClass('mousedown');
},
mouseup: function() {
jQuery(this).removeClass('mousedown');
}
});
});
GitHub gets it right by making the JavaScript portion only serve as an enhancement for button focus/mousedown. This JavaScript is not required.
In the end, the amount of CSS and JavaScript used is probably more than you would have thought but the result is really smooth. Feel free to profess your own love for GitHub below.
Discussion
Be Heard!
Share your thoughts with fellow developers of all skill levels! I want to hear from you!
The final result is awesome. But yeah, the amount of CSS and JS was way bigger than i expected :O
But it’s worth the result. Thanks for sharing David ;)
Regards
Very nice.
They need an active state though.
I never used to have them, but there is very good cause for using them. The hover says there is action here. The active shows that you are activating it.
As Adam is saying, I would use :active for the mousedown part. No need for JS here David!
There seems to be a little bug…once the button has been clicked, it cannot be clicked again without clicking elsewhere on the page…? Firefox 3.6.3
The “:active” part would be a good fallback. GitHub uses JavaScript so there must be some rational behind it. I’ll do more research…
I think it’s IE that does not support the “:active” pseudo class
@Adam, @David: Actually we most definitely support an active state. Hence the javascript for mousedown (click and hold on a button to see). The reason that I used javascript instead of the :active pseudo class is that the pseudo class continues to stay activated long after you’ve released your mouse button.
For example, using active and doing: click on a button, click the back button -> arrive on a page where :active is still activated and your button is in a depressed state even though it should be back to it’s normal state. This is only true for links (IIRC) and not for .
Maybe just the perfectionist in me, but that’s why I chose to use mousedown rather than :active.
@Kyle Neath: Thanks for following up Kyle. Nice work too! :)
@Adriaan:
Yup I agree the button does get stuck in the down position in FF…
Another bug is the active/mousedown state is not working in IE or Chrome.
@Adriaan, @B. Moore: I’ve changed “focus” to “mousedown” an for maximum compatibility.
nothing says “THIS SITE IS HTML5!!!” better than using a javascript framework to change background-images at a:hover / a:active :)
@David Walsh: you da’MAN!
thank you
@Metric Stormtrooper: Are you high?
@Kyle Neath: roflmao!! may be its the dark side of the force skewing whats left of his brain…..
:P
Technically, you could do all of this without any nested spans, js, or images (other than the icon) by using pseudo-elements, border-radius, and gradients or box-shadow…but obviously you’ll end up with different appearances in less capable browsers.
@Kyle Neath, David: Weird. On gitHub im getting no active state. And in this example, im getting a stuck active state if you decide to click but not release on the button (something a user may do if at the last second they decide to not go through with it.)
Im in Safari 4.0.5 – 10.6.3
@Adam: I don’t do support requests through blog comments (I find these are people who generally are doing things like disabling javascript).
But if you click and hold (without ever releasing) you will most definitely get an active state as you should. All buttons in all OSes behave in this manner.
this buttons are great!
Hello,
These buttons are sure nice, but I would like to know more about the buttons that you used in this blog, like the reply, post comment, etc, the ones that look like facebook.
If you could write a new posting about these, or point me in the right direction that would be great.
Best..
Nice effect. Thanks for the share.
These seem different in one way — if you click and then drag out and then release, the button is still dark but with black text. The fix for this is to also attach a “mouseleave” handler that removes the mousedown class. This works in jQuery for sure – I haven’t tested the others.
Might sound like a noob question, but how do I increase the size of the button?
I tried increasing the height in the CSS code but that includes a different color when increased.