简体   繁体   中英

Detect small (mobile) screen in Javascript (or via css)

I want to make some of the fonts on my website larger, if a visitor is using a small screen. Ideally without jquery, as I want to do this early on in the page load, and I don't want to load jquery until later, for faster loading.

The best I have come up with, is to check for screen size. But this is far from perfect. An iphone4 has relatively large size, but small screen, while some old netbook might have a smaller resolution but a larger screen. I guess what I really want is some variant of screen "DPI".

If there is some css way of saying "on a small screen do this, else on a large screen do that" that would work too.

In CSS2 there's a media property and in CSS3 this can be used to do media queries . It's not supported on all browsers, but it may be okay to use since your small devices like iPhone etc do support it.

 @media screen and (min-width: 781px) and (max-width: 840px) {
    body {
      font-size: 13px;
    }
  }

This site doesn't care about IE, try it in FF or Safari, change the browser width and notice how the width changes using this property.

Media Queries are the key and are a lot of fun to use.

See http://jsfiddle.net/neebz/kn7y3/ and change the width/height of the 'Result' panel to see it working.

Example taken from : http://www.maxdesign.com.au/articles/css3-media-queries/media-sample/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM