Thursday 9 January 2014

Media Queries Contd..

Media Queries Demo

Using media queries we will now rewrite the flexible layout we built previously. One of the current problems within the demo appears when the aside width becomes uselessly small within smaller viewports. Adding a media query for viewports under 420 pixels wide we can change the layout by turning off the floats and changing the widths of thesection and aside.
  1. @media all and (max-width: 420px) {
  2. section, aside {
  3. float: none;
  4. width: auto;
  5. }
  6. }
Demo without Media Queries
Fig. 4.02Without any media queries the section and asidebecome quite small. Perhaps too small to even contain any real content.
Demo with Media Queries
Fig. 4.03Using media queries to remove the floats and change their widths, the section and aside are now able to span the full width of the viewport, allowing breathing room for any existing content.

Identifying Breakpoints

Your instinct might be to write media query breakpoints around common viewport sizes as determined by different device resolutions, such as 320px480px768px1024px1224px, and so forth. This is a bad idea.
When building a responsive website it should adjust to an array of different viewport sizes, regardless of the device. Breakpoints should only be introduced when a website starts to break, look weird, or the experience is being hampered.
Additionally, new devices and resolutions are being released all of the time. Trying to keep up with these changes could be an endless process.

Mobile First

One popular technique with using media queries is called mobile first. The mobile first approach includes using styles targeted at smaller viewports as the default styles for a website, then use media queries to add styles as the viewport grows.
The operating belief behind mobile first design is that a user on a mobile device, commonly using a smaller viewport, should’t have to load the styles for a desktop computer only to have them over written with mobile styles later. Doing so is a waste of bandwidth. Bandwidth that is precious to any users looking for a snappy website.
The mobile first approach also advocates designing with the constraints of a mobile user in mind. Before too long, the majority of Internet consumption will be done on a mobile device. Plan for them accordingly and develop intrinsic mobile experiences.
A breakout of mobile first media queries might look at bit like the following.
  1. /* Default styles first then media queries */
  2. @media screen and (min-width: 400px) {...}
  3. @media screen and (min-width: 600px) {...}
  4. @media screen and (min-width: 1000px) {...}
  5. @media screen and (min-width: 1400px) {...}
Additionally, downloading unnecessary media assets can be stopped by using media queries. Generally speaking, avoiding CSS3 shadows, gradients, transforms, and animations within mobile styles isn’t a bad idea either. When used excessively, they cause heavy loading and can even reduce a device’s battery life.
  1. /* Default media */
  2. body {
  3. background: #ddd;
  4. }
  5. /* Media for larger devices */
  6. @media screen and (min-width: 800px) {
  7. body {
  8. background-image: url("bg.png") 50% 50% no-repeat;
  9. }
  10. }

Mobile First Demo

Adding media queries to our previous example, we overwrote a handful of styles in order to have a better layout on viewports under 420 pixels wide. Rewriting this code to use the mobile styles first by default then adding media queries to adjust for viewports over 420 pixels wide we build the following:
  1. section, aside {
  2. margin: 1.51515151%;
  3. }
  4. @media all and (min-width: 420px) {
  5. .container {
  6. max-width: 660px;
  7. }
  8. section {
  9. float: left;
  10. width: 63.63636363%;
  11. }
  12. aside {
  13. float: right;
  14. width: 30.30303030%;
  15. }
  16. }
Notice, this is the same amount of code as before. The only exception here is that mobile devices only have to render only one CSS declaration. All of the other styles are deferred, only loading on larger viewports and done so without overwriting any initial styles.

Viewport

Mobile devices generally do a pretty decent job of displaying websites these days. Sometimes they could use a little assistance though, particularly around identifying the viewport size, scale, and resolution of a website. To remedy this, Apple invented the viewport meta tag.
Website without Viewport Meta Tag
Fig. 4.04Although this demo has media queries, many mobile devices still do not know the initial width or scale of the website. Therefore, they may not interrupt media queries.

Viewport Height & Width

Using the viewport meta tag with either the height or width values will define the height or width of the viewport respectively. Each value accepts either a positive integer or keyword. For the height property the keyword device-height value is accepted, and for the width property the keyword device-width is accepted. Using these keywords will inherit the device’s default height and width value.
For the best results, and the best looking website, it is recommend that you use the device defaults by applying thedevice-height and device-width values.
  1. HEIGHT & WIDTH<meta name="viewport" content="width=device-width">
Website with Viewport Meta Tag
Fig. 4.05Letting devices know the intended width of the website,device-width in this case, allows the website to be sized properly and to pick up any qualifying media queries.

Viewport Scale

To control how a website is scaled on a mobile device, and how users can continue to scale a website, use theminimum-scalemaximum-scaleinitial-scale, and user-scalable properties.
The initial-scale of a website should be set to 1 as this defines the ratio between the device height, while in a portrait orientation, and the viewport size. Should a device be in landscape mode this would be the ratio between the device width and the viewport size. Values for initial-scale should always be a positive integer between 0 and 10.
  1. <meta name="viewport" content="initial-scale=2">
Viewport Scale Meta Tag
Fig. 4.06Using an integer above 1 will zoom the website to be larger than the default scale. Generally speaking, this value will most commonly be set to 1.
The minimum-scale and maximum-scale values determine how small and how large a viewport may be scaled. When using minimum-scale the value should be a positive integer lower than or equal to the initial-scale. Using the same reasoning, the maximum-scale value should be a positive integer greater than or equal to the initial-scale. Values for both of these must also be between 0 and 10.
  1. MINIMUM & MAXIMUM SCALE<meta name="viewport" content="minimum-scale=0">
Generally speaking, these values should not be set to the same value as the initial-scale. This would disable any zooming, which can be accomplished instead by using the user-scalable value. Setting the user-scalable value to no will disable any zooming. Alternatively, setting the user-scalable value to yes will turn on zooming.
Turning off the ability to scale a website is a bad idea. It harms accessibility and usability, preventing those with disabilities from viewing a website as desired.
  1. <meta name="viewport" content="user-scalable=yes">

Viewport Resolution

Letting the browser decide how to scale a website based off any viewport scale values usually does the trick. When more control is needed, specifically over the resolution of a device, the target-densitydpi value may be used. Thetarget-densitydpi viewport accepts a handful of values including device-dpihigh-dpimedium-dpilow-dpi, or an actual DPI number.
Using the target-densitydpi viewport value is rare, but extremely helpful when pixel by pixel control is needed.
  1. <meta name="viewport" content="target-densitydpi=device-dpi">

Combining Viewport Values

The viewport meta tag will accept individual values as well as multiple values, allowing multiple viewport properties to be set at once. Setting multiple values requires comma separating them within the content attribute value. One of the recommended viewport values is outlined below, using both the width and initial-scale properties.
  1. <meta name="viewport" content="width=device-width, initial-scale=1">
Website with Viewport Meta Tag
Fig. 4.07A combination of width=device-width and initial-scale=1 provide the initial size and zoom commonly required.

CSS Viewport Rule

Since the viewport meta tag revolves so heavily around setting the styles of how a website should be rendered it has been recommend to move the viewport from a meta tag with HTML to an @ rule within CSS. This helps keep the style separated from content, providing a more semantic approach.
Currently some browsers have already implemented the @viewport rule, however support isn’t great across the board. The previously recommended viewport meta tag would look like the following @viewport rule in CSS.
  1. @viewport {
  2. width: device-width;
  3. zoom: 1;
  4. }

Flexible Media

The final, equally important aspect to responsive web design involves flexible media. As viewports begin to change size media doesn’t always follow suit. Images, videos, and other media types need to be scalable, changing their size as the size of the viewport changes.
One quick way to make media scalable is by using the max-width property with a value of 100%. Doing so ensures that as the viewport gets smaller any media will scale down according to its containers width.
  1. img, video, canvas {
  2. max-width: 100%;
  3. }

Flexible Media Demo

100% WIDE CONTAINER
Chicago
75% WIDE CONTAINER
Chicago
50% WIDE CONTAINER
Chicago