<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bloggerrific &#187; image buttons</title>
	<atom:link href="http://www.davidjrush.com/blog/tag/image-buttons/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidjrush.com</link>
	<description>David J. Rush is a web designer and usability consultant in Pittsburgh who freelances and blogs about anything web related.</description>
	<lastBuildDate>Thu, 05 Jan 2012 02:50:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CSS Rollover Button</title>
		<link>http://www.davidjrush.com/blog/2009/01/css-rollover-button/</link>
		<comments>http://www.davidjrush.com/blog/2009/01/css-rollover-button/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 16:50:00 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[css classes]]></category>
		<category><![CDATA[css tricks]]></category>
		<category><![CDATA[image buttons]]></category>

		<guid isPermaLink="false">http://www.davidjrush.com/blog/?p=14</guid>
		<description><![CDATA[Everyone likes a good rollover button. It creates interactivity for the user (which younger website users like) but also gives an indication that the button is clickable (which is important for older and less experienced website users). There are a lot of ways of implementing a good rollover. Most people jump to Javascript to do [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone likes a good rollover button. It creates interactivity for the user (which younger website users like) but also gives an indication that the button is clickable (which is important for older and less experienced website users). There are a lot of ways of implementing a good rollover. Most people jump to Javascript to do this because they think that&#8217;s the only way. However, this requires you to write javascript that preloads your images so that there is no delay on the rollover for the first time. Well, I&#8217;ve found a much better solution using only CSS that is clean, easy to implement, won&#8217;t break, and works across all browsers. I love it so much that I use it for all of my buttons on <a href="http://collegeprowler.com/" target="_blank">College Prowler</a> (my fulltime employer).</p>
<p>So now you must be asking, how does this wondrous CSS miracle work? It is actually quite simple. Using only one image, you will restrict the size of your button (to only show part of the image which will be set as the background), and then use the pseudo-class :hover to shift the background-position of that image into the hovered portion. Again, this allows your full image to be preloaded so there is no delay on rollover. This has been cross-browser tested in Firefox 2 and 3, Internet Explorer 6* and 7, Safari, Opera, and Chrome, and it works perfectly. Here is an example below including the full image, the image as it will appear/work, the HTML, and the CSS.</p>
<div class="clear"></div>
<div style="width: 100%; float: left; margin: 15px 0px;"><img style="float: left; margin-left: 100px; _margin-left: 50px;" src="http://www.davidjrush.com/images/tutorials/sampleRolloverButton.png" alt="Sample Rollover Button" width="200" height="200" />
<div style="float: right; margin-right: 100px; _margin-right: 50px;">
<div id="exampleButton" class="rollover"><a onclick="return false;" href="#">My Example Button</a></div>
</div>
</div>
<div class="clear"></div>
<p><strong>This is the HTML:</strong></p>
<pre class="brush: xml; title: ; notranslate">&lt;div class=&quot;rollover&quot; id=&quot;exampleButtonIDHere&quot;&gt;
  &lt;a href=&quot;link location here&quot;&gt;
    Put a summary of your button here. A few words will suffice, imagine this as the &quot;alt&quot; text for your image/link
  &lt;/a&gt;
&lt;/div&gt;</pre>
<p><strong>This is the CSS</strong> that made the image on the left into the button on the right.</p>
<pre class="brush: css; title: ; notranslate">.rollover a{
   display: block;
   text-indent: -9999px;
   margin: auto auto auto auto;
   cursor: pointer;
   outline: transparent solid 0px;
}
#exampleButtonIDHere a{
   height: 100px; /*This height is the visible portion of the button only*/
   width: 200px; /*Put your image width here*/
   background: url('Put your image location here') no-repeat left top;
}
#exampleButtonIDHere a:hover{
   background-position: left -100px; /*This negative value should match the height above*/
}</pre>
<p>So basically, a div, with a background image of both buttons, and a height and width restricted to the size of only one of the default button, shifts on :hover to pushing the hover portion of the image up into the viewable portion of the div, with the default portion of the image pushed out. Simple right? I know! If you are going to have a lot of these buttons, I&#8217;d suggest a whole separate CSS document for them. You can also combine the .rollover with the #exampleButtonIDHere. I keep them separate because it reduces the length of the CSS document with lots of buttons.</p>
<p>Don&#8217;t forget the a tag inside the div tag, and include the a as part of your CSS style declarations. Each portion of that CSS is very important! The &#8220;display: block;&#8221; allows for the image to not be pushed out of it&#8217;s own div. The &#8220;text-indent: -9999px;&#8221; pushes the alt text out of the way, the &#8220;margin: auto auto auto auto;&#8221; eliminates any cross-browser issues with default settings, the &#8220;cursor: pointer;&#8221; forces every browser to display it as a link with the mouse icon changing, and the &#8220;outline: transparent solid 0px;&#8221; forces browsers to <em>not</em> outline the link when being clicked. If you have any trouble implementing this, let me know, because I completely swear by this amazing CSS trick. I&#8217;d also love to hear if you&#8217;ve had successes implementing this on your own website. Again, for great examples, check out <a href="http://collegeprowler.com/" target="_blank">College Prowler</a>.</p>
<p><span style="color: black; font-size:85%;">*Be careful, if you use png images for your buttons (which I do because they are far superior to gifs with transparency) then IE6 won&#8217;t allow your rollover to work. Most png fixes that I have found so far disable background-position properties in CSS in IE6. Annoying, but really not the end of the world. Either live with it for whatever percentage of your users are IE6 users, or use jpgs and gifs instead. I say the pngs are worth it for the better transparency quality and lower file size.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidjrush.com/blog/2009/01/css-rollover-button/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

