How to build standards-compliant responsive design using @viewport

Wdd Logo.
August 13, 2013
How to build standards-compliant responsive design using @viewport.

thumbnailOne 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

WDD Staff

WDD staff are proud to be able to bring you this daily blog about web design and development. If there's something you think we should be talking about let us know @DesignerDepot.

Read Next

20 Best New Websites, April 2024

Welcome to our sites of the month for April. With some websites, the details make all the difference, while in others,…

Exciting New Tools for Designers, April 2024

Welcome to our April tools collection. There are no practical jokes here, just practical gadgets, services, and apps to…

14 Top UX Tools for Designers in 2024

User Experience (UX) is one of the most important fields of design, so it should come as no surprise that there are a…

What Negative Effects Does a Bad Website Design Have On My Business?

Consumer expectations for a responsive, immersive, and visually appealing website experience have never been higher. In…

10+ Best Resources & Tools for Web Designers (2024 update)

Is searching for the best web design tools to suit your needs akin to having a recurring bad dream? Does each…

3 Essential Design Trends, April 2024

Ready to jump into some amazing new design ideas for Spring? Our roundup has everything from UX to color trends…

How to Plan Your First Successful Website

Planning a new website can be exciting and — if you’re anything like me — a little daunting. Whether you’re an…

15 Best New Fonts, March 2024

Welcome to March’s edition of our roundup of the best new fonts for designers. This month’s compilation includes…

LimeWire Developer APIs Herald a New Era of AI Integration

Generative AI is a fascinating technology. Far from the design killer some people feared, it is an empowering and…

20 Best New Websites, March 2024

Welcome to our pick of sites for March. This month’s collection tends towards the simple and clean, which goes to show…

Exciting New Tools for Designers, March 2024

The fast-paced world of design never stops turning, and staying ahead of the curve is essential for creatives. As…

Web Tech Trends to Watch in 2024 and Beyond

It hardly seems possible given the radical transformations we’ve seen over the last few decades, but the web design…