<?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>Lost Resort Blog &#187; relative values for measurement units</title>
	<atom:link href="http://www.lostresort.biz/blog/tag/relative-values-for-measurement-units/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lostresort.biz/blog</link>
	<description>Talking about us and about web site development</description>
	<lastBuildDate>Mon, 16 Aug 2010 08:38:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using absolute or relative values for measurement units</title>
		<link>http://www.lostresort.biz/blog/2009/07/21/using-absolute-or-relative-values-for-measurement-units/</link>
		<comments>http://www.lostresort.biz/blog/2009/07/21/using-absolute-or-relative-values-for-measurement-units/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 12:37:32 +0000</pubDate>
		<dc:creator>Cristi_N</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[absolute values for measurement units]]></category>
		<category><![CDATA[measurement units for margin padding width height]]></category>
		<category><![CDATA[pixel or em]]></category>
		<category><![CDATA[relative values for measurement units]]></category>

		<guid isPermaLink="false">http://www.lostresort.biz/blog/?p=476</guid>
		<description><![CDATA[To display elements in the way we desire we have to follow some rules, which means that, if we want to position an element inside a web page or to set the font size of the text, we can use some CSS style properties (such as margin, padding, font-size etc.) and eventually we can acquire [...]]]></description>
			<content:encoded><![CDATA[<p>To display elements in the way we desire we have to follow some rules, which means that, if we want to position an element inside a web page or to set the font size of the text, we can use some <code>CSS</code> style properties (such as <code>margin</code>, <code>padding</code>, <code>font-size</code> etc.) and eventually we can acquire the same result on most web user agents. The goal is to have the same result in all major web browsers. At this time we are talking about Mozilla Firefox, Internet Explorer, Opera, Safari and Chrome.</p>
<h3>CSS units for measurements</h3>
<p>According to W3 Consortium <a href="http://www.w3.org/TR/CSS21/syndata.html#value-def-length">lengths refer to horizontal or vertical measurements</a> and there are two types of length units: <em>relative</em> and <em>absolute</em>. Using relative units means that the scale from one medium to another will be more easily.</p>
<p>Relative units are:</p>
<ul>
<li><b>em</b>: the &#8216;font-size&#8217; of the relevant font</li>
<li><b>ex</b>: the &#8216;x-height&#8217; of the relevant font</li>
<li><b>px</b>: pixels, relative to the viewing device</li>
</ul>
<p>The absolute units are:</p>
<ul>
<li><b>in</b>: inches — 1 inch is equal to 2.54 centimeters.</li>
<li><b>cm</b>: centimeters</li>
<li><b>mm</b>: millimeters</li>
<li><b>pt</b>: points — the points used by CSS 2.1 are equal to 1/72nd of an inch.</li>
<li><b>pc</b>: picas — 1 pica is equal to 12 points.</li>
</ul>
<p><cite title="http://www.w3.org/TR/CSS21/syndata.html#value-def-length">Absolute length units are only useful when the physical properties of the output medium are known</cite> and with this, the discussion about these measurement units is closed. </p>
<p>The most known and used units within <code>CSS</code> style sheets are <em>em</em> and <em>px</em>. One is relative to the relevant font and the other is relevant to the viewing device. </p>
<h3>Using <em>em</em> as measurement unit</h3>
<p>If we check the properties for our default (or many) web browser we will see that the default font size is set to <b>16px</b>. A good approach is to define the <code>font-size</code> style attribute for the elements displayed in a web page to <b>62.5%</b> which means that <b>1em</b> measurement unit will be equal to <b>10px</b> (simple calculation <b>62.5%*16px = 10px</b>), or to directly declare <code>font-size:10px</code>. This is good because if we define a <b>1.1 em</b> unit it is translated to <b>11px</b> and so on. On the other hand if we are using values with more than one digit to represent non-integer values, problems may occur because, using values like <code>1.2345em</code> cannot split a pixel to the desired value (<b>1.2345*10px = 12.345px</b>) and the measurement unit will take the rounded value. The problem comes when the subvalue is half of the value, because there is a different behavior among the top wide-spread web browser on rounding these values. Some of them will round the subvalues up (IE 6 and IE 7) and some of them will round the subvalues down (Opera and Safari) and Firefox tends to round both up and down. With this approach the result will not be the same on different web browsers.</p>
<p>So, a good behavior is to use as much as possible multiplying values that in the end will compute integer values (measured as <em>pixels</em>). <a href="http://pxtoem.com/" title="Useful tool to transform px to em units">Take a look at this application which transforms <code>px</code> to <code>em</code> values and viceversa.</a></p>
<h3>Using <em>px</em> as measurement unit</h3>
<p>Because the content of the web pages is mostly served as graphic representation through an user agent to the audience a good reflex might be to set all measurement units using <code>px</code> values. This approach tends to be necessary and most desirable when the <code>width</code> and <code>height</code> style properties are declared, and even the measurement unit is relative to the viewing device, in absolute values the space occupied is the same. </p>
<h3>Using a mix of <em>em</em> and <em>px</em> as measurement units</h3>
<p>Another option is to use <em>px</em> as a measurement unit to declare some style properties, such as <code>width</code>, <code>height</code>, <code>background-position</code> or <code>letter-spacing</code>, and <em>em</em> measurement unit to declare another style properties like <code>margin</code>, <code>padding</code>, <code>line-height</code>, <code>font-size</code> or <code>text-indent</code>. In the last case, the properties will follow the size of the text font.</p>
<h3>Conclusions for what measurement units to use</h3>
<p>Based on whatever option we chose and use for measurement units it&#8217;s good to follow some practices.</p>
<p>A good start is to declare explicitly the <code>font-size</code> for the <code>body</code> element to a fixed value of <b>10px</b>. In this case the control of the sizes isn&#8217;t influenced by the default font size value on users&#8217; web browser. If we set the value as a percent (<b>62,5%</b>) relative to the default web browser&#8217;s font size value, and if this value is different from <b>16px</b>, problems may occur. </p>
<p>Use <em>px</em> as a measurement unit for <code>width</code> and <code>height</code> of the elements. You may find that in IE7 <code>11em</code> is not equal with <code>110px</code> when you set the <code>width</code> of an element (assuming that <b>1em = 10px</b>).</p>
<p>When we use <em>em</em> as measurement unit is better to define subvalues of it with one digit. Using more than one could lead to some unexpected displaying behavior on some web browsers which can make the web site pages look messy.</p>
<p>Also, some attention should be given on inheritance on <code>DOM</code> tree. Changing the <code>font-size</code> value, after the initial declaration of <b>10px</b>, should be made using the <em>em</em> measurement unit. Using <em>px</em> to declare the font size of an element will also modify the value of <em>em</em> measurement unit.</p>
<p>Today web browsers developers tend to adhere to <code>HTML</code> and <code>CSS</code> standards more accurately than a few years ago, and for a web site developer this represents less time consumed for fixing bugs for old versions of user agents. I hope that one day in the near future, the old versions browser will be gone for ever, and the <code>HTML</code> and <code>CSS</code> standards will be implemented in the same way in all major browsers.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.lostresort.biz/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.lostresort.biz/blog/2009/07/21/using-absolute-or-relative-values-for-measurement-units/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
