<?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>David Calhoun&#039;s Developer Blog &#187; media queries</title>
	<atom:link href="http://davidbcalhoun.com/tag/media-queries/feed" rel="self" type="application/rss+xml" />
	<link>http://davidbcalhoun.com</link>
	<description>Just another blog</description>
	<lastBuildDate>Fri, 03 Feb 2012 01:29:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Does it still make sense to use em rather than px?</title>
		<link>http://davidbcalhoun.com/2010/does-it-still-make-sense-to-use-em-rather-than-px</link>
		<comments>http://davidbcalhoun.com/2010/does-it-still-make-sense-to-use-em-rather-than-px#comments</comments>
		<pubDate>Tue, 05 Oct 2010 18:13:15 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[em]]></category>
		<category><![CDATA[media queries]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[px]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=476</guid>
		<description><![CDATA[Alex Kessinger tweeted asking if there was a reason to still use EMs instead of PX measurements in mobile. This is an interesting question, but 140 characters isn&#8217;t quite enough to explain my thoughts. So here&#8217;s an old-fashioned blog post! Just a quick review: em and percentage (%) units in CSS are relative measurements, whereas [...]]]></description>
			<content:encoded><![CDATA[<p>Alex Kessinger <a href="http://twitter.com/voidfiles/status/26470129747">tweeted</a> asking if there was a reason to still use EMs instead of PX measurements in mobile.  This is an interesting question, but 140 characters isn&#8217;t quite enough to explain my thoughts.  So here&#8217;s an old-fashioned blog post!</p>
<p>Just a quick review: em and percentage (%) units in CSS are relative measurements, whereas px and other measurements are not.  What this means is that the resulting size of relative measurements depends on the size of the parent.</p>
<h3>The original reason to use EMs instead of PX measurements</h3>
<p>As far as I can tell, the original reason why it became a good practice to use EMs was because of page scaling and accessibility reasons.  When developers used PX measurements, they found that when the page was scaled (using Ctrl + Scroll Wheel), the parts of the page with fixed PX measurements didn&#8217;t scale.</p>
<p>Oh no!  So when a user with poor eyesight tried to scale a page to read the text better, it turns out that the text they were interesting in making bigger actually didn&#8217;t change size at all!</p>
<p>So is this still the case today?  By and large, no.  All modern browsers except IE6 will now scale pages correctly, regardless of the type of CSS units used.</p>
<p>From what I&#8217;ve heard, there are still many users with disabilities using IE6, but the reason is because they don&#8217;t want to upgrade their copy of the screen reader JAWS, which is quite expensive.  However, these users obviously aren&#8217;t affected by page scaling issues because they are blind.</p>
<h3>Maintainability concerns</h3>
<p>Even if the original reason to use EMs (above) is no longer valid, I still believe there are good reasons to use them.</p>
<p>One of the main reasons is maintainability.  If a page is designed to have one base font size (applied to the Body) and all other font sizes on the page use relative units (EMs or percentages), changing the font sizes later becomes almost trivial.  If someone wants to later increase the size of the fonts on their entire page, all they need to do is increase the one base font size.</p>
<p>This might be a bit of an edge case, but the elegance of the solution is too enticing!  <img src='http://davidbcalhoun.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Media queries and mobile</h3>
<p>Very closely related to the above point is the subject of media queries.  Recently media queries have been somewhat hailed as a <a href="http://www.quirksmode.org/blog/archives/2010/09/rethinking_the.html">panacea for all mobile development</a>.  I slightly disagree, but that&#8217;s a topic for another post!</p>
<p>The main point here is that people are now using media queries to quickly make mobile-friendly versions of their desktop websites.  That&#8217;s awesome, there&#8217;s no doubt about that.</p>
<p>So what about EMs and PX measurements?  Here&#8217;s the problem: if a developer coded their webpage in such a way that they used fixed pixel measurements, the amount of CSS required in the mobile media query could be obscenely large.  This is because each individual element with pixel measurement was <i>designed with the desktop in mind</i>!  This makes the task of adapting to different content very painful.</p>
<p>On the other hand, think about a page designed with relative units such as EMs or percentages.  This page was designed so that each element was proportional to its parent element, so that ideally changing the size of the entire page occurs in only a few places, at the Body level of the document.  Suddenly the CSS required for the mobile media query is much more manageable, since it&#8217;s now (ideally) only changing a few values on the Body element, not on <i>every</i> element on the page.</p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2010/does-it-still-make-sense-to-use-em-rather-than-px/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Dealing with device orientation (Mobile web part 6)</title>
		<link>http://davidbcalhoun.com/2010/dealing-with-device-orientation</link>
		<comments>http://davidbcalhoun.com/2010/dealing-with-device-orientation#comments</comments>
		<pubDate>Tue, 24 Aug 2010 08:41:51 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[mobile]]></category>
		<category><![CDATA[media queries]]></category>
		<category><![CDATA[orientation]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=410</guid>
		<description><![CDATA[Introduction If you read part 4 of this series, you&#8217;ll know that because of the variety of screen sizes, the best way to develop for mobile is to develop fluid layouts that take 100% of the available space on the screen. What you probably didn&#8217;t think of is that there&#8217;s different screen widths even on [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>If you read <a href="http://davidbcalhoun.com/2010/on-designing-a-mobile-webpage">part 4</a> of this series, you&#8217;ll know that because of the variety of screen sizes, the best way to develop for mobile is to develop fluid layouts that take 100% of the available space on the screen.</p>
<p>What you probably didn&#8217;t think of is that there&#8217;s different screen widths <em>even on the same device</em>!  This is due to screen orientation.  And when the user changes the screen orientation, stuff may break (hey, it&#8217;s not a perfect world).  In my experience this has meant needing to tweak percentage widths on elements, but I imagine there&#8217;s even more needs.  Imagine an image carousel that can only fit three images across in portrait mode but can possibly fit four images in landscape mode.  It might be nice to re-initialize the carousel to accommodate a forth image when the phone is in landscape mode (then again, that might create a lot of unnecessary overhead&#8230;).</p>
<p>In any case, I hope you can agree that it would sometimes be useful to know the screen orientation.</p>
<h3><code>window.orientation</code> and the <code>orientationchange</code> event</h3>
<p>Luckily on the latest smartphones you have some goodies available to you that you don&#8217;t have on the desktop (since desktop users aren&#8217;t in the habit of constantly turning their screens sideways!).</p>
<p><b><code>window.orientation</code></b>: this property gives the current screen orientation: 0 in portrait mode, 90 when rotated left, -90 when rotated right (no special value when the screen is upside-down)</p>
<p><b><code>orientationchange event</code></b>: this window event fires after every 90 degrees of rotation and, like other events, <a href="http://en.wikipedia.org/wiki/DOM_events">can be applied in various ways</a>:</p>
<pre name="code" class="JScript">
// DOM Level 0 (avoid)
window.onorientationchange = function(){};

// DOM Level 2
window.addEventListener('orientationchange', function(){}, false);
</pre>
<p><a href="http://www.iphonemicrosites.com/articles/6-tips-to-optimize-your-current-site-for-the-iphone/">Some websites</a> recommend using <code>orientationchange</code> to dynamically add an <code>orient</code> attribute on the <code>body</code> element and target the orientation with CSS selectors (<code>body[orient=landscape]</code>), <i>but this is in error</i>.  As it turns out, <code>orientationchange</code> is only fired AFTER the screen has been rotated (which also <a href="http://www.stubbornella.org/content/2009/03/27/reflows-repaints-css-performance-making-your-javascript-slow/">triggers a CSS reflow</a>), which means this attribute is updated later (after the reflow).  And unfortunately editing this <code>orient</code> attribute doesn&#8217;t trigger another CSS reflow.  The result?  When you rotate the device, these new CSS styles don&#8217;t get applied!</p>
<p>The fix is to add the orientation as a CSS class, which does trigger a CSS reflow.  So our code at this point will look something like this:</p>
<pre name="code" class="JScript">
(function(){
var init = function() {
  var updateOrientation = function() {
    var orientation = window.orientation;

    switch(orientation) {
      case 90: case -90:
        orientation = 'landscape';
      break;
      default:
        orientation = 'portrait';
    }

    // set the class on the HTML element (i.e. <html class="portrait">)
    document.body.parentNode.setAttribute('class', orientation);
  };

  // event triggered every 90 degrees of rotation
  window.addEventListener('orientationchange', updateOrientation, false);

  // initialize the orientation
  updateOrientation();
}

window.addEventListener('DOMContentLoaded', init, false);

})();
</pre>
<p>Now we can target elements like this in CSS:</p>
<pre name="code" class="CSS">
.portrait body div { width: 10%; }
.landscape body div { width: 15%; }
</pre>
<h3>With a little help from media queries</h3>
<p>You may have heard of <a href="https://developer.mozilla.org/en/css/media_queries">media queries</a> being used to target mobile devices (based on screen pixel width) or to <a href="http://www.bdoran.co.uk/2010/07/19/detecting-the-iphone4-and-resolution-with-javascript-or-php/">target the iPhone 4&#8242;s Retina display</a>, but you may not have known that you can also target screen orientation!</p>
<pre name="code" class="CSS">
@media all and (orientation: portrait) {
  body div { width: 10%; }
}

@media all and (orientation: landscape) {
  body div { width: 15%; }
}
</pre>
<p>The orientation media query is available on iOS 3.2+, Android 2.0+, and some other browsers.</p>
<p>This is a lot cleaner than the above JavaScript example in the sense that it&#8217;s pure CSS, and it&#8217;s part of the CSS that gets reflowed when the screen is rotated.</p>
<p>(Minor note: iOS 4 on the iPhone Simulator running 4.0.0 looks like it&#8217;s stuck in landscape orientation, but the media queries work correctly on my 3GS with 4.0.1)</p>
<h3>Fallback: when window.orientation and media queries aren&#8217;t available&#8230;</h3>
<p>If <code>window.orientation</code> isn&#8217;t available on a device, chances are the <code>orientationchange</code> event and media queries (for orientation) will also not be available.  Oh no, what do we do now?</p>
<p>Even though this isn&#8217;t an entirely foolproof method, we can dynamically measure the window width and height and guess orientation based on that:</p>
<pre name="code" class="JS">
(function(){
var HTMLNode = document.body.parentNode;
var updateOrientation = function() {
  // landscape when width is biggest, otherwise portrait
  var orientation = (window.innerWidth > window.innerHeight) ? 'landscape': 'portrait';

  // set the class on the HTML element (i.e. <html class="portrait">)
  HTMLNode.setAttribute('class', orientation);
}
var init = function() {
  // initialize the orientation
  updateOrientation();

  // update every 5 seconds
  setInterval(updateOrientation, 5000);
}
window.addEventListener('DOMContentLoaded', init, false);
})();
</pre>
<p>Ok, so it&#8217;s not pretty, but it seems to work.  The overhead in this fallback example is the fact that we have to use a polling technique (in this case every 5 seconds [5000 milliseconds]) to check for changes in orientation.</p>
<p>Note: there&#8217;s also the strong possibility that these browsers will not support the <code>DOMContentLoaded</code> event, but we&#8217;ll ignore that for the purposes of this article. (if you have problems, change <code>DOMContentLoaded</code> to <code>load</code>)</p>
<h3>Putting it all together</h3>
<p>Ok, so if you want the fallback example to work in addition to newer methods, unless you want to duplicate your CSS, then avoid using media queries to target orientation.  Instead we&#8217;ll rely on adding a class to the <code>html</code> tag (or the <code>body</code> tag if you prefer).</p>
<p>Once we put everything together, we get something that looks like this:</p>
<pre name="code" class="JScript">
(function(){
var supportsOrientation = (typeof window.orientation == 'number' &#038;&#038; typeof window.onorientationchange == 'object');
var HTMLNode = document.body.parentNode;
var updateOrientation = function() {
  // rewrite the function depending on what's supported
  if(supportsOrientation) {
    updateOrientation = function() {
      var orientation = window.orientation;

      switch(orientation) {
        case 90: case -90:
          orientation = 'landscape';
        break;
        default:
          orientation = 'portrait';
      }

      // set the class on the HTML element (i.e. <html class="portrait">)
      HTMLNode.setAttribute('class', orientation);
    }
  } else {
    updateOrientation = function() {
      // landscape when width is biggest, otherwise portrait
      var orientation = (window.innerWidth > window.innerHeight) ? 'landscape': 'portrait';

      // set the class on the HTML element (i.e. <html class="portrait">)
      HTMLNode.setAttribute('class', orientation);
    }
  }

  updateOrientation();
}
var init = function() {
  // initialize the orientation
  updateOrientation();

  if(supportsOrientation) {
    window.addEventListener('orientationchange', updateOrientation, false);
  } else {
    // fallback: update every 5 seconds
    setInterval(updateOrientation, 5000);
  }

}
window.addEventListener('DOMContentLoaded', init, false);
})();
</pre>
<p>Minified (540 bytes):</p>
<pre name="code" class="JScript">
(function(){var e=typeof window.orientation=="number"&#038;&#038;typeof window.onorientationchange=="object",f=document.body.parentNode;function c(){c=e?function(){var d=window.orientation;switch(d){case 90:case -90:d="landscape";break;default:d="portrait"}f.setAttribute("class",d)}:function(){f.setAttribute("class",window.innerWidth>window.innerHeight?"landscape":"portrait")};c()}window.addEventListener("DOMContentLoaded",function(){c();e?window.addEventListener("orientationchange",c,false):setInterval(c,5E3)},false)})();
</pre>
<h3>Conclusion</h3>
<p>And that&#8217;s it!  Now we can reliably target different screen orientations with some straightforward CSS:</p>
<pre name="code" class="CSS">
.portrait body div { width: 10%; }
.landscape body div { width: 15%; }
</pre>
<p>Again, in my experience I&#8217;ve used this to fix bugs.  But I&#8217;m sure you can find more creative uses for it!</p>
<h3>Related</h3>
<p><a href="http://www.nczonline.net/blog/2010/04/06/ipad-web-development-tips/">iPad web development tips (Nicholas C. Zakas)</a><br />
<a href="http://ajaxian.com/archives/iphone-windowonorientationchange-code">iPhone window.onorientationchange Code (Ajaxian)</a><br />
<a href="http://www.quirksmode.org/blog/archives/2010/04/the_orientation.html">The orientation media query (Quirksmode)</a></p>
<h3>More from the Mobile Web series:</h3>
<p><a href="http://davidbcalhoun.com/2010/viewport-metatag">Part 1: The viewport metatag</a><br />
<a href="http://davidbcalhoun.com/2010/the-mobile-developers-toolkit-mobile-web-part-2">Part 2: The mobile developer’s toolkit</a><br />
<a href="http://davidbcalhoun.com/2010/designing-buttons-that-dont-suck">Part 3: Designing buttons that don’t suck</a><br />
<a href="http://davidbcalhoun.com/2010/on-designing-a-mobile-webpage">Part 4: On designing a mobile webpage</a><br />
<a href="http://davidbcalhoun.com/2010/using-mobile-specific-html-css-javascript">Part 5: Using mobile-specific HTML, CSS, and JavaScript</a><br />
<a href="http://davidbcalhoun.com/2010/dealing-with-device-orientation">Part 6: Dealing with device orientation</a><br />
<a href="http://davidbcalhoun.com/2010/mobile-javascript-libraries-and-frameworks">Part 7: Mobile JavaScript libraries and frameworks</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2010/dealing-with-device-orientation/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Using mobile-specific HTML, CSS, and JavaScript (Mobile web part 5)</title>
		<link>http://davidbcalhoun.com/2010/using-mobile-specific-html-css-javascript</link>
		<comments>http://davidbcalhoun.com/2010/using-mobile-specific-html-css-javascript#comments</comments>
		<pubDate>Wed, 30 Jun 2010 03:27:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[mobile]]></category>
		<category><![CDATA[basejs]]></category>
		<category><![CDATA[blackberry.network]]></category>
		<category><![CDATA[blackberry.system]]></category>
		<category><![CDATA[capture api]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[gesture events]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[iui]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jqtouch]]></category>
		<category><![CDATA[media queries]]></category>
		<category><![CDATA[mozorientation]]></category>
		<category><![CDATA[orientationchange]]></category>
		<category><![CDATA[pastrykit]]></category>
		<category><![CDATA[touch events]]></category>
		<category><![CDATA[webkit]]></category>
		<category><![CDATA[xui]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=317</guid>
		<description><![CDATA[(updated June 27, 2011) Mobile-specific HTML Viewport tag Use the viewport tag to properly fit the content to the screen: &#60;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0&#34;/&#62; Tel scheme (to initiate phone calls) &#60;a href=&#34;tel:18005555555&#34;&#62;Call us at 1-800-555-5555&#60;/a&#62; Sms scheme (to initiate text messages) &#60;a href=&#34;sms:18005555555&#34;&#62; &#60;a href=&#34;sms:18005555555,18005555556&#34;&#62; &#60;!-- multiple recipients --&#62; &#60;a href=&#34;sms:18005555555?body=Text%20goes%20here&#34;&#62; &#60;!-- predefined message body [...]]]></description>
			<content:encoded><![CDATA[<p><em>(updated June 27, 2011)</em></p>
<h3>Mobile-specific HTML</h3>
<h4>Viewport tag</h4>
<p>Use the <a href="http://davidbcalhoun.com/2010/viewport-metatag">viewport tag</a> to properly fit the content to the screen:</p>
<pre name="code" class="html">
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;/&gt;
</pre>
<h4><a href="http://www.rfc-editor.org/rfc/rfc3966.txt">Tel scheme</a> (to initiate phone calls)</h4>
<pre name="code" class="html">
&lt;a href=&quot;tel:18005555555&quot;&gt;Call us at 1-800-555-5555&lt;/a&gt;
</pre>
<h4><a href="http://www.rfc-editor.org/rfc/rfc5724.txt">Sms scheme</a> (to initiate text messages)</h4>
<pre name="code" class="html">
&lt;a href=&quot;sms:18005555555&quot;&gt;
&lt;a href=&quot;sms:18005555555,18005555556&quot;&gt;              &lt;!-- multiple recipients --&gt;
&lt;a href=&quot;sms:18005555555?body=Text%20goes%20here&quot;&gt;  &lt;!-- predefined message body --&gt;
</pre>
<h4>Disable automatic telephone number linking</h4>
<pre name="code" class="html">
&lt;meta name=&quot;format-detection&quot; content=&quot;telephone=no&quot;&gt;
</pre>
<h4>iOS-specific HTML (some work on Android as well)</h4>
<p>You also have access to several <a href="http://developer.apple.com/library/safari/#documentation/appleapplications/reference/safariwebcontent/configuringwebapplications/configuringwebapplications.html">Apple-specific tags to use in your iOS applications (iPhone, iPad, and don&#8217;t forget the iPod Touch!)</a>.</p>
<pre name="code" class="html">
<!-- iOS 1.1.3+: this is the icon that's used when the user adds your app to the home screen -->
<!-- also works on Android! -->
&lt;link rel=&quot;apple-touch-icon&quot; href=&quot;icon.png&quot;/&gt;

<!-- iOS 2.0+: tell iOS not to apply any glare effects to the icon -->
&lt;link rel=&quot;apple-touch-icon-precomposed&quot; href=&quot;icon.png&quot;/&gt;

<!-- iOS 4.2+ icons for different resolutions -->
&lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;72x72&quot; href=&quot;touch-icon-ipad.png&quot; /&gt;
&lt;link rel=&quot;apple-touch-icon&quot; sizes=&quot;114x114&quot; href=&quot;touch-icon-iphone4.png&quot; /&gt;

<!-- iOS 3+: full-screen startup splash screen image (must be 320x460) -->
&lt;link rel=&quot;apple-touch-startup-image&quot; href=&quot;startup.png&quot;&gt;

<!-- enable full-screen mode (only when launched from home screen) -->
&lt;meta name=&quot;apple-mobile-web-app-capable&quot; content=&quot;yes&quot; /&gt;

<!-- controls the appearance of the status bar in full-screen mode -->
&lt;meta name=&quot;apple-mobile-web-app-status-bar-style&quot; content=&quot;black&quot; /&gt;
</pre>
<h4>Turn off autocorrect, autocomplete, and autocapitalize</h4>
<p>And also some handy attributes to turn off annoying autocorrect features:</p>
<pre name="code" class="html">
&lt;input autocorrect=&quot;off&quot; autocomplete=&quot;off&quot; autocapitalize=&quot;off&quot;&gt;
</pre>
<h3>Mobile-specific CSS</h3>
<h4>position:fixed and overflow:scroll</h4>
<p>Mobile browsers are now starting to support these basic CSS properties better.  Position:fixed will work on Android 2.2+ and iOS 5+.  Overflow:scroll works with one finger on iOS 5+.</p>
<p>Also, iOS 5 has additional CSS to give the native scrollbar and momentum/intertia to elements with overflow:scroll: <code>-webkit-overflow-scrolling: touch;</code></p>
<h4>Media queries</h4>
<p><a href="http://www.w3.org/TR/css3-mediaqueries/">Media queries</a> enable you to target specific features (screen width, orientation, resolution) within CSS itself.  Media queries themselves are actually quite old and are not mobile specific (they used to be popular for making a print-friendly version of webpages).</p>
<p>You can use them two ways: 1) inline in a CSS stylesheet or 2) as the &#8220;media&#8221; attribute in the link tag, which loads an external stylesheet.  The following is an example of inline CSS that&#8217;s applied only when the device is in portrait mode:</p>
<pre name="code" class="css">
@media all and (orientation: portrait) {
	body { }
	div { }
}
</pre>
<p>Here&#8217;s the same media query using the other method, which points to an external stylesheet (not recommended):</p>
<pre name="code" class="html">
&lt;link rel=&quot;stylesheet&quot; media=&quot;all and (orientation: portrait)&quot; href=&quot;portrait.css&quot; /&gt;
</pre>
<p>This is not recommended because it creates an additional HTTP request (bad for performance).  Also, in the case of screen orientation, the separate CSS stylesheet is NOT downloaded when the screen is rotated.</p>
<p>Here&#8217;s a few examples of using inline CSS:</p>
<pre name="code" class="css">
// target small screens (mobile devices or small desktop windows)
@media only screen and (max-width: 480px) {
  /* CSS goes here */
}

/* high resolution screens */
@media (-webkit-min-device-pixel-ratio: 2),
             (min--moz-device-pixel-ratio: 2),
             (min-resolution: 300dpi) {
  header { background-image: url(header-highres.png); }
}

/* low resolution screens */
@media (-webkit-max-device-pixel-ratio: 1.5),
             (max--moz-device-pixel-ratio: 1.5),
             (max-resolution: 299dpi) {
  header { background-image: url(header-lowres.png); }
}
</pre>
<p>Read more: <a href="https://developer.mozilla.org/en/css/media_queries">Media queries (Mozilla Developer Center)</a></p>
<h4>Miscellaneous CSS</h4>
<p>-webkit-tap-highlight-color (iOS): override the semitransparent color overlay when a user clicks a link or clickable element.  To completely disable it, set the value to &#8216;transparent&#8217; or &#8216;rgba(0,0,0,0)&#8217;</p>
<p>-webkit-user-select: none; &#8211; prevent the user from selecting text (also works on desktop WebKit)</p>
<p>-webkit-touch-callout: none; &#8211; prevent the callout toolbar from appearing when a user touches and holds an element such as an anchor tag.</p>
<h3>Mobile-specific JavaScript</h3>
<h4>window.scrollTo(0,0);</h4>
<p>This hides the address bar and takes advantage of the entire device screen.  You&#8217;ll have to set this in a timeout and make sure to get the timing right.  See <a href="http://remysharp.com/2010/08/05/doing-it-right-skipping-the-iphone-url-bar/">Remy Sharp&#8217;s post</a> for more details.</p>
<h4>window.matchMedia()</h4>
<p>(iOS 5+) Again, just as CSS media queries aren&#8217;t specific to mobile, they do come in especially useful for mobile, so it&#8217;s worth mentioning their JavaScript counterpart.  window.matchMedia() is a JavaScript-based version of media queries.  You can use <a href="https://github.com/scottjehl/Respond">respond.js</a> as a polyfill for devices that don&#8217;t support this functionality natively.</p>
<h4>navigator.connection</h4>
<p>(Android 2.2+) Determine if the phone is running on WiFi, 3G, etc.  Example:
<pre name="code" class="JScript">
if (navigator.connection.type==navigator.connection.WIFI) {
  // code for WiFi connections (high-bandwidth)
}
</pre>
<h4>window.devicePixelRatio</h4>
<p>Determine screen resolution (analogue to the CSS media query).  (iPhone 4 has the value 2, while Nexus One has the value 1.5).</p>
<h4>window.navigator.onLine</h4>
<p>Not strictly mobile, but helpful for apps to determine if they&#8217;re being run offline.</p>
<h4>window.navigator.standalone</h4>
<p>(iOS 2.1+): determine if it&#8217;s running in full-screen mode</p>
<h4>Touch and gesture events</h4>
<p><a href="http://developer.apple.com/safari/library/documentation/appleapplications/reference/safariwebcontent/handlingevents/handlingevents.html">touch events (iOS, Android 2.2+)</a>: touchstart, touchmove, touchend, touchcancel</p>
<p><a href="http://developer.apple.com/safari/library/documentation/internetweb/conceptual/safarivisualeffectsprogguide/InteractiveVisualEffects/InteractiveVisualEffects.html">gesture events (Apple only, iOS 2+)</a>: gesturestart, gesturechange, gesturend give access to predefined gestures (rotation, scale, position)</p>
<h4>Screen orientation (every 90 degrees)</h4>
<p><a href="http://ajaxian.com/archives/iphone-windowonorientationchange-code">orientationchange event</a>: triggered every 90 degrees of rotation (portrait and landscape modes).  The current orientation is available through window.orientation</p>
<h4>Device orientation (more fine-grained)</h4>
<p>The <a href="http://frontendstuff.com/javascript/examples/deviceorientation.html">deviceorientation event</a> will fire very frequently, and gives more fine-grained information about the device&#8217;s orientation in three dimensions.<br />
<a href="https://developer.mozilla.org/en/Detecting_device_orientation">MozOrientation</a> (or onmozorientation?) (Fennec/Firefox Mobile, Firefox 3.5+): also not strictly mobile.  Gives access to the device&#8217;s accelerometer (x-y-z orientation data), updated periodically.  Works on Android phones running Mobile Firefox.  On the desktop this works with laptops such as Thinkpads and MacBooks.</p>
<h4>devicemotion (shake gestures, etc.)</h4>
<p><a href="http://frontendstuff.com/javascript/examples/devicemotion.html">devicemotion</a> fires when the user shakes or moves their device.  Devicemotion taps into the accelerometer, which is fires off when the device accelerates.  Contrast this with the deviceorientation event, which taps into the device&#8217;s gyroscope (if it has one), which only measures the 3D angle orientation, even when the device is at rest.</p>
<h4>Media capture API</h4>
<p>While iOS is still lacking basic file inputs, Android is forging ahead, giving developers fine-grained control over content users can upload.</p>
<pre name="code" class="html">
&lt;!-- regular file upload (Android 2.2+, NO iOS) --&gt;
&lt;input type=&quot;file&quot;&gt;&lt;/input&gt;

&lt;!-- opens directly to the camera (Android 3.0+) --&gt;
&lt;input type=&quot;file&quot; accept=&quot;image/*;capture=camera&quot;&gt;&lt;/input&gt;

&lt;!-- opens directly to the camera in video mode (Android 3.0+) --&gt;
&lt;input type=&quot;file&quot; accept=&quot;video/*;capture=camcorder&quot;&gt;&lt;/input&gt;

&lt;!-- opens directly to the audio recorder (Android 3.0+) --&gt;
&lt;input type=&quot;file&quot; accept=&quot;audio/*;capture=microphone&quot;&gt;&lt;/input&gt;
</pre>
<h4>BlackBerry specific</h4>
<p>If you&#8217;re developing for a BlackBerry Widget, you have access to proprietary information through the <a href="http://www.blackberry.com/developers/docs/widgetapi/Summary_system.html">blackberry object</a> (which gives access to useful information such as blackberry.network [returns values such as CDMA and Wi-Fi] and blackberry.system).</p>
<p>You also have the option to use <a href="http://phonegap.com/">PhoneGap</a>, which augments JavaScript and gives you access to more phone features that native apps would have access to.</p>
<h3>Use a mobile-optimized JavaScript library</h3>
<p>I&#8217;ve created <a href="http://davidbcalhoun.com/2010/mobile-javascript-libraries-and-frameworks">a separate entry for the available mobile libraries and frameworks</a>.</p>
<p>Because smartphone browsers are standards-based, the aim of a JavaScript library on mobile is less towards API normalization and more towards providing an actual UI framework, usually to emulate the feel of native apps (and to provide easier workarounds to <a href="http://doctyper.com/archives/200808/fixed-positioning-on-mobile-safari/">lack of access to position:fixed</a>).  We&#8217;ve seen a few libraries released that emulate the iPhone UI, and in the future we might see libraries emulating the Android UI, as well as entirely new UIs.</p>
<p>There&#8217;s also a bit to be said about simply loading full desktop JavaScript libraries into mobile clients.  In my opinion this doesn&#8217;t particularly make sense, especially in a world where latency and bandwidth are so much more of a concern.  It doesn&#8217;t make sense to force the user wait longer and download code that&#8217;s ultimately useless to them (hacks for desktop browsers such as IE 6, etc).</p>
<h3>Take advantage of new stuff!</h3>
<p>While not specific to mobile, there&#8217;s a lot of new stuff in general that you can use.  If you limit yourself to the top smartphones (iPhone, Android, and maybe webOS), compared to the desktop you immediately have access to an even wider array of new stuff, especially many Webkit proprietary features, since most of these top smartphones have browsers based on Webkit.</p>
<p>-HTML: new tags (<a href="http://diveintohtml5.org/">HTML5</a> (I&#8217;m sure you&#8217;ve heard of it by now…))<br />
-CSS: <a href="http://webkit.org/blog/130/css-transforms/">2d transforms</a>, <a href="http://webkit.org/blog/386/3d-transforms/">3d transforms</a>, animation, <a href="http://border-radius.com/">border radius</a>, <a href="https://developer.mozilla.org/en/css/@font-face">custom fonts with @font-face</a>, etc.<br />
-JavaScript: <a href="http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/">strict mode</a>, <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Constants">constants</a>, <a href="http://davidbcalhoun.com/2009/javascript-tidbit-block-scope-with-let">block scope</a>, <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Date">Date.now()</a>, etc.</p>
<h3>Slides</h3>
<div style="width:425px" id="__ss_7424222"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/franksvalli/mobile-html-css-and-javascript" title="Mobile HTML, CSS, and JavaScript">Mobile HTML, CSS, and JavaScript</a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/7424222" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/franksvalli">franksvalli</a> </div>
</p></div>
<h3>More from the Mobile Web series:</h3>
<p><a href="http://davidbcalhoun.com/2010/viewport-metatag">Part 1: The viewport metatag</a><br />
<a href="http://davidbcalhoun.com/2010/the-mobile-developers-toolkit-mobile-web-part-2">Part 2: The mobile developer’s toolkit</a><br />
<a href="http://davidbcalhoun.com/2010/designing-buttons-that-dont-suck">Part 3: Designing buttons that don’t suck</a><br />
<a href="http://davidbcalhoun.com/2010/on-designing-a-mobile-webpage">Part 4: On designing a mobile webpage</a><br />
<a href="http://davidbcalhoun.com/2010/using-mobile-specific-html-css-javascript">Part 5: Using mobile-specific HTML, CSS, and JavaScript</a><br />
<a href="http://davidbcalhoun.com/2010/dealing-with-device-orientation">Part 6: Dealing with device orientation</a><br />
<a href="http://davidbcalhoun.com/2010/mobile-javascript-libraries-and-frameworks">Part 7: Mobile JavaScript libraries and frameworks</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2010/using-mobile-specific-html-css-javascript/feed</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
	</channel>
</rss>

