Optional HTML content based on screen resolution - mobiles and high resolution desktops

Displaying optional content for high resolution or mobile users on your web page

The W3C Media Queries provide a great opportunity to display a webpage content differently based on the capabilities of the user’s browser or machine.
This can be especially useful for mobile browsers or variable screen width/resolution.

Many webpages do not care at all if you have a high resolution screen, the content will stay static and will just waste the extra space on your monitor –as two wide white columns on the left and right.

However, without any special programming we can provide extra, non essential but useful content to the users with a decent resolution (or remove these for mobile devices).

If we want to target a specific resolution, we can address them using the following CSS media query:

@media only screen and ( max-width : 600px) {
/* CSS styles here*/
}

These styles will only apply on the users screen if the browser window size is maximum 600 pixels – meaning probably on small handheld devices or on any desktop where the browser window is sized quite small.

If we want to query the physical resolution and ignore the actual screen size, we can simply use the following:

@media only screen and ( max-device-width : 600px) {
/* CSS styles here*/
}

In this case, regardless how big the browser window is, these style will only target the lower resolution screens.

Using this simple technique, we can easily create a mobile/simple desktop version of our site just by removing extra elements from the screen, giving it a different menu and increasing the font size a bit:

@media only screen and ( max-width : 600px) {
 body {
  font-size: 20px;
 }
 .header {
  display: none;
 }
 .middle,.right {
  display: none;
 }
}

@media only screen and ( min-width : 601px) {
 body {
  font-size: 15px;
 }
 .header-mobile {
  display: none;
 }
}

And the HTML page itself:

<body>
 <div class='header'>Header content, yay</div>
 <div class='header-mobile'>Mobile header, yay</div>
 <div class='left content'>This is the static left content</div>
 <div class='middle content'>This is the optional middle content</div>
 <div class='right content'>This is some other extra</div>
 <div class='clear content'></div>
</body>
Mobile CSS applied - no extra content

Desktop CSS applied - two extra columns


In the example above, we simply hide the middle and right extra content while replacing the standard menu with a mobile menu.

This approach is extremely easy to follow, there is no need to develop a different mobile site we can simply fine-tune the content as we go.

It is important to note, that while there is a media query for handheld device property, many of the smartphones (including iOS/iPhone) will ignore the following:

@media handheld {
...
}

The reasoning is that the Apple iPhone is an advanced device so it does not need a special mobile version of the site, it will display the original content perfectly. Still, we can use the physical resolution of the screen. However, we still can use the screen density (DPI) to show different content:

@media (min-resolution: 300dpi)
{
…
}

A desktop monitor has got a resolution of about 90DPI while the new mobile devices (like iPhone 4S, the Samsung Galaxy S III, or the ‘new iPad’) are around 300DPI.

For a list of resolution of different handheld and desktop devices, head to the Wikipedia (http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density).


For all the W3C Media Queries, go to the W3C site (http://www.w3.org/TR/css3-mediaqueries)

Comments

Popular posts from this blog

MurMurHash3, an ultra fast hash algorithm for C# / .NET

Quick select algorithm - find the Kth element in a list in linear time

Octoprint as a systemd service - running it with high priority