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.

NOTE: the site used in this example runs our company's own content management system for web designers, LightCMS, but the technique described here can be implemented in static websites as well as sites running on any CMS that, like LightCMS, provides you with full control over your HTML, CSS and JS.

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, the G1 and many Android devices. 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 our CMS allows us to use HTML standards, 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, G1 and many Android phones. 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, 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.

30 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

17. danny wrote:
hi there.

thank you very much for this tutorial, however i have not really accomplished my site goal in this way. i have used a stylesheet switcher based on the user agent. i am also using a CMS which is smarty based. i also have not specified an absolute width for my iphone css. i am using 100% width to guarantee proper placement.

my question relates to font sizes. i have tried using the desktop browser base font size of 12px. when i first viewed it in my iphone the text was way too small. i have upped it to 30px and now it looks ok. i am using em scales withink my H1 and H2 and P tags with the body having the base of 30px. can someone tell me why the iphone font size is so big but for the desktop browser and handheld it doesnt need to be that big at all?

my site is http://www.pembrokeshire.ac.uk/esw

cheers

Danny

March 31, 2010 @ 3:26 AM

18. Amine Ouahidi wrote:
Danny,

I think this is happening because you are not specifying the viewport width.
it would look something like this
You can find more info about the viewport here: http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html#//apple_ref/doc/uid/TP40006509-SW25

Hope this helps,
Amine

April 1, 2010 @ 9:32 AM

19. Brand wrote:
I tried to optimise it, but it is very difficult...

April 6, 2010 @ 4:23 PM

20. Christian Nguye wrote:
I love your codes. Thanks a bunch. :D

April 28, 2010 @ 2:46 PM

21. Ankur Datta wrote:
Hi, its a nice and informative article. However, i am quite a novice as far as mobile web development is concerned. I have a site already in place and now i want it to be mobile device compatible.

How should i go about it? do we i need to do re-code the entire site for mobile application separately?

Do I need to keep 2 separate versions on the server: one for normal web and another one for mobile devices?

A reply would be highly appreciated.

thnx,

Ankur

May 17, 2010 @ 4:47 AM

22. Amine wrote:
It mainly depends on how you use your website, but you can do it either way.
You could optimized your current site to be viewable on mobile devices like we did here in this tutorial, that would work great for a blog, or a website that gets updated frequently.
Or you could create a separate site specifically for mobile devices and redirect your users once they visit your website via a mobile device, that's usually easier to style and more customizable but could be time consuming depending of your website size since you will be recreating the content all over again.

Amine

May 17, 2010 @ 9:43 AM

23. Klara wrote:
Thank you for this tutorial. It's very helpful!

July 7, 2010 @ 10:18 AM

24. ROSSiDEAS wrote:
Thank you... working on a project right now... very helpful

July 25, 2010 @ 7:59 AM

25. Igors wrote:
Thanks for great tutorial! I will use it for my website mob.version!

August 6, 2010 @ 6:16 PM

26. Steve Markey wrote:
I used your tutorial on a site, and for iPhone it works fine, but in a Blackberry it still shows the original page, is there any way to get this to work with blackberry?

August 13, 2010 @ 11:46 AM

27. Jasper - Your Online Guy wrote:
Optimizing websites for mobile phones and other mobile devices is going to be added to my list of services very soon. I just wanted to let you know I appreciate the valuable info you share here is your post.

August 16, 2010 @ 2:43 PM

28. Ana Maria wrote:
Fabulous and very useful post for mobile webmasteres. Really helped me. Thanks for sharing.

August 30, 2010 @ 10:33 AM

29. Ana Maria wrote:
Fabulous and very useful post for mobile webmasteres. Really helped me. Thanks for sharing.

August 30, 2010 @ 10:33 AM

30. startbattery wrote:
Welcome to http://www.start-battery.com/ and thank you for visiting http://www.start-battery.com/ ! We have a lot of selection of rechargeable camera batteries,rechargeable camcorder batteries, laptop battery, and battery chargers for CANON, SONY, PANASONIC, JVC etc. Our batteries are made of high quality cells ,which offer the quality and capacity as their Original Equipment Manufacturer counterparts. We offer fast shipping for orders to customers. We use secure online payments via our secure server.

September 2, 2010 @ 4:26 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