<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Converting to Pixels with javascript</title>
	<atom:link href="http://blog.stchur.com/2006/09/20/converting-to-pixels-with-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.stchur.com/2006/09/20/converting-to-pixels-with-javascript/</link>
	<description>If you don&#039;t expect too much from me, you might not be let down.</description>
	<lastBuildDate>Thu, 26 Jan 2012 08:23:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<item>
		<title>By: sstchur</title>
		<link>http://blog.stchur.com/2006/09/20/converting-to-pixels-with-javascript/comment-page-1/#comment-128359</link>
		<dc:creator>sstchur</dc:creator>
		<pubDate>Fri, 16 Dec 2011 18:59:26 +0000</pubDate>
		<guid isPermaLink="false">http://stchur.com/blog2/2006/09/20/converting-to-pixels-with-javascript/#comment-128359</guid>
		<description>Joe:

This is a little bit tricky because you have to consider the fact that when you specify a dimension as a percentage in CSS, it&#039;s a percentage of the parent&#039;s STYLE height (that is, its height as specified in CSS).  Bear in mind that this could be rather different from its rendered height on screen.

For instance, if you specify that an element have height: 400px, but then also give it 10px padding all around, and a 20px border all around, the total height (offsetHeight) will actually be 60px more (30px on top and bottom).  However, that extra 60px doesn&#039;t come into play when calculating the height of some child element specified as a percentage.

What you really want then is the style height of the outer element (that is, you need the height in pixels of the outer element as specified in the CSS).  And to further complicate your life, that value might not be specified in px, and if you&#039;re in IE, then you&#039;ll need to convert it to px :-)

And you&#039;ll need the same value of the inner element.  Probably, your favorite Javascript library has a way of getting the height (or width) of an element as specified in CSS (note that reading .style.height will not work if the height was not specified via .style.height or via an inline style attribute).

I don&#039;t know how to do this in jQuery or Prototype or Dojo, but in Gimme (my library) it would look like:

var innerElemHeight = parseInt(Gimme(&#039;#innerElem&#039;).get_style(&#039;height&#039;));
var outerElemHeight = parseInt(Gimme(&#039;#outerElem&#039;).get_style(&#039;height&#039;));
var pct = innerElemHeight / outerElemHeight;

Hope this helps!</description>
		<content:encoded><![CDATA[<p>Joe:</p>
<p>This is a little bit tricky because you have to consider the fact that when you specify a dimension as a percentage in CSS, it&#039;s a percentage of the parent&#039;s STYLE height (that is, its height as specified in CSS).  Bear in mind that this could be rather different from its rendered height on screen.</p>
<p>For instance, if you specify that an element have height: 400px, but then also give it 10px padding all around, and a 20px border all around, the total height (offsetHeight) will actually be 60px more (30px on top and bottom).  However, that extra 60px doesn&#039;t come into play when calculating the height of some child element specified as a percentage.</p>
<p>What you really want then is the style height of the outer element (that is, you need the height in pixels of the outer element as specified in the CSS).  And to further complicate your life, that value might not be specified in px, and if you&#039;re in IE, then you&#039;ll need to convert it to px <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>And you&#039;ll need the same value of the inner element.  Probably, your favorite Javascript library has a way of getting the height (or width) of an element as specified in CSS (note that reading .style.height will not work if the height was not specified via .style.height or via an inline style attribute).</p>
<p>I don&#039;t know how to do this in jQuery or Prototype or Dojo, but in Gimme (my library) it would look like:</p>
<p>var innerElemHeight = parseInt(Gimme(&#039;#innerElem&#039;).get_style(&#039;height&#039;));<br />
var outerElemHeight = parseInt(Gimme(&#039;#outerElem&#039;).get_style(&#039;height&#039;));<br />
var pct = innerElemHeight / outerElemHeight;</p>
<p>Hope this helps!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: joe</title>
		<link>http://blog.stchur.com/2006/09/20/converting-to-pixels-with-javascript/comment-page-1/#comment-128357</link>
		<dc:creator>joe</dc:creator>
		<pubDate>Fri, 16 Dec 2011 18:19:14 +0000</pubDate>
		<guid isPermaLink="false">http://stchur.com/blog2/2006/09/20/converting-to-pixels-with-javascript/#comment-128357</guid>
		<description>great stuff!  Now... How do I convert from pixels to percent?  I have a div within a div, and I want to find the percent the inner div is of the outerdiv.  I tried (slice/pie)*100; and then set the innerdiv to that percent, but sometimes it&#039;s off by a pixel.</description>
		<content:encoded><![CDATA[<p>great stuff!  Now&#8230; How do I convert from pixels to percent?  I have a div within a div, and I want to find the percent the inner div is of the outerdiv.  I tried (slice/pie)*100; and then set the innerdiv to that percent, but sometimes it&#039;s off by a pixel.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: raul</title>
		<link>http://blog.stchur.com/2006/09/20/converting-to-pixels-with-javascript/comment-page-1/#comment-100030</link>
		<dc:creator>raul</dc:creator>
		<pubDate>Wed, 26 Jan 2011 15:44:17 +0000</pubDate>
		<guid isPermaLink="false">http://stchur.com/blog2/2006/09/20/converting-to-pixels-with-javascript/#comment-100030</guid>
		<description>if _context is optional and units are percentual, function raise an error when calling convertToPixels(&#039;75%&#039;)
in this line:
_context = _context.parentNode &#124;&#124; _context;</description>
		<content:encoded><![CDATA[<p>if _context is optional and units are percentual, function raise an error when calling convertToPixels(&#039;75%&#039;)<br />
in this line:<br />
_context = _context.parentNode || _context;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sstchur</title>
		<link>http://blog.stchur.com/2006/09/20/converting-to-pixels-with-javascript/comment-page-1/#comment-1274</link>
		<dc:creator>sstchur</dc:creator>
		<pubDate>Wed, 07 May 2008 00:20:12 +0000</pubDate>
		<guid isPermaLink="false">http://stchur.com/blog2/2006/09/20/converting-to-pixels-with-javascript/#comment-1274</guid>
		<description>Vlado,

Thanks for the tip.  I&#039;ve only run across .scrollHeight once or twice and haven&#039;t really utilized it much.  Do you know if it give the overall height of an element, including borders and padding?  Or just the internal height of the element (minus borders and padding)?

What does IE6 think of this property?</description>
		<content:encoded><![CDATA[<p>Vlado,</p>
<p>Thanks for the tip.  I&#039;ve only run across .scrollHeight once or twice and haven&#039;t really utilized it much.  Do you know if it give the overall height of an element, including borders and padding?  Or just the internal height of the element (minus borders and padding)?</p>
<p>What does IE6 think of this property?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vlado</title>
		<link>http://blog.stchur.com/2006/09/20/converting-to-pixels-with-javascript/comment-page-1/#comment-1273</link>
		<dc:creator>Vlado</dc:creator>
		<pubDate>Tue, 06 May 2008 20:57:14 +0000</pubDate>
		<guid isPermaLink="false">http://stchur.com/blog2/2006/09/20/converting-to-pixels-with-javascript/#comment-1273</guid>
		<description>This returns height of the element in pixels.
In firefox 2.0.0.14 and IE 7.

document.getElementById(&#039;div1&#039;).scrollHeight

It seems to me that the browsers are implemented the way to complicate the things as much as possible. You must cheat all the time :-))</description>
		<content:encoded><![CDATA[<p>This returns height of the element in pixels.<br />
In firefox 2.0.0.14 and IE 7.</p>
<p>document.getElementById(&#039;div1&#039;).scrollHeight</p>
<p>It seems to me that the browsers are implemented the way to complicate the things as much as possible. You must cheat all the time <img src='http://blog.stchur.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ferrai</title>
		<link>http://blog.stchur.com/2006/09/20/converting-to-pixels-with-javascript/comment-page-1/#comment-14</link>
		<dc:creator>Ferrai</dc:creator>
		<pubDate>Tue, 04 Dec 2007 10:45:25 +0000</pubDate>
		<guid isPermaLink="false">http://stchur.com/blog2/2006/09/20/converting-to-pixels-with-javascript/#comment-14</guid>
		<description>This is perfect: I&#039;m writing a image replacement solution that will benefit hugely from this. Thanks for sharing it!</description>
		<content:encoded><![CDATA[<p>This is perfect: I&#039;m writing a image replacement solution that will benefit hugely from this. Thanks for sharing it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Smalltalk</title>
		<link>http://blog.stchur.com/2006/09/20/converting-to-pixels-with-javascript/comment-page-1/#comment-13</link>
		<dc:creator>Smalltalk</dc:creator>
		<pubDate>Mon, 05 Feb 2007 04:58:43 +0000</pubDate>
		<guid isPermaLink="false">http://stchur.com/blog2/2006/09/20/converting-to-pixels-with-javascript/#comment-13</guid>
		<description>This has been very helpful. Thanks!</description>
		<content:encoded><![CDATA[<p>This has been very helpful. Thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

