Reponsive website partially based on device instead of screen resolution - javascript

I've got my responsive website done with breakpoints setted up with #media queries. I've got couple of basic breakpoints
min-width: 1600px - for TVs and bigger resolution screens (that just center the whole page and make white margins on sides, nothing too significant)
max-width: 1024px - for tablets, there are many changes, especially because tablets has of corse touch screen, which desktops usually haven't. At all it looks pretty different.
max-width: 600px - for smarphones, there is also a lot of changes based especcialy on narrowing the content from two or four colums into one.
Now I found that might be a problem, because there are nowdays tablets which acts in the browser, as they have resolution width for example 1280px, but there are also still computers with width of monitor 1280px too, even smaller, so I can't change the breakpoint value for this.
I of course don't want the desktop version on tablet and tablet version on comuter, becase they're created not as much for resolution but more for the platform.
I know that there are things called user agents like WURFL, which seems to be perfect for this. But is there any way how to connect this value from WURFL (tablet, desktop, smartpohne...) with css #media queries.
For example detect in WURFL that the device is tablet and change the breakpoint value in CSS file, so it shows the tablet version, instead of desktop version? Or is there any other way with similar result as this theoretical solution?

There is no need identify the device, just use the right media queries.
#media only screen and (min-width : 320px) {
}
/* Extra Small Devices, Phones */
#media only screen and (min-width : 480px) {
}
/* Small Devices, Tablets */
#media only screen and (min-width : 768px) {
}
/* Medium Devices, Desktops */
#media only screen and (min-width : 992px) {
}
/* Large Devices, Wide Screens */
#media only screen and (min-width : 1200px) {
}

I think you are looking for this
jQuery code to detect mobile devices
http://www.webtrainingcentre.com/jquery/scripts/jquery-code-to-detect-mobile-devices/

Related

How do I detect browsers on mobile or on desktop? [duplicate]

This question already has answers here:
Detecting a mobile browser
(46 answers)
Closed 2 years ago.
When I presume that a css media query with max-width: 768px can make my web page responsive to fit a mobile device. However, iPhone X and later still load the desktop version, because recent phones have even higher resolution than an old desktop.
How do I detect a mobile device not relying on the screen resolution, with Vanilla javascript, pure HTML and CSS?
Pixels don't actually work based on the real density of the screen. The pixels we use are Density independance and the get scalated depending on the screen. If you follow up the media query Bootstrap uses you can see they actually set it as max-width: 575.98px for mobile devices.
// Extra small devices (portrait phones, less than 576px)
#media (max-width: 575.98px) { ... }
// Small devices (landscape phones, less than 768px)
#media (max-width: 767.98px) { ... }
// Medium devices (tablets, less than 992px)
#media (max-width: 991.98px) { ... }
// Large devices (desktops, less than 1200px)
#media (max-width: 1199.98px) { ... }

Different CSS sheet depending on screen size, using javascript

I have created 2 different versions of my website, one for laptop screen size and one for mobile screen size (tablet screen size, on the way). So, I was wondering if you could tell me how am I supposed to use javascript (because I have a feeling that it could be done through javascript) in order to have as a main webpage file the one that is supposed to be for laptops and in case the user opens my website through their mobile, the file that will open will be the one for mobile size screens. I hope you get my question.
Thank you very much in advance!
PS: I read something about #media but still trying to figure things out.
PS2: If you need me to post any code, let me know which part you might need and I will gladly post it.
You can use media queries to import different CSS files, depending on screen size.
This will work without javascript and would be the prefered approach in my opinion.
Your site could have the following code in a tag in the header:
/* Everything 1000px and lower */
#import url("./mobile.css") only screen and (max-width: 1000px);
This will load another stylesheet if the media query is satisfied.
You can read more about media queries here:
https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
Use media queries in your CSS file:
#media screen and (max-width: 992px) {
body {
background-color: blue;
}
}
#media screen and (max-width: 600px) {
body {
background-color: olive;
}
}
Examples and more, here
when it comes to responsive, avoid as much as you can javascript, you can do it with css media queries, you can learn more about it here:
https://www.w3schools.com/css/css3_mediaqueries_ex.asp
or you can go for the framework approach, bootstrap for example.
I think if you use bootstrap, you can easily manage you requirement quickly.
Please refer following link to get idea -
https://getbootstrap.com/docs/3.3/css/
You can use media query to handle css on you web page
// For Ipad Portrait and Landscape
#media screen and (min-width: 768px) and (max-width: 1024px) {
}
// For iPhone and smaller devices
#media screen and (max-width: 767px) {
}
also to handle javascript you need to use device width in jquery
if ($(window).width() < 767) {
alert('Less than 767');
}
else {
alert('More than 767');
}

Media Queries: How to target only tablet?

I have been doing some research on media queries and I understand the concept of mobile-first design.
I know, that there are lots of questions regarding media queries but none of them targets my specific question.
Also I understand the concept of structuring your stylesheets with media queries like this:
/* Small devices (tablets, 768px and up) */
#media (min-width: 768px) { ... }
/* Medium devices (desktops, 992px and up) */
#media (min-width: 992px) { ... }
/* Large devices (large desktops, 1200px and up) */
#media (min-width: 1200px) { ... }
However I really need a way for my stylesheets to target only tablets regardless of the browser resolution (width & height). Checking the browser-width is simply not safe enough (and definitely not what I am searching for) in 2015 where some tablets have a bigger resolution than older desktops.
Here is a list of things I have tried so far:
I made a list of "common" tablet-resolutions and simply specified
detailed media queries. This approach failed after I found tablets
with 1024px width (which is the same as older desktop browsers).
I read a lot about media queries and found out that modern browsers (especially on tablets) sometimes ignore specifications like "#media only screen" and that most values of a media query are deprecated.
1.) Is there a safe way to target tablets regardless of their resolution with media queries?
2.) Is there any way to use JavaScript/JQuery to find out if the Browser is used on a Tablet (touchscreen)?
Maybe there is a certain css property OR JavaScript function that is only "triggered" on tablets (I am out of ideas)? Thank you for your help.
You can use this:
#media only screen and (min-device-width: 768px){}
For ref : Media Queries: How to target desktop, tablet and mobile?
Short answer: no.
I would say, what is a tablet nowadays? Some phones are so big that they seem more like tablets and some tablets are so small that they overlap the bigger phones market...
So I would say, go for feature detection, target the features that you are interested in, and the best way for me, apart from media queries, is Modernizr.
You can take a look at their custom builds page and literally pick the features that you are interested in (touch events? geolocation?), knowing that you got the support of a reliable, well tested library.

Icon size using media queries

I am developing an app which runns on mobile and desktop browsers. I'm trying to find a sollution that could fix my icon size. I want icons to be small on desktop browsers but bigger on mobile browsers.
Can media queries detect screen density and if so can screen desinity determine my icon size?
And another thing: Is it better to detect screen size or density and adjust icons depending on the better choice.
Yes there is a media query that detects pixel density. Here is an example:
#media (-webkit-min-device-pixel-ratio: 2),
(min-resolution: 192dpi) {
/* Then you would change your background image and background image size here */
}
There is also a ton more info in this article at CSS-Tricks.
The question of whether to target device size or pixel density is tricky: it will depend on the specifics each time, and there's nothing to say you shouldn't combine both in your site. The media query you want to cover everything is:
#media (min--moz-device-pixel-ratio: 1.5),
(-o-min-device-pixel-ratio: 3/2),
(-webkit-min-device-pixel-ratio: 1.5),
(min-resolution: 1.5dppx) {
/* your retina rules here */
}
...which is probably the most helpful if you're sticking with PNGs rather than SVG graphics or an icon font. I recommend looking at retinafy.me - it's a useful resource (but not free), which is where that code came from.

Device orientation metadata

I have designed a website using media-query.
In the browser it is looking perfect. But when i am checking in the tablet or in mobile device, in portrait mode it is good,
but when I am changing the device orientation portrait to landscape then it is not looking perfect. So my doubt is "is there any meta-data to disable the device orientation, so even though user change the device orientation then the content will not rotate."
thanks,
naresh kumar.
If i understood you properly, you no need to disable the orientation but rather you can adjust screen elements even when the device is rotated. You can use the below queries:
#media all and (orientation: portrait) { ... }
#media all and (orientation: landscape) { ... }
Because a good design should fit in all devices and in all orientations perfectly...

Categories