Talk to a real, live person

Complete the simple form to the right, and one of our professional Internet consultants will be in touch. You’ll get free website advice and discussion with no obligation to purchase anything.

Or, call us toll-free

If forms aren’t your thing, just give us a call at 1-877-WEB-5585.

Never mind. Maybe later.

1 Your Contact Information

Please enter your name

Please enter your company

Please enter a valid email address

Please enter a valid telephone number

2 Project Summary


« Previous

3 Complete

Thank you for your interest. One of our professional Internet consultants will be in touch shorty.







Tutorial: optimizing your website for mobile devices

Since we announced our optimization of the Element Fusion website for mobile devices including the iPhone, we've had quite a few requests to share the steps that were taken to complete this task. While there are different ways to accomplish such a task, the following is a look at how we did it.

Phone considerations

For our purposes, we divided all mobile devices into three categories.

  1. Devices with screens that have widths between 320 and 480 pixels, that support CSS, and that support screen media types and media queries. This includes the iPhone and the G1. We will refer to this group collectively as the iPhone. These devices will be able to support all of the optimizations we put in place.
  2. Devices that support CSS and that either support the handheld media type or support the screen media type and media queries and have screens smaller than 320px wide. We'll create separate, simplified CSS rules for these devices since their screen size will limit what they are able to display.
  3. Devices that don't support CSS. These devices will render our content from the markup only. We don't have to provide any special optimizations for this group. Since we use standards-based XHTML, it will still render effectively on these devices.

Design

First, we had to decide which elements should be kept onscreen for the iPhone and which elements should be removed. Here's a look at the Element Fusion home page with overlayed notes about our decision-making process.

Once these decisions were made, we created a mockup to lay out the included elements on a screen that's 320 pixels wide.  Our mockup can be seen in the image below.

Notice the "Give us a call" and "Find us on the map" buttons in the footer of this mock-up. We're replacing the regular footer on the EF site with this special footer content which will only be shown on the iPhone. We'll cover this special situation a little further down.

Implementation

To implement our changes, we first set the viewport to the device's width. Do this by inserting the following line of code into the head of your XHTML template:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />

Next, we linked up a special stylesheet called mobile.css right below our initial screen stylesheet and set it to target our first group of devices, the ones with screens between 320 and 480 pixels. We could have targeted all screens smaller or equal to 480 pixels without specifying a minimum value, but in this case, we wanted to use a different stylesheet for any screen smaller than 320 pixels. Note that only devices that support screen media types and media queries will use this stylesheet, but that includes the iPhone and G1 phone. Here's the code we added to the head of our XHTML template:

<link media="only screen and (max-device-width: 480px) and (min-device-width: 320px)" href="mobile.css" type="text/css" rel="stylesheet" />

Then, we linked up another stylesheet called mobile_simple.css which would target our second group of devices. This stylesheet will not only target browsers that support the handheld media type, but also other browsers that support the screen media type and media queries and have screens smaller than 320 pixels. Here's the code we added to the head, directly below the previous link:

<link media="handheld, only screen and (max-device-width: 319px)" href="mobile_simple.css" type="text/css" rel="stylesheet" />

Development

Next, we created our mobile.css stylesheet to target the iPhone / G1 phone, overwriting the PC screen style sheet, changing dimensions, modifying backgrounds, and hiding or showing elements as defined by our mockups for the iPhone. This tutorial assumes knowledge of CSS to make such changes, so we won't get into all of the CSS rules here. However, you can view our stylesheet here to see what's under the hood.

We also created the mobile_simple.css stylesheet for smaller-screen devices. This stylesheet essentially strips out most of the content styling, making our website display in an almost "unstyled" form which is more accessible to other mobile devices with smaller screens. You can view this stylesheet here.

As a final step, we added this javascript to the head of our template to hide the URL Bar when the page loads on the iPhone.

<script type="application/x-javascript">
if (navigator.userAgent.indexOf('iPhone') != -1) {
addEventListener("load", function() {
setTimeout(hideURLbar, 0);
}, false);
}

function hideURLbar() {
window.scrollTo(0, 1);
}

</script>

Testing & troubleshooting

We used the emulators that come with iphone and Android SDKs to test our site. These emulators are free and available to the public. We also used some browser-based emulators.

The final product

Here's the resulting code added to the head of our template documents. To see this in context, you can view the source of our Element Fusion homepage and look at the head section.

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<link media="Screen" href="style.css" type="text/css" rel="stylesheet" />

<link media="only screen and (max-device-width: 480px) and (min-device-width: 320px)" href="mobile.css" type="text/css" rel="stylesheet" />
<link media="handheld, only screen and (max-device-width: 319px)" href="mobile_simple.css" type="text/css" rel="stylesheet" />
<script type="application/x-javascript">
if (navigator.userAgent.indexOf('iPhone') != -1) {
addEventListener("load", function() {
setTimeout(hideURLbar, 0);
}, false);
}

function hideURLbar() {
window.scrollTo(0, 1);
}
</script>

Special content for iPhone

As we noted on our mock-up above, we created a custom footer containing two special buttons that will exist only on the iPhone to provide special functionality unique to that mobile platform. The "Give us a call" button immediately dials our company number. Handy, since the user is browsing on their phone. The "Find us on the map" button launches the built-in Google client on the device and pinpoints our location.

Whereas all of the previous modifications were made by simply restyling and hiding content that already existed on our website, these new buttons are actually additional content that had to be added to the markup of our web pages. So, we added the following HTML code to the footer of all our templates.

<div class="iphone">
<ul>
<li id="iphone_call"><a href="tel:(405) 948-8300">Give us a call</a></li>
<li id="iphone_map"><a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=element+fusion&ie=UTF8&ll=35.594786,-97.053223&spn=5.760649,9.30542&z=7&iwloc=A">Find us on the map</a></li>
</ul>

<p>© 2009 Element Fusion, LLC. All Rights Reserved. <a href="http://www.embracewater.com/" title="Water content management system for business">Powered by Water</a></p>
</div>

Here are some important things to note about this content:

  1. The "iphone" class applied to the div surrounding this content allows us to hide it for PC browsers and to enable it only for our iPhone devices, using the stylesheets mentioned above.
  2. Note the syntax of the link reference for the phone number. This tells the iPhone to dial the phone number when the link is clicked.
  3. The Google maps link works just like every other Google maps link. The iPhone knows internally to launch them through the internal Google client.

16 comments (Add your own)

1. Andrea wrote:
Wow, this is *really* great stuff. Thanks very much for sharing this!

May 19, 2009 @ 3:40 PM

2. Michael Dick wrote:
Amine, great post on mobile web, I am glad to see EF supporting its cause! Looking forward to seeing EF supporting it more!

May 22, 2009 @ 11:15 AM

3. Amine Ouahidi wrote:
Thanks Guys.

May 26, 2009 @ 8:27 AM

4. Jeff wrote:
Great tutorial. One question: in the phone link above, is the ? in the number deliberate, or is it a rendering error?

Tel:(405) 948?8300

I imagine the original had an em dash, outside the character set?

June 9, 2009 @ 10:45 AM

5. Tim Wall wrote:
@Jeff, you are correct. That was just a rendering error. Thanks for the catch, I've corrected it in the post.

June 9, 2009 @ 11:12 AM

6. Dave Myron wrote:
I believe the correct value for the meta tag is device-width, not device-width-constant.

July 2, 2009 @ 1:35 AM

7. Amine Ouahidi wrote:
@Dave,

Yes that's correct. Thanks for pointing that out.

July 2, 2009 @ 8:07 AM

8. jack wrote:
www.selingmobiles.com

August 18, 2009 @ 3:01 AM

9. Alicia Trimble wrote:
I followed your tutorial, but I can't get the new footer buttons to go to the bottom of the screen in the actual footer section... any quick hints?

August 29, 2009 @ 10:30 PM

10. Sree wrote:
Hi,
Very useful stuff!!
Thank you very much.

September 4, 2009 @ 3:36 AM

11. Andy wrote:
Great post, I really loved it!

November 6, 2009 @ 8:21 AM

12. lisa wrote:
Hello - I enjoyed reading your article. I have been studying mobile web design for some time now and have read that the keyword !important can have a huge negative impact on the maintenance of a site.

http://reference.sitepoint.com/css/importantdeclarations/

I would like to know your thoughts on this. I see it used a lot in your css.

Thanks, lisa

November 16, 2009 @ 10:49 AM

13. Amine Ouahidi wrote:
@Alicia,
Sorry for the delayed reply, I am not sure why the buttons aren't going on the bottom of the footer for you, this sounds more like a CSS issue, and it also depends on where you placed them in the markup.
I would start troubleshooting it by checking the markup and making sure all the tags are closed properly and that you have the buttons placed in the right area where you would like them to be placed.
Hope this helps.

@lisa,
I wouldn't disagree with you, I think the keyword !important can have a negative impact on the maintenance of a site but I have to admit that sometimes it is the quickest way to adjust things or make changes, especially when dealing with multiple style-sheets.
Since we've optimized our website using the same markup, in other words we didn't create a new website just for mobile users, we wanted to use the same content and change the layout using different stylesheet, I used the important keyword to make sure that nothing in the markup would overwrite the mobile stylesheet.
Hope this make sense, Let us know if you have any questions.

Thanks.

November 16, 2009 @ 12:41 PM

14. lande wrote:
thanks a lot for this article. it's really helpful!

December 8, 2009 @ 3:17 PM

15. Kien wrote:
Excellent tut on designing for the iphone. Best one I've come across so far. Like it or not, the iphone is how many are viewing websites on the go now - might as well learn how to personalize the experience. Thx for the links under Testing & troubleshooting...a BIG help as I get my hands dirty. Do you know of any web based iphone emulators? Been searching with no luck.

February 26, 2010 @ 4:17 PM

16. Sildenafil wrote:
Great tutorial, thank you

March 12, 2010 @ 4:04 AM

Add a New Comment

Enter the code you see below:
code
 

Comment Guidelines: No HTML is allowed. Off-topic or inappropriate comments will be edited or deleted. Thanks.


Let’s Talk

Get free website advice from one of our professional Internet Consultants. Click or call us to get started.

1-877-WEB-5585