'Detect if a browser in a mobile device (iOS/Android phone/tablet) is used
Is there a way to detect if a handheld browser is used (iOS/Android phone/tablet)?
I tried this with the goal to make an element half as wide in a browser on a handheld device but it doesn't make a difference.
width: 600px;
@media handheld { width: 300px; }
Can it be done and if so how?
edit: From the referred page in jmaes' answer I used
@media only screen and (max-device-width: 480px).
Solution 1:[1]
Here's how I did it:
@media (pointer:none), (pointer:coarse) {
}
Solution 2:[2]
Don't detect mobile devices, go for stationary ones instead.
Nowadays (2016) there is a way to detect dots per inch/cm/px that seems to work in most modern browsers (see http://caniuse.com/#feat=css-media-resolution). I needed a method to distinguish between a relatively small screen, orientation didn't matter, and a stationary computer monitor.
Because many mobile browsers don't support this, one can write the general css code for all cases and use this exception for large screens:
@media (max-resolution: 1dppx) {
/* ... */
}
Both Windows XP and 7 have the default setting of 1 dot per pixel (or 96dpi). I don't know about other operating systems, but this works really well for my needs.
Edit: dppx doesn't seem to work in Internet Explorer.. use (96)dpi instead.
Solution 3:[3]
Detecting mobile devices
Related answer: https://stackoverflow.com/a/13805337/1306809
There's no single approach that's truly foolproof. The best bet is to mix and match a variety of tricks as needed, to increase the chances of successfully detecting a wider range of handheld devices. See the link above for a few different options.
Solution 4:[4]
I believe that a much more reliable way to detect mobile devices is to look at the navigator.userAgent string. For example, on my iPhone the user agent string is:
Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 Mobile/14F89 Safari/602.1
Note that this string contains two telltale keywords: iPhone and Mobile. Other user agent strings for devices that I don't have are provided at:
https://deviceatlas.com/blog/list-of-user-agent-strings
Using this string, I set a JavaScript Boolean variable bMobile on my website to either true or false using the following code:
var bMobile = // will be true if running on a mobile device
navigator.userAgent.indexOf( "Mobile" ) !== -1 ||
navigator.userAgent.indexOf( "iPhone" ) !== -1 ||
navigator.userAgent.indexOf( "Android" ) !== -1 ||
navigator.userAgent.indexOf( "Windows Phone" ) !== -1 ;
Solution 5:[5]
Many mobile devices have resolutions so high that it's hard to distinguish between them and much larger screens. There are two ways to deal with this problem:
Use the following HTML code to scale the pixels (grouping smaller pixels into groups the size of the unit pixel - 96dpi, so px units will have the same physical size on all screens). Note that this will affect the scale of pretty much everything in your website, but this is generally the way to go when making sites mobile-friendly.
<meta name="viewport" content="width=device-width, initial-scale=1">
Alternatively, measuring the screen width in @media queries using cm instead of px units can tell you if you're dealing with a physically small screen regardless of resolution.
Solution 6:[6]
CSS Level 5 Media Queries are best for this
below code styles css for touch screen devices except old androids and windows touch screens because windows touch screens are hover-able devices with mouse pointer...
@media (hover: none) { /* css for touch screen devices except old androids and windows touch screens because windows touch screens are hover-able devices with mouse pointer... */ }
Solution 7:[7]
I know this is an old thread but I thought this might help someone:
Mobile devices have greater height than width, in contrary, computers have greater width than height. For example:
@media all and (max-width: 320px) and (min-height: 320px)
so that would have to be done for every width i guess.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | GrayFace |
| Solution 2 | |
| Solution 3 | Community |
| Solution 4 | Jesse Heines |
| Solution 5 | potato |
| Solution 6 | |
| Solution 7 |
