<?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>Xhtmlmagic &#187; jQuery</title>
	<atom:link href="http://www.xhtmlmagic.com/blog/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xhtmlmagic.com/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 13 Aug 2009 07:28:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to fix 1px background centering error in Firefox using jQuery</title>
		<link>http://www.xhtmlmagic.com/blog/jquery/how-to-fix-1px-background-centering-error-in-firefox-using-jquery/</link>
		<comments>http://www.xhtmlmagic.com/blog/jquery/how-to-fix-1px-background-centering-error-in-firefox-using-jquery/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 10:00:44 +0000</pubDate>
		<dc:creator>Mario</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Css]]></category>
		<category><![CDATA[Firefox]]></category>

		<guid isPermaLink="false">http://www.xhtmlmagic.com/blog/?p=7</guid>
		<description><![CDATA[<strong>Short version:</strong> If you already have jQuery included, fixing 1px background centering error in Firefox is easy - just detect Firefox and change background's horizontal position to 49.99% while leaving 50% in all other browsers.]]></description>
			<content:encoded><![CDATA[<blockquote><p><strong>Short version</strong> If you already have jQuery included, fixing 1px background centering error in Firefox is easy &#8211; just detect Firefox and change background&#8217;s horizontal position to 49.99% while leaving 50% in all other browsers</p></blockquote>
<p>Although i&#8217;ts nice to blame every coding problem on IE, Firefox has its share of bugs too. One of those is 1px background centering offset. It goes like this: when you try to match centered body background with the background of your centered div, you get 1px offset whenever the browser window pixel width is odd. For even width, background centers ok.</p>
<p>There is no way around it in css (some tutorials say that you just need to add 100% height to the body, but that doesn&#8217;t work) but there is a simple solution using jQuery (or any other library which lets you change the css property of an element). Agreed, including jQuery just to fix this is an overkill, but chances are, you already use it for something else in your page. <span id="more-7"></span></p>
<p>First thing, we need to detect Firefox. Latest jQuery version doesn&#8217;t have browser detection function, instead they put a function which tells you what the browser support and what not. We&#8217;re not going to use that. There is a nice line of code for browser detection, which worked great for me in the past, and I have no idea where I found it, but here goes:</p>
<p><pre>B = (function x(){})[-5]==&#039;x&#039;?&#039;FF3&#039;:
        (function x(){})[-6]==&#039;x&#039;?&#039;FF2&#039;:
            /a/[-1]==&#039;a&#039;?&#039;FF&#039;:
                &#039;\v&#039;==&#039;v&#039;?&#039;IE&#039;:
                    /a/.__proto__==&#039;//&#039;?&#039;Safari&#039;:
                        /s/.test(/a/.toString)?&#039;Chrome&#039;:
                            /^function \(/.test([].sort)?&#039;Opera&#039;:
                                &#039;Unknown&#039;
</pre></p>
<p>It just works. And then you just need to give special css rule, just for Firefox &#8211; </p>
<p><pre>if(B == &#039;FF&#039;) { $(&#039;body&#039;).css(&#039;background-position&#039;,&#039;49.99% 0&#039;); }</pre></p>
<p>Ta-daaa! All browsers are happily centering with 50% and Firefox is forced to always center the background to the left, which makes it perfectly matching your centered div.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-wealth">
<ul class="socials">
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.xhtmlmagic.com/blog/jquery/how-to-fix-1px-background-centering-error-in-firefox-using-jquery/&amp;title=How+to+fix+1px+background+centering+error+in+Firefox+using+jQuery" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.xhtmlmagic.com/blog/jquery/how-to-fix-1px-background-centering-error-in-firefox-using-jquery/&amp;t=How+to+fix+1px+background+centering+error+in+Firefox+using+jQuery" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-gmail">
			<a href="https://mail.google.com/mail/?ui=2&amp;view=cm&amp;fs=1&amp;tf=1&amp;su=How+to+fix+1px+background+centering+error+in+Firefox+using+jQuery&amp;body=Link: http://www.xhtmlmagic.com/blog/jquery/how-to-fix-1px-background-centering-error-in-firefox-using-jquery/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Short%20version%3A%20If%20you%20already%20have%20jQuery%20included%2C%20fixing%201px%20background%20centering%20error%20in%20Firefox%20is%20easy%20-%20just%20detect%20Firefox%20and%20change%20background%27s%20horizontal%20position%20to%2049.99%25%20while%20leaving%2050%25%20in%20all%20other%20browsers." rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://www.xhtmlmagic.com/blog/jquery/how-to-fix-1px-background-centering-error-in-firefox-using-jquery/&amp;title=How+to+fix+1px+background+centering+error+in+Firefox+using+jQuery" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://www.xhtmlmagic.com/blog/jquery/how-to-fix-1px-background-centering-error-in-firefox-using-jquery/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=How+to+fix+1px+background+centering+error+in+Firefox+using+jQuery+-+http://b2l.me/wadys&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.xhtmlmagic.com/blog/jquery/how-to-fix-1px-background-centering-error-in-firefox-using-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
