Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I support more mobile viewport widths on CSS3-enabled website and force mobile browser to use the proper width?

I have website with fixed width of 1000px. I want to support 2 smaller widths of this page. I have succesfully done this through CSS media-queries like this:

@media only screen and (min-width : 800px) and (max-width : 999px) {
  #content { width:800px; }
}
@media only screen and (max-width : 799px) {
  #content { width:600px; }
}

Now when I test my webpage on Android browser (2.3.3), it uses viewport width of 1000px so it displays website in full width. But it would be much better, if it would chosen viewport width of 800px, because webpage would display more optimized for device with smaller displays.

I know I can set viewport width using e.g.: <meta name="viewport" content="width=800px" /> or to set width coresponding to device physical width using: <meta name="viewport" content="width=device-width" />, but none of these would work. Because either it would force all mobile devices to always use 800px viewport in first case, or it would use physical width of pixels on device in second case, but that wouldn't work out too, because in portrait mode (how most people surf mobile web and use phone) that would be 300px or at most 400px, which is too small and user would have to scroll horizontally a lot.

So actually my question is this - can I support and possibly force mobile browsers to use different viewport widths depending on their actual screen size ? I don't know if I explained it properly enough, so I will get example what I want to achieve:

For mobile devices with physical width of 400px (modern devices with bigger displays) I would like to force viewport of 800px width and possibly with scale 0.5 so on first page visit would be displayed full without need of horizontal scrolling and with user option to zoom in to parts of the page.

For mobile devices with physical width of 300px ( some middle-class and low-end devices ) or smaller - I would like to force viewport of 600px width and possibly with scale 0.5 so on first visit would be displayed full or at least most of it on screen with little horizontal scrolling needed. And of course, user could zoom in to parts of the page.

Is this possible set up using just CSS or CSS3 ? I can imagine JS solution, but I would like to implement this just using CSS.

Thank you for any help in advance.

like image 917
Frodik Avatar asked Dec 31 '11 08:12

Frodik


People also ask

How do I get the width of a viewport in CSS?

You can use the window. innerHeight property to get the viewport height, and the window. innerWidth to get its width. let viewportHeight = window.

Which will set the width of the element as viewport width from the following?

Setting The Viewport The width=device-width part sets the width of the page to follow the screen-width of the device (which will vary depending on the device). The initial-scale=1.0 part sets the initial zoom level when the page is first loaded by the browser.

How do I change my desktop view to mobile view in HTML?

Launch the Google Chrome browser and go to the site you want to view. Hit F12 on your keyboard to access DevTools. When the mode is turned on, click the “Toggle Device Emulation” icon. You can choose from a list of iOS and Android devices to emulate them.

How do you adjust the height of a viewport?

A simple way to solve this is to use the viewport percentage unit vh instead of %. One vh is defined as being equal to 1% of a viewport's height. As such, if you want to specify that something is 100% of the latter, use " height: 100vh ". Or if you want to make it half the height of the viewport, say " height: 50vh ".


1 Answers

I do understand some part of your question...I have mainly worked on iPhone / iPad devices and less on Android ones...

I would recommend using

<meta name="viewport" content="width=device-width" />

to set the viewport based on devoce width..

Now even though you say, it would not work on orientation change, on the iPhone/iPad, the device width is always 768px whatever be the orientation.

So atleast on those devices, setting to device width would be fine..

For the Android devices, i am not really sure if the device width is returned different in each orientations.. My guess is it should be the same.

Could you please try setting the meta tag as above and see if it works fine. if no, i would be able to suggest some other way..

Thank you.

like image 151
copenndthagen Avatar answered Sep 26 '22 01:09

copenndthagen