CSS3 Media Queries

91
CSS3 MEDIA QUERIES

description

Media queries are one of the most exciting aspects about CSS today. They will allow us to change our layouts to suit the exact need of different devices - without changing the content. This presentation explains what Media queries are, how to use them, how to target the iPhone and how to create flexible layouts.

Transcript of CSS3 Media Queries

Page 1: CSS3 Media Queries

CSS3MEDIA QUERIES

Page 2: CSS3 Media Queries

Why shouldyou care aboutmedia queries?

Page 3: CSS3 Media Queries

Media queries are one of themost exciting aspects about

CSS today.

Page 4: CSS3 Media Queries

Media queries will allow us tochange our layouts to suit the

exact need of different devices- without changing the content.

Page 5: CSS3 Media Queries

For example, we will be able tomove away from “one-size-fits-

all” solutions such as liquid,elastic and fixed width layouts.

Page 6: CSS3 Media Queries

Let’s take a standard3 column 1000px wide layout…

Page 7: CSS3 Media Queries
Page 8: CSS3 Media Queries

Imagine if it could become a2 column 800px wide if the user

has a narrower browserwindow…

Page 9: CSS3 Media Queries
Page 10: CSS3 Media Queries

…or a single column 400pxwide layout if the user has a

mobile device or a very narrowbrowser window…

Page 11: CSS3 Media Queries
Page 12: CSS3 Media Queries

And all done with CSS alone - noJavaScript…

Page 13: CSS3 Media Queries

This is just one quick exampleof how media queries can

help us deliver CSS innew and exciting ways

Page 14: CSS3 Media Queries

But… before we talk aboutmedia queries, we need to do aquick overview of media types.

Page 15: CSS3 Media Queries

So, what aremedia types?

Page 16: CSS3 Media Queries

CSS can be used to specifyhow a document is presented

in different media.

Page 17: CSS3 Media Queries

There are ten media typesdefined in CSS 2.1

Page 18: CSS3 Media Queries

suitable for all devicesfor speech synthesizersfor Braille tactile feedback devicesfor paged Braille printersfor handheld devicesfor print materialfor projected presentationsfor color computer screensfor teletypes and terminalsfor television type devices

allaural

brailleembossedhandheld

printprojection

screenttytv

Page 19: CSS3 Media Queries

There are five methods that canbe used to specify media

for style sheets.

Page 20: CSS3 Media Queries

Method 1:<link> within HTML

Page 21: CSS3 Media Queries

You can use a <link> element inthe head of your HTML documentto specify the target media of an

external style sheet.

<link rel="stylesheet" href="a.css" type="text/css" media=”screen" />

Page 22: CSS3 Media Queries

Method 2:<?xml stylesheet>

within XML

Page 23: CSS3 Media Queries

You can use <?xml-stylesheet ?>in the head of your XML documentto specify the target media of an

external style sheet.

<?xml-stylesheet media="screen" rel="stylesheet" href="example.css" ?>

Page 24: CSS3 Media Queries

Method 3:@import within

HTML

Page 25: CSS3 Media Queries

You can use @import in the headif your HTML document to specify

the target media of an externalstyle sheet.

<style type="text/css" media="screen">@import "a.css";</style>

Page 26: CSS3 Media Queries

Warning:@import should be avoided as it

can cause issues in someversions of Internet Explorer.

http://www.stevesouders.com/blog/2009/04/09/dont-use-import/

Page 27: CSS3 Media Queries

Method 4:@import within CSS

Page 28: CSS3 Media Queries

You can specify the target mediumwithin a CSS file using @import

@import url("a.css") screen;

Page 29: CSS3 Media Queries

Media-types within @importrules are not supported by IE5,IE6 or IE7. The rule is ignored.

Page 30: CSS3 Media Queries

Method 5:@media within CSS

Page 31: CSS3 Media Queries

You can specify the target mediumwithin a CSS file using @media

@media screen {

body { color: blue; }}

Page 32: CSS3 Media Queries

Why should we careabout these five

methods?

Page 33: CSS3 Media Queries

Because you can use these fivemethods to define not only media

types, but media queries

Page 34: CSS3 Media Queries

Let’s talkmedia queries

Page 35: CSS3 Media Queries

Media queries are a CSS3extension to media types that gives

us more control over renderingacross different devices.

<link rel="stylesheet" type="text/css" href="a.css" media="screen and (color)">

Page 36: CSS3 Media Queries

A media query is a logicalexpression that is either

true or false.

Page 37: CSS3 Media Queries

The CSS associated with themedia query expression is only

applied to the device if theexpression is true.

Page 38: CSS3 Media Queries

Media querysyntax

Page 39: CSS3 Media Queries

A media query generally consists ofa media type and zero or more

expressions.

<link rel="stylesheet" type="text/css" href="a.css" media=”screen and (color)">

Media type Expression

Page 40: CSS3 Media Queries

<link rel="stylesheet" type="text/css" href="a.css" media=”screen and (color)">

An expression consists of zero ormore keywords and a media

feature.

Keyword Media feature

Page 41: CSS3 Media Queries

Media features are placed withinbrackets.

<link rel="stylesheet" type="text/css" href="a.css" media=”screen and (color)">

Media feature

Page 42: CSS3 Media Queries

A media feature can be usedwithout a media type or keyword.

The media type is assumed to be “all”.

<link rel="stylesheet" type="text/css" href="a.css" media=”(color)">

Media feature

Page 43: CSS3 Media Queries

Most media features accept“min-” or “max-” prefixes.

<link rel="stylesheet" type="text/css" href="a.css" media="screen and (min-height: 20em)">

Page 44: CSS3 Media Queries

Media features can often be usedwithout a value.

<link rel="stylesheet" type="text/css" href="a.css" media="screen and (color)">

Page 45: CSS3 Media Queries

Media features only accept singlevalues: one keyword, one number,or a number with a unit identifier.

(orientation: portrait)(min-width: 20em)(min-color: 2)(device-aspect-ratio: 16/9)

Except aspect-ratio and device-aspect-ration which require two numbers

Page 46: CSS3 Media Queries

The full mediafeature list

Page 47: CSS3 Media Queries

Featureaspect-ratiocolorcolor-indexdevice-aspect-ratiodevice-heightdevice-widthgridheightmonochromeorientationresolutionscanwidth

Valueratio (integer/integer) integerintegerratio (integer/integer)lengthlengthintegerlengthintegerkeyword (portrait/landscape)resolution (dpi)keyword (progressive/interlace)length

min/maxyesyesyesyesyesyesnoyesyesnoyesnoyes

Page 48: CSS3 Media Queries

A simple example

Page 49: CSS3 Media Queries

The CSS file in this exampleshould be applied to screendevices that are capable of

representing color.

<link rel="stylesheet" type="text/css" href="a.css" media="screen and (color)">

Page 50: CSS3 Media Queries

This same media enquiry could beused with @import via HTML.

<style type="text/css" media="screen and (color) ">@import "a.css";</style>

Page 51: CSS3 Media Queries

It could be used with@import via CSS.

@import url("a.css") screen and (color);

Page 52: CSS3 Media Queries

Or using @media via CSS.

@media screen and (color){

body { color: blue; }}

Page 53: CSS3 Media Queries

Multiple expressions

Page 54: CSS3 Media Queries

You can use multipleexpressions in a media query if

you join them with the “and”keyword.

Page 55: CSS3 Media Queries

The CSS file in this example will beapplied by hand-held devices, but

only if the viewport width is at> 20em and < 40em.

<link rel="stylesheet" type="text/css" href="a.css" media="handheld and (min-width:20em) and (max-width:40em)">

Page 56: CSS3 Media Queries

Comma separated

Page 57: CSS3 Media Queries

You can also use multiple,comma-separated media queries.

The comma acts like an “or”keyword.

Page 58: CSS3 Media Queries

The CSS file in this example will beapplied to screen with color orhandheld devices with color.

<link rel="stylesheet" type="text/css" href="a.css" media="screen and (color), handheld and (color)">

Page 59: CSS3 Media Queries

Using the “not”keyword

Page 60: CSS3 Media Queries

You can use the not keyword in amedia query if you want your CSSto be ignored by a specific device.

Page 61: CSS3 Media Queries

The CSS file in this example will beapplied to all devices except those

with color screens.

<link rel="stylesheet" type="text/css" href="a.css" media="not screen and (color)">

Page 62: CSS3 Media Queries

Using the “only”expression

Page 63: CSS3 Media Queries

The CSS file in this example will beapplied only to all devices with

color screens.

<link rel="stylesheet" type="text/css" href="a.css" media="only screen and (color)">

Page 64: CSS3 Media Queries

Support formedia queries

Page 65: CSS3 Media Queries

Browser support for media queries:

Safari 4 yes

Firefox 3.6 yes

IE8 no

Opera 10 yes

Chrome 5 yes

* Based on basic testing only

Page 66: CSS3 Media Queries

What do otherbrowsers see?

Page 67: CSS3 Media Queries

Browsers that do not supportmedia queries should stillsupport the media type.

<link rel="stylesheet" type="text/css" href="a.css" media="screen and (color)">

Page 68: CSS3 Media Queries

The “only” keyword is sometimesused to hide CSS from devices

that do not support media queries,but may read the media type.

<link rel="stylesheet" type="text/css" href="a.css" media="only screen and (color)">

Page 69: CSS3 Media Queries

Targeting theiPhone

Page 70: CSS3 Media Queries

The iPhone does not supporthandheld media type. Apple

recommends targeting the iPhoneusing media queries.

Page 71: CSS3 Media Queries

This rule will be applied by theiPhone which has a maximumdevice width (screen width) of

480px.

<link rel="stylesheet" type="text/css" href="a.css"media="only screen and (max-device-width: 480px)" >

Page 72: CSS3 Media Queries

Using mediaqueries to

control layouts

Page 73: CSS3 Media Queries

So, how could we use mediaqueries to change a page layout

so that it can appear wide, mediumor narrow depending on the width

of the screen?

Page 74: CSS3 Media Queries

Here is a quick stepby step example

Page 75: CSS3 Media Queries

Step 1:Add a link to your style sheet

<link rel="stylesheet" type="text/css" href=”master.css"media="screen" >

Page 76: CSS3 Media Queries

Step 2:Add your “wide page layout” CSS

rules into your CSS file

Page 77: CSS3 Media Queries

Step 3:Add a @media rule with a media

query

@media screen and (max-width:999px){

/* add your rules here */}

Page 78: CSS3 Media Queries

Step 4:Add your “medium page layout”

CSS rules inside this @media rule.

Page 79: CSS3 Media Queries

Step 5:Add a second @media rule with a

media query

@media screen and (max-width:480px){

/* add your rules here */}

Page 80: CSS3 Media Queries

Step 6:Add your “narrow page layout”

CSS rules inside this new @mediarule.

Page 81: CSS3 Media Queries

@media screen and (max-width:999px) {

}

@media screen and (max-width:480px){

}

Wide page layout CSS rules

Medium page layout CSS rules

Narrow page layout CSS rules

Your CSS file should be structuredsomething like this:

Page 82: CSS3 Media Queries

A note on the CSS

Page 83: CSS3 Media Queries

Devices wider than 1000px will seethe “wide page layout” CSS only.

Page 84: CSS3 Media Queries

Devices narrower than 1000px willsee the “wide page layout” CSSAND the “medium page layout”

CSS.

Page 85: CSS3 Media Queries

Devices narrower than 480px willsee the “wide page layout”,“medium page layout” and“narrow page layout” CSS.

Page 86: CSS3 Media Queries

What does thismean?

Page 87: CSS3 Media Queries

This means that rules written insideeach @media statements mustoverride the previous rules.

Page 88: CSS3 Media Queries

A quickrecap

Page 89: CSS3 Media Queries

I believe that as media queriesbecome supported, we will see aradical change in the way wedevelop websites in the future.

Page 90: CSS3 Media Queries

Now is a good time to getyour head around these

powerful CSS3 expressionsso that you are ready when

the time comes!

Page 91: CSS3 Media Queries

We’re done