Simply-Buttons v2

» November 11th, 2009 » 50 Comments

TESTED IE6 IE7 FF(PC/MAC) Safari SafariMobile Opera(PC/MAC)

Background

I had some time a while back and read a few interesting articles on buttons:

These got me thinking… these techniques are pretty nice, but I think they can meet somewhere in the middle and make something even better, after all the button tag was created to make really nice buttons for your sites (W3 Button Element). After a little work I came up with the “Simply-Buttons Technique”.

Benefits

  • Buttons size to fit text.
  • There are 3 states: Inactive, Active, and Hover.
  • They look and behave the same way in every browser and operating system.
  • The do not need the javascript to work, they work with just the css
  • Way better looking buttons than what anyone else can do with XHTML/CSS, isn’t that reason enough?!

How it Works

The markup is pretty simple, as you can see below. The thing to notice here is that both the button and link tags each have a nested-nested span tag.

<button>
  <span><span>Button</span></span>
</button> 

<a class="button">
  <span><span>Button</span></span>
</a>

Now there are two parts involved here, the first thing to know here is that this method will work with just CSS. However it works a little better with some Javascript enhancements. Let’s take a look:

Generic (no javascript)

Now we see the buttons load nicely here, we are using the Sliding Doors Technique. The caveats are:

  • There are outlines on the buttons.
  • The text is selectable.
  • These buttons are not mobile friendly (the button tag).

The Solution

In comes the the Simply-Buttons Javascript component.   The library solves the caveats above as you can see below:

YouTube Style (w/ javascript)

Below is the object you can configure to further customize the buttons to suite your design.


  options : {
    hyperlinkClass : 'button' // 'a' tags with this class will be processed
    ,activeButtonClass : 'button_active' // Active class for active buttons
    ,states : { // The various states and css properties, define them as normal
      outer : {
        active : {
          backgroundPosition : 'bottom left'
        }
        ,inactive : {
          backgroundPosition : 'top left'
        }
      }
      ,inner : {
        active : {
          backgroundPosition : 'bottom right'
        }
        ,inactive : {
          backgroundPosition : 'top right'
        }
      }
    }
    ,iphone : {
      replaceButtons : true // if iPhone; replace buttons with inputs?
    }
  }

Download the Code/Sample

Below you can download not only the core CSS and Javascript files but you also will get a few samples. I am not the greatest at tutorials so I will update this post as feedback comes in, however there are quite a few comments throughout the files. The important files are: stylesheets/buttons.css, javascripts/simply-buttons-v2.js.

Installation

Simply include the stylesheet and javascript and make sure they can find your images. Then simple call the init function, either using the default options as below or configuring it as show above.

<link rel="stylesheet" type="text/css" href="stylesheets/SimplyButtons.css" media="screen" />
<script type="text/javascript" src="javascripts/SimplyButtons.js"></script>
<script type="text/javascript">
  window.onload = function()
  {
    SimplyButtons.init();
  };
</script>

Customize

As shown in the YouTube example, you can easily modify the buttons look and feel. You can do this by creating your own stylesheet and you can use the one supplied as an example, its filled with comments. There are also options that can be changed and configured on the javascript end as show above.

SUPPORT: Feedback and Donations help to support this Plugin and future development.

Spread the Word
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • LinkedIn
  • NewsVine
  • Reddit
  • BlinkList
  • StumbleUpon
  • Technorati
  • Yahoo! Buzz
  • Propeller

Share

Comments

50 Responses to “Simply-Buttons v2”

  1. Good to see updates to this project. Good work, thanks for sharing it with world :-)

  2. Morgan Roderick on November 11th, 2009 at 5:07 am
  3. No need to have two images for the sprite, you can have just one with both sides! had came up with this same idea a few weeks ago…

  4. Santiago on November 11th, 2009 at 11:16 am
  5. Good idea. Thak you… :)

  6. Erwin Romo on November 11th, 2009 at 11:51 am
  7. Another caveat, they don’t resize vertically, so if the user uses text-zoom, has a larger minimum font-size set than expected or they contain enough text that the text wraps then the text either overflows the button or is cut off.

  8. Nik on November 11th, 2009 at 12:19 pm
  9. Why do this with javascript when there’s a perfect css solution available.

  10. Bogdan Pop on November 11th, 2009 at 1:25 pm
  11. I’ve got a bug when I click on the button, and release the click outside the button.
    I’m using Firefox 3.5.5 under Linux Fedora 11.

    (If you need, I could try with other browser/os)

    Hope this helps :)

  12. cx42net on November 11th, 2009 at 2:06 pm
  13. @Bogdan Pop : I’m ok with the css (I prefer using natural way of work of the browser instead forcing him with some js), but how you deal with rounded corner (specifically ie) ?

    Because using a JS plugin that will add specific tag that you would do hard coded is probably better, unless if you have a better solution (and I would be pleased to know, seriously :) ).

  14. cx42net on November 11th, 2009 at 2:12 pm
  15. Thanks for the input guys and keep it coming :) Some responses below.

    @Nik: You are right, but honestly I can’t remember ever making a button with wrapping text or even the last time I saw one? Edge Case?

    @Santiago: You are right, in fact I did just that for the YouTube example. The idea is to show off different ways so people can see how to customize them to make them their own.

    @Bogdan Pop: The css solution does work well, the js just removes the button outlines, disables the text, and if it is IE changes the press state to what we see and want in other browsers and finally the option with the iPhone to remove the buttons altogether and replace them with inputs. Think of the JS as a Supplement, not necessary but adds a few finishing touches.

    @cx42net: If you could try another that would be great, I will also try this out on Fedora as soon as possible to see what is happening there.

  16. Kevin on November 11th, 2009 at 2:48 pm
  17. Can’t see any valuable reason for realizing buttons this ugly way. there is a much cleaner solution using css only!

  18. Kay on November 11th, 2009 at 3:15 pm
  19. Looks great… I miss the pointer “hand” icon though. Maybe add,

    cursor: pointer;

    to button span’s css?

  20. kel on November 11th, 2009 at 3:17 pm
  21. Thanks for the input again guys! Some more responses below:

    @Kel: Definitely, I will add that into the next update!

    @Kay and similar: I appreciate your opinion on the topic, however could you post up some links to the stuff you refer too or talk a a little about what you mean? I made these because I couldn’t find anything out there that did everything these do, did I miss a great library?

  22. Kevin on November 11th, 2009 at 3:48 pm
  23. I haven’t much to say but thanks for your work. I learned a lot looking through it. Without discussing the need for even more .js, I love it as the details, or finishing touches as you put it, are smooth. Thanks again.

  24. Joe Howard on November 11th, 2009 at 10:45 pm
  25. @Kay and others like him: If all you can do is talking bullshit in a rather arrogant way you should just shut up.

    If you don’t like what Kevin did you’re free to tell him why you think other solutions are better or why you don’t like his solution. Just calling his way “ugly” and saying there are cleaner solutions out there is pathetic. How about providing links to solutions that can do the same (or even more) with CSS only so we all can learn from them?

    (( Sorry, Kevin for that rant – I’m just fed up of people acting like they are “oh so awesome” in comments while in reality they can’t do shit on their own ))

  26. Chris on November 12th, 2009 at 4:11 am
  27. @Chris: +1 ;)

  28. Kevin on November 12th, 2009 at 6:14 pm
  29. thanks!

  30. apit on November 14th, 2009 at 3:06 am
  31. This is excellent! Just what I have been looking for. Will use this in my next project. Thanks!

  32. Dragon on November 14th, 2009 at 8:19 am
  33. A very nice solution to this problem – pure CSS is nice, but this hybrid approach is a good compromise.

  34. Daniel on November 15th, 2009 at 5:53 pm
  35. Will these techniques also work on input elements of the type submit?

  36. bertgoethals on November 16th, 2009 at 5:34 am
  37. @bertgoethals: You can have a button element of type submit yes. However in order to achieve this look and behavior you need the button element, after all, this is what is intended for, better looking buttons than inputs can ever be ;)

  38. Kevin on November 16th, 2009 at 10:00 am
  39. Thanks man. It’s easily and beatiful. Best regards.

  40. Sedat Kumcu on November 16th, 2009 at 7:18 pm
  41. Nice work!

    One wee suggestion would be to leave the link outlines in the :focus state to aid keyboard navigation

  42. Pete B on November 18th, 2009 at 4:16 am
  43. Button disabled state is not working.

  44. Rakesh on November 18th, 2009 at 4:46 am
  45. One of the problems I’ve experienced with putting nested tags inside of the element is that it can cause problems with server side frameworks when a bunch of garbage gets submitted in IE6/7 (since those submit the innerHTML instead of the value).

    The way I solved the problem was to move the span’s outside of the button tag, any chance of that happening here?

  46. Zach Leatherman on November 18th, 2009 at 5:35 am
  47. Dave Shea posted about how to make consistently good-looking CSS buttons last year, but he relies on a couple of extra elements to get that to happen.

    http://mezzoblue.com/archives/2008/09/17/anchor_butto/

  48. Wayne Elgin on November 18th, 2009 at 6:28 am
  49. awsome works, CSS is great but javascript give it a lot more possibilities

  50. bali website design on November 18th, 2009 at 7:33 am
  51. This will get rid of the outline on buttons in Firefox.

    button::-moz-focus-inner {
    border: none;
    }

  52. Davin Studer on November 18th, 2009 at 9:11 am
  53. Thanks for taking the time to share this little gem. The buttons look fantastic!

  54. Bronson on November 18th, 2009 at 11:43 am
  55. Do you have any images that have transparent background in PNG format? The GIF images supplied in the release package doesn’t work good for sites with dark designs.

  56. All Knight Access on November 18th, 2009 at 8:50 pm
  57. Awesome work…

  58. Srinivas Tamada on November 18th, 2009 at 9:07 pm
  59. @Davin Studer: Nice! I will take a look at it and if it tests well I will add it in with props ;)

    @All Knight Access: There isn’t at the moment, I am looking into the next version supporting transparent pngs but that’s a whole other ball of wax. In the mean time just check out the images and make your own set with the background color you need, the idea here is to make your button design so your buttons can work like these and look the same across the board.

  60. Kevin on November 18th, 2009 at 9:15 pm
  61. element.style.cursor = ‘default’;
    to
    element.style.cursor = ”;

  62. ohaiyo on November 19th, 2009 at 1:50 am
  63. In reply to comment 30:

    @All Knight Access: There isn’t at the moment, I am looking into the next version supporting transparent pngs but that’s a whole other ball of wax.

    To avoid too much trouble/hacks when using transparent PNGs in an IE7- friendly way, using an approach based in PNG8 can make it just a quick (image) authoring effort. ;-)

    Keep up the good work! :-)

  64. Helder Magalhães on November 19th, 2009 at 7:07 am
  65. Great work! It’s an elegant and tidily written solution. Being a developer and not a web designer making things look nice is always a major pain. At least now I’m sorted… button-wise.

    Can I ask why haven’t you used jQuery. I know it’s an unnecessary dependancy but it’s so widely used that it’s unlikely to be included just for your button extension.
    It would make the code even tidier, options handling and DOM manipulation is a incredibly easy. You could also develop it as a jquery plugin so that I could just call it on any block element myDiv.button(’Save’), something like that.

  66. Stefano on November 19th, 2009 at 7:41 am
  67. IE7 problem: The sexy buttons generated with button tags have glitches while rendering on scroll. This happens only in IE7

  68. Jerry on November 20th, 2009 at 3:07 am
  69. Wow!! gr8 work, Thanks for sharing!

  70. Vipul Limbachiya on November 20th, 2009 at 5:00 am
  71. @Jerry: can you be more specific or provide a url to demonstrate the issue? More than happy to take a look and fix it ;)

  72. Kevin on November 20th, 2009 at 11:21 am
  73. I think the ie7 problem jerry was referring to is that horizontal lines occur in the button when scrolling: screenshot: http://alvarsirlin.com/simplybuttons-ie7.png

  74. alvar on November 24th, 2009 at 8:50 am
  75. also, small fyi, the stylesheet in the package says “Simply Buttons, version 1.0″. Not sure if that was an oversight, or the wrong file is in the package. Oh, and thanks for the great work!

  76. alvar on November 24th, 2009 at 8:54 am
  77. Awesome work on these buttons. Could you put the code on Github.com so we can browse it without having to download the zip? That would also let us maintain it for you ;)

  78. Jack Danger Canty on November 25th, 2009 at 1:11 pm
  79. It also works in chrome 3!

  80. Jesu on November 27th, 2009 at 1:03 am
  81. If you set the brackground-color: transparent on the “buttons”, remove the white areas around the button corners and make them also transparent and put a no-repeat on the background definition of the first span, you should be able to use the buttons on different color backgrounds. Since my forms are on white and light gray bg’s, using the same GIF’s worked out for me .. but for different background colors, you would have to either use PNG’s or create different GIF’s with different transparent values to match your background area. Good job though .. it works very nicely. I haven’t tested this out in IE and frankly I don’t care about IE! :)

  82. Ramin on December 5th, 2009 at 9:32 am
  83. Hi Kevin,

    What is the license for this? I’d like to use it in commercial software but want to make sure I’m not violating anything.

    Thanks,
    Jared

  84. Jared on December 21st, 2009 at 4:01 pm
  85. I cannot resist myself implementing these buttons. Thank you dear

  86. Alpesh Darji on January 13th, 2010 at 7:02 am
  87. Great update. We’ve been using the original buttons over the last 18 months and looking forward to trying out the new styles.

  88. Ecenica on January 14th, 2010 at 1:59 am
  89. Very cool buttons! Thanks for the code.

  90. Jeremy on January 16th, 2010 at 8:10 am
  91. Good Job……… :)

  92. Awan on February 21st, 2010 at 11:22 pm
  93. Great buttons!

    The only issue I had was that they don’t seem to work with keyboard navigation. That is I can’t tab over the button and hit enter to submit.

  94. Joel on March 21st, 2010 at 4:56 pm
  95. Great work, thank you fo share :)

  96. Ben on April 20th, 2010 at 3:19 pm
  97. These are fantastic. Thanks!

  98. Tom on May 5th, 2010 at 2:30 am
  99. Loved the post.
    I am taking the liberty of adding a reference to your article on my CSS aggregator site. Do let me know if you are ok with this.

  100. netbaazigar on May 19th, 2010 at 10:18 am

Trackbacks/Pingbacks

  1. 45 Powerful CSS/JavaScript-Techniques « wwwba
  2. 45种有用的Css/Javascript技巧 | narco
  3. 跨浏览器的CSS按钮

Leave a Reply