Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect mobile browser for screen size?

I'm working on a site that needs to work on desktop and mobile. Right now I have a main content div that is set to 70% of the screen width. However, I feel that this is to small for mobile devices (like phones, not so much tablets) and want to up it to 90 or 95% How can I do this (say for screen sizes smaller than 5 inches) without using terribly unreliable browser sniffing? I hear the mantra "feature detection feature detection feature detection" over and over, and I understand why that's a good thing...but I don't know what "feature" to detect for this problem...

Thanks.

like image 849
Esaevian Avatar asked Jul 01 '13 16:07

Esaevian


People also ask

How do I find my browser screen width?

Use window. innerWidth and window. innerHeight to get the current screen size of a page.

What is the best way to detect a mobile device JavaScript?

The Window. matchMedia() is one of the best properties for detecting mobile users with JavaScript.

How do I know if a site is open on mobile or desktop?

We can use the CSS media queries to check whether the website is opened inside a web browser or a mobile browser. This information can be fetched using the min-width and the max-width of the webpage.


1 Answers

You can use CSS:

@media screen and (max-width:500px) {
  /* smaller screens */
}

@media screen and (max-width:960px) {
  /* bigger screens */
}
like image 152
Naftali Avatar answered Oct 07 '22 16:10

Naftali