Navigation

How to build standards-compliant responsive design using @viewport

By Sara Vieira | CSS | Aug 13, 2013

One of the key concepts in any responsive design is the change of viewport size. That’s because mobile viewports vary greatly from desktop viewports. To control the viewport size we traditionally use the viewport meta tag.

However, the viewport meta tag, like all the worst browser developments of the last forever, isn’t W3C valid. It was originally introduced by Apple in Safari and has since been adopted by other browsers. This results in an inconsistent implementation.<!–more>

Happily, the W3C have ridden to our rescue once again by introducing the @viewport CSS rule.

 

Old school

Using the old meta tag approach, this is how we’d tell the browser what size the viewport should seen as:

<meta name='viewport' content='width=device-width'>

 

The CSS rule

Quite apart from being invalid, the viewport instruction isn’t data, it’s presentation. So, sticking to our data and presentation separation principles, the viewport instruction needs to be in CSS, not HTML.

The W3C solution in CSS looks like this:

@viewport {
    width: device-width;
}

Or alternately, you can set the viewport with a number, like so:

@viewport {
    width: 640px;
}

You can use the @viewport rule in conjunction with @media queries to force any viewport larger than 960 to shrink to 960px, like so:

@media screen and (min-width: 960px){
    @viewport {
        width: 960px;
    }
}

 

Additional properties

The @viewport rule also allows us to zoom into a page by default, and even set the maximum zoom:

@viewport {
    width: 960px;
    zoom: 1;
    max-zoom: 3;
}

It is possible to block zooming altogether by setting the user-zoom property to fixed. However zooming, especially on smartphones, is necessary for accessibility and I would recommend using this property.

One more very useful property allows us to lock our web page into landscape or portrait mode:

@viewport {
    orientation: landscape;
}

 

Browser support

Here’s the bad news: currently this rule is only supported by Internet Explorer 10 and Opera, and requires the -ms- and -o- browser prefixes respectively.

Whilst that is disappointing to say the least, the fact that viewport functionality is already available in most browsers should mean this is a simple rule to pick up. Hopefully we’ll start seeing it introduced in nightly builds very shortly.

 

Do you care about standards-compliance? Do the W3C help or hinder progress on the web? Let us know in the comments.

Featured image/thumbnail, window image via LostBob Photos

Share this post
Comments (no login required)
  • Er.Shivam Kumar

    Does w3c validation really matters that much i dont think a site which is dynamic and using plugins remains w3c valid for too long after it is been updated daily though it would be valid with no errors at the time of its creation even my site uses viewport,i cant say weather it hinders or not but setting up a standard is a must for making something globally portable.

    Brainlubricator

  • http://www.mathewporter.co.uk/ Mathew Porter

    Nice to see a post of using viewport instead of just screen size. On validation it is hard to validate the emerging tech that has reached final stages iin W3C’s spec, but aslong as you use elements that wont be droppe (hard to do at the moment, lol) you should be ok’ ish in the future.

  • digimadmedia

    Haven’t read much about using viewport and it seems like a nice integration into the responsive design. Shame about compatibility but still useful.

    Digimad Media