• 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

    • The GEEKOM A8 mini PC is 20% off (lower than Prime Day pricing) plus Huge Storewide Sale by Steven Parker GEEKOM is back with a deal on a variant of its A8 Mini PC powered by AMD's Ryzen 7 8945HS, which came out in 2024 with a TDP of just 45W, with a base clock of 3.8 GHz and a Turbo Boost of 4.9 GHz; although we never reviewed this variant, we did check out the Ryzen 9 version. As a reminder of what you get, below are the specifications for this Mini PC. GEEKOM A8 Dimensions 112.4 mm x 112.4 mm x 37 mm Weight 450g CPU Ryzen 7 8745HS (8 cores, 16 threads, 16MB L3 cache, 3.8 - 4.9 GHz, TDP: 45 W) cTDP: 35-54W (Default 45W) Graphics AMD Radeon™ 780M Graphics 12 RDNA 3 Graphics Cores 2700MHz 768 shading units / stream processors (12 CUs), 48 texture mapping units, and 32 ROPs NPU XDNA architecture (Up to 16 NPU TOPS) Memory 16GB Dual-channel Crucial DDR5-5600MT/s SODIMM (up to 64GB) Storage 1TB NVMe M.2 (PCIe Gen 4.0 x4) Operating System Windows 11 Pro Bluetooth Bluetooth v5.2 Wireless LAN Wi-Fi 6E Kensington Lock No SD Card reader Yes (left side) Adapter 120W, 6.32A, 19V Power Adapter Front I/O Ports 2 x USB 3.2 Gen 2 Type-A 1 x 3.5mm front stereo headset jack Rear I/O Ports 1 x USB 3.2 Gen 2 Type-A 1 x USB 4 Gen 3 Type-C with Power delivery up to 15W (5V 3A) 1 x USB 3.2 Gen 2 Type-C 1 x USB 2.0 Type-A 2 x HDMI 2.0b 1 x 2.5G RJ45 LAN 1 x DC-in Deal Price $519 (buying links below) GEEKOM has two configurations of the A8, with the only difference being the slightly less-powerful Ryzen 7 CPU with half the storage (1TB) and DDR5 memory (16GB). This knocks $350 off the price compared to the $999 Ryzen 9 configuration. In both instances, a Windows 11 Pro license is also pre-loaded. As I said previously, this appears to be an update on the A7 with the only difference being the newer CPU. The packaging has changed quite a bit from the A7. Instead of dark colors, now the box is completely white, and the foam cushion has been replaced by a cardboard mould that the A8 sits in, above a small envelope that contains a thank you card and booklet that has guidance on all of the controls, how to access the A8 to swap out the SSD or memory, and safety information in several European languages. Upon removing the cardboard mould, you can find another cardboard compartment that contains the power lead, HDMI cable, VESA plate, and a bag of screws. What’s In The Box 1 x A8 Mini PC 1 x Power Adapter 1 x HDMI Cable 1 x VESA plate and bag of screws 1 x Envelope with booklet and Thank you card Unlike the A7, the VESA mount option is back with the A8. In short, you have everything you need to get started. All products sold by GEEKOM receive a 3-year free Warranty from the date you receive the product. If needed, you can RMA or return locally relative to your region (the U.S. has a U.S. warehouse, mainland E.U. has a German warehouse). GEEKOM A8 at GEEKOM U.S. for $519 was $649 (20% off) GEEKOM A8 at GEEKOM U.K. for £503 was £629 (20% off) GEEKOM A8 at GEEKOM CA for $735.20 was $919 (20% off) Use coupon code NWGKA820 when checking out. This flash deal expires on July 2. Next up is the highest savings on the A7 Max series of Mini PC in the Spring Sale. The GEEKOM [2026 Edition] A7 MAX with AMD Ryzen 9 7940HS, 16GB DDR5, and 1TB SSD. Operating System: Windows 11 Pro CPU Model: Ryzen 9 7940HS CPU Speed: 5.2 GHz Cache Size: 24 MB Graphics Card Description: Integrated Graphics Coprocessor: AMD Radeon 780M Memory Storage Capacity: 16GB DDR5 SSD: 1 TB We reviewed this Mini PC back in January, and praised it for its modern internals like a dedicated NPU and DDR5 memory; as such, it is more than capable of keeping up with today's offerings of Mini PC on the market. GEEKOM A7 Max at GEEKOM U.S. for $587 (was $699) 16% off GEEKOM A7 Max at GEEKOM U.K. for £551 was £689 (20% off) Use coupon code NWGKA7MAX when checking out. This flash deal expires on July 2. Huge Summer Sale If the above deals don't tickle your fancy, from today, there are deep discounts on a range of other GEEKOM products. From June 15 to June 30, the GEEKOM Official Store will be running its Summer Sale, with discounts starting from 15% off across the entire lineup, up to 50%! This is their biggest promotion of the year so far, offering pricing that is even lower than select Prime Day deals. You can check out the discounts at the dedicated Summer Sale landing pages below. GEEKOM U.S. Summer Sale GEEKOM U.K. Summer Sale What's more, all products from GEEKOM receive a 3-year free Warranty from the date you receive the product. If needed, you can RMA or return locally relative to your region (the U.S. has a U.S. warehouse, mainland E.U. has a German warehouse, the U.K. has a U.K. warehouse, Australia has an AU warehouse). While the Summer Sale ends on June 30, deals on the A8 and A7 Max will remain active until July 2.
    • Oh man, the memories.
    • Affinity by Canva 3.2.2.4557 by Razvan Serea Affinity by Canva is a free, all-in-one creative app combining vector design, photo editing, and page layout in a single platform. Originally developed by Serif, it’s now under Canva, offering professional-grade tools without subscriptions. Users get full access to Pixel, Vector, and Layout studios, plus ongoing updates. Designed for Mac and Windows, it empowers designers, illustrators, and content creators to work faster, smarter, and more creatively than ever before. Affinity is a unified, high-performance design platform combining vector, raster, and layout workflows. It offers fully non-destructive editing, advanced curve and shape manipulation, artboards, symbols, and seamless integration of pixel and vector content. The photo engine supports RAW editing, compositing, retouching, and batch processing with macro automation. Layout tools include long-form document support, typographic precision, navigational elements, CMYK-ready print, and Data Merge. Canva AI Studio adds generative tools (Fill, Expand, Edit), Depth Map, Super Resolution, and advanced portrait effects, accessible via Canva Pro or higher. Broad file compatibility and customizable workspaces ensure professional-grade efficiency. Advanced AI features like Generative Fill and Expand are unlocked in Affinity through the Canva AI Studio for users with a Canva premium plan (Pro, Business, Enterprise, or Education). Affinity is truly free. Every tool in the Pixel, Vector, and Layout studios is fully accessible, along with all customization and export options—no limits, no payments required. The app also receives free updates with new features and improvements. Your creativity remains yours. Affinity stores all your work locally on your device. Canva does not use any Affinity content to train AI or develop features, including anything created with Canva AI tools within Affinity. If you export or upload your work to Canva, you remain in full control. Data preferences can be reviewed or updated at any time in your Canva account settings. Why is Affinity free? Curious how this is possible? Here’s the philosophy behind it and how it works. Note: A free Canva account is required to use Affinity. Your account gives access to Affinity along with other Canva products and features. Download: Affinity 3.2.2.4557 | ARM64 | ~600.0 MB (Freeware) Links: Affinity Website | macOS | Screenshot 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
      513
    2. 2
      +Edouard
      205
    3. 3
      PsYcHoKiLLa
      136
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      85
  • Tell a friend

    Love Neowin? Tell a friend!