I'm finally getting around to writing HTML, and I'm faced with a catch: when I test my web page in a desktop browser, it looks just like I want it to, but when I test it in a mobile browser, it looks too small ( I mean, it looks just like the desktop version, but like I zoomed in at like 25%).
Is there a simple way to ensure that when the page is visited from a mobile phone, it has an appropriate size? If so, how?
For the zoom problem you have to add a meta-tag to indicate the values of the viewport .
Thus, for example, indicate that the width of the mobile browser is equal to the width of the device screen itself.
You have more information about this meta-tag on the MDN .
But there is really NO easy way to do it . And this is because it depends on many things, mainly on how your site is built.
Responsive design
Nowadays, since there is so much variety of resolutions, the ideal is either to use different versions of the web, or to use a responsive design ( Responsive Web Design ).
These types of designs are based on liquid designs (generally) in which then, through media-queries , it is indicated in CSS how the design should behave depending on the size of the screen.
A very simplistic example would be that in a resolution of
1024*768
four columns would be seen and in a resolution of320*480
one would be seen.Use media queries
The media query is the mechanism that CSS has to test certain aspects of the User Agent and the device where the document is being displayed. This includes the dimensions of the screen, but you can also tell if it's a printer, which is very useful.
Each media query is a logical expression that can be true or false, (it is similar to an IF).
There are 3 ways to use them:
Through the "media" attribute of the "link" element, that is, using different files
.css
for each "variant" of your page.By importing more styles into one CSS file, just like before, you use different CSS files for each variant.
Conditionally applying styles within a CSS (my favorite)
There are other variables that can be tested to decide which styles to use, for example screen orientation:
I recommend that you read the media queries draft to see what other options are available to you.
Well, although there are people who say that it is not easy, I use Bootstrap for large web apps and complement it with CSS3.
Now, if your app is small and well-formed, with a couple of tags
@media
that change the proportion you will have enough:First define a container for your html with the columns you need, these are going to be the responsive ones and the elements inside must be of relative position and sizes with percentage, NEVER WITH FIXED PIXELS!
Then define the size of the standard columns, those of medium screen and those of mobile (let's imagine 3 columns in large, 2 + 1 in the middle and 3 rows in mobile.
And voila, just to be clear, check out this live example on fiddle and resize the screen to see the 3 layouts that are created depending on the size.
NOTE: I have put an extra footer in the example to give information.
Bootstrap is undoubtedly a good candidate for you to make your page responsive, it is very easy to use, you can use it in "plain html" (without framework), it has good documentation, a large community and its general scheme is easy to understand and use.
There really isn't an easy way to do it automatically with a ready-made web page. You would need to do it following an adaptive design (or responsive as it is commonly known).
Basically you will have to play with the CSS media queries , with screen being the media type for desktop screens and handheld for mobile.
The thing would look like this for mobiles, for example:
And so the one you're already using for desktops:
For more information I leave you here a couple of references in Spanish, from Google and Mozilla .
I would like to add here
Foundation Zurb
, in its version 6. It is completely responsive, with many add-ons, grid layout, many pluginsjavascript
for mobile functions, it is completely focused on responsiveness.What can I tell you, it is surprising, it is implementing many of the technologies that have already become fashionable, as well as other new ones. It has quite a few libraries for modals.
It also already has a package
npm
andbower
forcli
, simple predesigned templates, if you want something basic, or complete, for heavy work, with files configured forgulp
orgrunt
, it would be missingwebpack
, compiled packagessass
andjs
, among others.I'm going to implement it myself now, but this time seriously, modifying
sass
only what I need and adding only the modules that are necessary, which will not only make it responsive, but also lighter.I would also have talked about framework like Bootstrap and I would have talked about using
width
ymax-width
with percentage values in CSS and using media query.The only thing I would like to add to the other answers concerns the use of the mobile-first approach .
In particular, prefer
@media (min-width: )
instead of@media (max-width: )
because the main style is for smaller devices, not the other way around:should be preferred to
I'm sorry but I only speak Italian and I used Google translate :)