Web Development Blog

Twitter

12 Jun, 2009

Making a website iPhone-friendly using CSS

Web Development » CSS, Design, Tutorials » Making a website iPhone-friendly using CSS

Unlike any other mobile web browsers, iPhone comes with Safari browser which makes it possible to view any website that works on Safari. However, the challenge is that the screen size of an iPhone is not as big as a traditional monitor and therefore, if you have got a website which has a width of say, 1000px, the website will appear really small and unreadable on an iPhone screen.

Screenshot of Web Development Blog on iPhone
Screenshot of Web Development Blog on iPhone

Here are the steps to make a website iPhone-friendly using just CSS.

1. Create a separate stylesheet for iPhone

Tips:
1. Hide unnecessary elements
On this blog, I set the following elements to display: none in CSS. Use display: none rather than visibility: hidden because visibility: hidden will also hide the content inside an element and not hide the element completely.
2. Use a fluid layout
Set the width of your main container (or any other containers) to 100% rather than a specific width in pixel.

For example:

body {
background-color: #fff;
}

.header, .footer {
width: 100%;
}

.sidebar {
display: none;
}
<link media="only screen and (max-device-width: 480px)" href="/iphone.css" type= "text/css" rel="stylesheet" />

The viewport of an iPhone is 320 pixels in portrait orientation and 480 pixels in landscape orientation.

2. Specify meta data for viewport

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

Here, I am not specifying any width because I am using a fluid layout. Setting the user-scalable property to 0 (false) ensures that the user isn’t able to zoom in or out the website. Refer to Safari HTML Reference – Supported Meta Tags to see any other meta tags that you might want to use.

3. Create an icon

This is very much like a favicon that will be used when the user add your website to their home screen. Use the following code to specify a custom icon. The icon should be at least 57×57 pixel.

<link rel="apple-touch-icon" href="/iphone.png" />

apple-touch-icon
An example of apple-touch-icon for Facebook

apple-touch-icon
An example of apple-touch-icon for Web Development Blog

4. Putting it altogether

Add all of the above to the <head> section of your website.

<!-- Start iPhone -->
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<link rel="apple-touch-icon" href="/iphone.png" />
<link media="only screen and (max-device-width: 480px)" href="/iphone.css" type= "text/css" rel="stylesheet" />
<!-- End iPhone -->

For WordPress (template specific)

<!-- Start iPhone -->
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
<link rel="apple-touch-icon" href="<?php echo bloginfo('template_url'); ?>/iphone.png" />
<link media="only screen and (max-device-width: 480px)" href="<?php echo bloginfo('template_url'); ?>/iphone.css" type= "text/css" rel="stylesheet" />
<!-- End iPhone -->

A few things to note

  • The overflow CSS property does not work very well on iPhone. The scrollbars aren’t displayed but you can use two fingers and scroll across the area.
  • This solution does not make the website light-weight because all elements on your website are still loaded. However, it does look neater and cleaner because many elements are hidden via CSS using display: none.
  • The ideal solution is to develop a special version of the website for iPhone and essentially reduces the bandwidth required to load the website.
Interested in getting an iPhone friendly website for your company or organisation? Contact us at Anansi Web Development to find out how we may be able to help you.

Other similiar posts that you might be interested in:

9 Responses to "Making a website iPhone-friendly using CSS"

1 | Levon Gravett

June 21st, 2009 at 6:12 am

Avatar

Thank you for this really helped me a lot. I have one question tho as I am nee to mobi sites. How would I go about setting up the domain for the site I would like to have it as mobile.site.com how would I set this up so that the mobile device picks it up? Your help would be greatly appreciate. Many thanks. Levon

2 | eisabai

June 22nd, 2009 at 9:19 pm

Avatar

Hi Levon,
If you want to redirect your visitors to a different address/domain based on what browsers they are using, you can use this mobile user agent detection script: http://detectmobilebrowsers.mobi/

4 | Andy from Brand Aspect

September 18th, 2009 at 4:41 am

Avatar

This is a really useful post, but the key as always is planing your website. Hiding stuff may not be the best solution but it will be quicker. Its probably going to be easier to plan content structures for all media types from scratch. Well that rather than trying to create a stylesheet for an existing site on the basis that background image substitution would be one of the quicker ways to style using alternate stylesheets. Anyhow well done on the post mate.

5 | Optimising for iPhone Using CSS « Blog Relations

September 24th, 2009 at 8:33 pm

Avatar

[...] some simple optimisation of this site for iPhone using only CSS.     I picked up some good tips here and [...]

6 | Optimizing the Web for the iPhone « Adventures in Internet Technology

November 6th, 2009 at 5:29 am

Avatar

[...] came across this fantastic resource.  Finally, for information on CSS for the iPhone, I found this site. Possibly related posts: (automatically generated)No Title Categories: Uncategorized [...]

7 | iPhone Web App Resources | Ash White – MIST7500

November 6th, 2009 at 1:02 pm

Avatar

[...] Make a Website iPhone-friendly Using CSS [...]

8 | J

January 6th, 2010 at 3:15 am

Avatar

“Unlike any other mobile web browsers, iPhone comes with Safari browser which makes it possible to view any website that works on Safari.”

Ever heard of Opera and Opera Mobile? :)

9 | Star Config web design sydney

January 18th, 2010 at 5:51 pm

Avatar

thank you for explaning to us how to make it website i phone friendly using css, it is really great idea, i would like to try it

Comment Form


Interests

Web development, Web design, Open source technologies, Portal development, APIs, Web services, Social media applications, Search engine optimisation, Mobile application development, iPhone Apps, Web 2.0, Web 3.0, Latest Internet technologies