• 0

Mobile Site Development Tips & Tricks


Question

I made this topic to highlight some tips and ideas for mobile website development.

Your free to post your own ideas/input below :D

Some tips/ideas/tricks I know:

1. CSS3 Media Queries

In most cases is making a custom page for mobile devices not needed, instead you can use Media Queries.

It's CSS3 that gives response to the width, height, etc of your device screen.

Here is a nice tutorial about this:

More advanced stuff:

What if you got a
navigation
at the top and want to only to show the '
header
' and
'navigation'
on the mobile website homepage?

And you want to show a home button instead of the navigation at the other pages?

A example
*
:

*Test it on a mobile device or on a modern webbrowser with a browser window of 480px or smaller.

Adding and removing the elements on the pages will be quite some work on big sites,

so why not get it done automatically trough JS?

Here you go:


if(document.URL != ('http://' + document.domain + '/')) {
$("html").addClass("mobile"); /*jQuery*/
}

[/CODE]

[/indent]

[indent=1]Above code adds the [i]'mobile'[/i] class to the [i]'html'[/i] element when you're not on the homepage, as example you're at [u][i]domain.com/something[/i][/u] instead of [u][i]domain.com[/i][/u].[/indent]

[indent=1]Now we can remove elements by using CSS:

[CODE]
html #container {
display: none;
}
.mobile #navigation {
display: none;
}
.mobile #container {
display: block;
}
.mobile #homebutton {
display: block;
}
[/CODE]

[/indent]

[indent=1]You can of course do a lot more as shown at the example above with Media Queries and this piece of JS, use your imagination![/indent]

[indent=1][i][b]2. Iphones[/b][/i][/indent]

[indent=1]These days a lot of people use a iphone, but what for resolution do these have?[/indent]

[indent=1]Most mobile phones are 320x480 and some are 480x640 and some are even 640x960![/indent]

[indent=1]The resolution for the iphone is '320x480' and for the iphone 4(s) '640x960'.[/indent]

[indent=1]So that would mean that we have to design our mobile sites for 640 pixel width screens too?[/indent]

[indent=1]Short answer: no[/indent]

[indent=1]As you have noticed is the iphone4 resolution exactly the 2 times higher then the iphone resolution.[/indent]

[indent=1]But the screen size(inch) is not 2 times bigger, which makes the pixels very small![/indent]

[indent=1]Those super sharp displays in iphone 4's are called [i]'retina'[/i] displays.[/indent]

[indent=1]As a result has apple made the design for the iphone 4 user interface different,[/indent]

[indent=1]as example: borders which were normally 1px width are now wider so they look still as 1px width on the iphone 4 device itself.[/indent]

[indent=1]We can do the same design idea as apple did without making a custom page for the iphone 4.[/indent]

[indent=1]To do this we're going to keep our mobile website just 480px width and add a meta tag to the html header which zooms the page in to 640px width.[/indent]

[indent=1]This will make everything 2 times bigger.[/indent]

[indent=2][i]Keep in mind that css styled elements are suggested for a smooth result at the 2 times zoom in,[/i][/indent]

[indent=2][i]if you use images and want them to stay sharp make sure to use a image 2 times bigger then the img tag width or at a div with a background-image an image 2 times bigger then the div and add 'background-size: div-width div-height;'[/i][/indent]

[indent=1]Now everything is 2 times bigger on the iphone 4 we also got bigger border width's which look smooth on the retina display![/indent]

[indent=1]Heres the meta tag:

[CODE]<meta name="viewport" content="width=device-width; initial-scale=1.0">[/CODE]

[/indent]

[indent=1]So after doing this we can just keep our 480px design and use it on the 640px iphone 4 :D[/indent]

[indent=1][i][b]3. Detect devices trough JS[/b][/i][/indent]

[indent=1]You want to know which device is viewing your webpage?[/indent]

[indent=1]In the following guide will be explained hwo you can see that, as example we will try to make the background of a [i]'#container'[/i] [i]red[/i] when the device is a android device.[/indent]

[indent=1]JS to use:[/indent]

[CODE]if(navigator.userAgent.match(/Android/i)){
$("html").addClass("android"); /*jQuery*/
}
else if(navigator.userAgent.match(/webOS/i)){
$("html").addClass("webos"); /*jQuery*/
}
else if(navigator.userAgent.match(/iPhone/i)){
$("html").addClass("iphone"); /*jQuery*/
}
else if(navigator.userAgent.match(/iPod/i)){
$("html").addClass("ipod"); /*jQuery*/
}
else if(navigator.userAgent.match(/iPad/i)){
$("html").addClass("ipad"); /*jQuery*/
}
else if(navigator.userAgent.match(/BlackBerry/i)){
$("html").addClass("blackberry"); /*jQuery*/
}[/CODE]

CSS for this example:

[CODE].android body #container {
background: red;
}[/CODE]

With above code we simply look up the userAgent string to see what device we've got and then

we add a class to the 'html' element so we can use it to style the page for a android device.

Above code can of course also be used in different ways, picking all the apple devices as example:

[CODE]if(navigator.userAgent.match(/(iPhone|iPad|iPod)/i)){
$("html").addClass("apple"); /*jQuery*/
}[/CODE]

[indent=1]_________________________________________________________________________________________________________________________[/indent]

[indent=1]If you got more suggestions feel free to add them below in the comments, I will try to write some more in next few days ;)[/indent]

Edited by Anaron
Link to comment
https://www.neowin.net/forum/topic/1048933-mobile-site-development-tips-tricks/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Is there an easy way to detect if the user is using a mobile device? (eg using the user agent string, or whatever it is called)

JS:

if(navigator.userAgent.match(/Android/i)){
$("html").addClass("android"); /*jQuery*/
}
else if(navigator.userAgent.match(/webOS/i)){
$("html").addClass("webos"); /*jQuery*/
}
else if(navigator.userAgent.match(/iPhone/i)){
$("html").addClass("iphone"); /*jQuery*/
}
else if(navigator.userAgent.match(/iPod/i)){
$("html").addClass("ipod"); /*jQuery*/
}
else if(navigator.userAgent.match(/iPad/i)){
$("html").addClass("ipad"); /*jQuery*/
}
else if(navigator.userAgent.match(/BlackBerry/i)){
$("html").addClass("blackberry"); /*jQuery*/
}[/CODE]

CSS example for a android device:

[CODE]
.android body #container {
background: red;
}
[/CODE]

Keep in mind that you have to write the code as a function to let it work!

A example JS file showing how to call and make functions: http://seahorsepip.org/js/js.js

And I agree with above guys that media queries are enough BUT if you want your site to have a more apple look when you're on a iphone and more android look when you're on a android device you have to use more then just media queries ^^

I will post some more tricks in next few days ^^

P.S.

Some small handy thing to know: using modernizr isn't required in most cases when you have jQuery, take a look here: http://api.jquery.com/jQuery.support/

This topic is now closed to further replies.
  • Posts

    • Anyway to download these versions without being on the Experimental builds?
    • Nothing is stopping you from continuing with your testing cadence. If updates are released every 2 weeks instead of 4, and you test once every 4 weeks, the exact same amount of patches will still be available for you in those 4 weeks. For example: Before 4th week - patch 1, 2, 3, 4 After 2nd week - patch 1 and 2 4th week - patch 3 and 4 Still the same amount after 4.
    • Everyone else has said it. I'm gonna say it - you don't know what you're talking about. I do. I have two laptops. One work, one personal. I have access to two more laptops - both personal. At home I manually update my personal laptop when I see on Neowin that there is an update - I carry on and only apply the updates when I am ready. My work one only updates when my workplace decides to send it - I carry on and only apply the updates (when they actually arrive, which is usually days after the release) when I switch off the laptop at the end of the day as usual. The two other personal laptops only get updated when I get to it which is rarely - the people who own them carry on using them until I get to it and update them. All of the browsers on all laptops are configured to restore the tabs when launched. Google and Microsoft have changed from 6 weeks to 4, and it looks like it's going to move to 2. None of these changes affect how any of these browsers on the laptops are used. Not one jot. My advice to you is stop panicking whenever you see an update. Just carry on with what you're doing. This even benefits you in a way - from your comment you sound like you don't like the changes or the frivolous new features - great - then carry on as before!
    • AMAZON needs to take total accountability for this.
    • Server Summit had a heap of announcements, ADCS changes are baller.
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      80
  • Tell a friend

    Love Neowin? Tell a friend!