• 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

    • Nice, hope they *finally* fixed the issue with the NTFS driver where the system would completely brick during large file copies using the built in driver. It's been broken for years requiring me to use the older, slower, NTFS-3G FUSE driver.
    • Windows 11 KB5094126 BSODing, freezing, forcing BitLocker lockout, breaks OneDrive, and more by Sayan Sen Microsoft released Windows 11 KB5094126 and KB5093998 last week as the latest Patch Tuesday updates. Following that the company also published the accompanying dynamic updates under KB5094149, KB5095971, and KB5094156. While Microsoft has so far not acknowledged any major problems with the release, some users online are running into problems. These range from OneDrive and Dropbox access issues, BitLocker recovery lockouts, to blue screens and BSODs. The most common one seems to be happening with HP systems wherein affected users say they hit 0xc0430001 BSOD (blue screen of death) error code after the KB5094126 update. We wonder if this could be related to the recent bug we covered on HP devices wherein the ongoing Secure Boot certificate updates are leading to similar issues. While we are not certain, users affected by this issue likely need to ensure that the boot.stl file is included on the installation media (such as a USB installer or ISO), if the above-mentioned dynamic updates are deployed. If this file is missing, computers may fail to boot from the installation media and could display the error 0xc0430001. This STL file is used by Secure Boot to verify that the boot files are trusted, so it must match the same Windows version and system architecture. To ensure the file is included, Microsoft recommends using the Update WinPE script, which automatically updates the image and handles the required files. Alternatively, you can manually copy the boot.stl file from the Windows\Boot\EFI folder on a Windows device and place it in the matching folder on your installation media before deploying the updated image. Aside from blue screening some users also note their systems have been freezing following the update. This could be happening to Lenovo PCs specifically. In the case of the OneDrive and Dropbox access issues, a user figured out that there could be a conflict with UAC. He explained: "Okay, so I did some digging, and in our environment KB5094126 breaks OneDrive and Dropbox in Explorer. I went through all our GPOs and found out that the combination of disabling UAC and having my user being a local admin breaks OneDrive in Explorer. ... If I enable UAC again, then it works, even with KB5094126 still installed." Hopefully, Microsoft will look into these issues. Source: Microsoft forum (link1, link2, link3, link4), Reddit (link1, link2, link3, link4)
    • It is when it's a desktop in my house though for a PC that's lightly used and not really important when it is. If it was a laptop, it would be a different story. The real solution is varied and begins starting at post #22 in that thread.
    • Win11Debloat 2026.06.14 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 2026.06.14 changes: This is a minor release that hopefully addresses the false positives in Windows Defender and Bitdefender that prevented users from downloading and/or running Win11Debloat. Refactor Get-RegFileOperations.ps1 to address false positives by @Raphire in #626 Add logging around WinGet app retrieval and increase timeout to 20s by @Raphire Download: Win11Debloat 2026.06.14 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • 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
      508
    2. 2
      +Edouard
      198
    3. 3
      PsYcHoKiLLa
      138
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!