With so many people in the world using smartphones to surf the web, more and more webmasters are looking for ways in which they can make their websites mobile-friendly. This usually means modifying their sites for the smaller screen size found on such devices, either by providing a separate page that can be viewed comfortably there, or, more commonly, making their websites automatically adapt by shrinking things and moving stuff around. The latter method, often referred to as "responsive web design", is described in this tutorial series.
Since this tutorial deals with the changes you need to make to your website's low level code, you will need to know some HTML and CSS. You do not need to be an expert or anything like that, but some knowledge is necessary, otherwise this tutorial will be opaque to you.
Incidentally, if you are here because you thought this article is about designing a website from scratch, please read How to Create a Website instead.
In responsive design, we will present the same web page that desktop or laptop computer users see to your mobile audience. Only the Cascading Style Sheets, or CSS, will be different. That is, browsers on desktop/laptop computers will render the page using one set of CSS instructions, while those on mobile phones another.
This method of working not only saves you the labour of creating a different set of pages for each type of user, but also the hassle of maintaining those 2 sets over the years, trying to keep them in sync.
For the purpose of this article, to avoid having to qualify everything I say, making things even more wordy than it needs to be, I will use the following shorthand. When I say either "desktop" or "computer" here, I mean a desktop or laptop computer, and not a smartphone or tablet (even though the latter two are actually computers too). And when I say a "mobile device", I mean a smartphone, a tablet with a small screen, and the like, and not a laptop computer (even though the latter is also portable). Without this shorthand, the article is going to be even more difficult to read than it already is, with multiple sentences just to explain what I mean when I say these terms. I also tend to use "smartphone" synonymously with "mobile device" here, so that the article doesn't sound too monotonous.
The browsers of modern smartphones are written with the knowledge that websites are traditionally designed for computer monitors. As such, it adapts by pretending to the website that it has a computer-sized screen and scaling everything to fit in it. For example, Safari on the iPhone 5 pretends that it has a screen width of 980 pixels by default, even though its real size is 320 pixels (in portrait mode). So if you were to design a website with a fixed width of (say) 730 pixels, its entire width will fit into your mobile phone's screen, even though the latter isn't that wide. The browser accomplishes this by shrinking your website so that everything becomes really small. If the user needs to read anything, they will have to zoom in the relevant portions. You can see this effect by going to the fixed width demo page with your smartphone. That particular page has a fixed width of 730 pixels, and is deliberately designed not to adapt to your use of a mobile phone.
Since this default of pretending that the device has a width of 980 pixels and automatically scaling content
defeats our attempt to manually create a comfortable experience for mobile users, we have to override it before we can
do anything meaningful. To do this, add the following HTML tag to the
<HEAD> section of your web page:
The viewport meta tag above instructs the browser to use the actual device width with a scaling factor of 1. That is, it is not to pretend that it has some other width, nor is it to scale the content so that it fits into the existing window. Everything is to be used as-is. This instruction makes mobile browsers behave exactly like their desktop counterpart.
Now that we have got the mobile phone's browser to refrain from resizing things behind our back, we have to adapt to its small screen manually. While this seems like a step backward, it actually allows us to do things in a more appropriate way than the phone's automated facility: for example, we can resize the things that can be resized (eg, images), while leaving alone others that shouldn't be resized (like the words). To make space for this, we can send elements that are not so crucial to the bottom of the screen. For example, if you were to read any article on thesitewizard.com, including this one, on a mobile phone, you will find that my navigation menu (ie, the list of buttons) that is normally in the left column in a desktop browser, is positioned at the bottom of the page on a smartphone. I figured that since the user is on this page, his/her primary purpose is to read the article. As such, I put the article at the top so that visitors can get to it immediately.
To accomplish magic like this, we need some way to detect the screen size. Modern browsers provide this facility in the form of a "media query".
A media query in a style sheet looks something like this:
Any CSS enclosed within the curly brackets of that "
@media screen and (max-width:320px)" test will
only apply to screens that have a maximum width of 320 pixels. You are, of course, not restricted to testing for a width of
320 pixels. The latter is merely a figure I picked for this example. You can test for
max-width of any size. You can even test for range of sizes as well, such as in the following code.
CSS rules that are not enclosed within a "
@media" section apply to everyone. And code that is enclosed
within a specific "
@media" section will only be used when the conditions of the query are met.
If you have multiple conditions that must be met simultaneously, connect them with "
and" as in the examples given.
You can have multiple media query blocks, each of which will only be applied when the conditions for that block are met.
Note that the above is just an example meant to illustrate the use of multiple blocks of media queries. My choice of the numbers used there is arbitrary, so don't spend time puzzling over them.
You can also put your media queries in the
<link> element itself, so that an entire stylesheet
is only applied when that particular set of conditions is met. For example, the following loads 3 style sheets,
one supposedly for mobile devices in portrait mode, another for their landscape mode, and the final for desktop and laptop
This allows you to cleanly separate your code for different screen resolutions into different files, if
that is what you want. Once again, note that the point of the example above is to demonstrate the use
of media queries in
<link> tags. The numbers are arbitrarily selected.
Instead of using specific resolutions to figure out if a device is in the portrait or landscape mode, you can also
use the condition "
orientation: portrait" and "
I tend not to use these since I find that knowing the number of pixels available more useful than just figuring out the orientation of the device. But the facility is available if you need to use it.
Apart from the above, you can also insert tests for
In addition, you can start the media query with "
only", as in the following snippet:
Very old browsers that don't understand the modern media queries syntax will think that "only" is a type of device (like "screen" or "print" or "speech"). And since they think that the rules in the block are meant for devices classified as "only", they will not follow them. Modern browsers, on the other hand, ignore the word "only" (by design), so this conditional test is useful if you need to guard against old browsers parsing your mobile-only rules and applying them even on a desktop computer.
If you want to use CSS for all situations except when certain conditions are met, you can use
not" before your condition, such as in the following example.
(Note that since I didn't specify "
screen" in the example above, it implies "
which means all devices.)
Be warned though, "not" is treated like "only" in very old browsers. That is, it will interpreted as a device type and therefore the style sheet in the block that follows will be not be applied.
Here is a list of the browser screen widths of some commonly-used mobile devices. The list is not exhaustive, since new brands and models are released all the time. However, the list is enough to give you an idea of the kinds of sizes that you need to accomodate.
On thesitewizard.com, I treat all screen widths above 629 pixels as being sufficient for my 2 column layout. Resolutions below that get a single column, with the navigation column (the left column) pushed to the bottom of the screen. You can see the effect of my mobile CSS rules even if you are on a desktop: simply resize your browser window on this very article you are reading. If you shrink the width smaller than 629 pixels, you will get the single column that the mobile users see.
Note that you do not have to follow my system of partitioning the CSS at 629 pixels. That's just the figure I used because it seems to work fine for thesitewizard.com's content. In fact, if I remember correctly, I saw a site that switched to a mobile layout only at 480 pixels, while another switched to different layouts depending on whether the screen had 940, 640 or 520 pixels on its horizontal axis. I recommend that you do not blindly follow other sites' size conditions: use a number that suits your content, testing and adjusting it accordingly.
The media queries facility that allows us to test for screen size is a latecomer to the web browser scene. That is to say, CSS had already existed for years before the standard included the means to conditionally apply certain rules to certain screen sizes. As such, very old browsers do not support these features.
Where smartphones are concerned, as far as I know, media queries are only supported on Android browsers beginning with version 2.1, on iPhones's Safari 3.2 and above, Blackberry 7 and later, Opera Mobile 12 and later, Opera Mini 8, and Internet Explorer ("IE") mobile 10 and above.
On the desktop/laptop browsers front, support appears to have started with IE 9, Firefox 3.5, Safari 4 and Chrome 4. Microsoft Edge, since it was originally based on IE 11's code, has always had media queries support.
In view of the above, how safe is it to use media queries on your site?
A lot depends on your site's demographics. For example, if your site has many people using phones with IE mobile 9 and earlier, you will probably want to support them.
This is not impossible, since early versions of IE allow the use of conditional comments, where you can include rules that will only be rendered by them and not other browsers. As such, it's possible to detect those browsers without resorting to media queries.
If your site has very few visitors using such old mobile browsers, then you have to decide whether or not you want to bother creating a solution specially for them. You may find that the time and effort you need to expend is disproportionate to the number of people that actually benefit from it. And that number will only decrease with time. As such, you may want to just let such users view your site using the default style sheet (which was what everyone would have seen anyway, before you suddenly decided to create a mobile-friendly one).
The next chapter deals with some ways of modifying a two column layout so that it is mobile-ready.
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.
It will appear on your page as: