source:w3 schools - http://www.w3schools.com/tags/att_link_media.asp
HTML media Attribute
Example
<head>
<link href="theme.css" rel="stylesheet" type="text/css"></link>
<link href="print.css" media="print" rel="stylesheet" type="text/css"></link>
</head>
Browser Support
![]() ![]() ![]() ![]() ![]() |
Definition and Usage
The media attribute specifies what media/device the target resource is optimized
for.
This attribute is mostly used with CSS stylesheets to specify different styles
for different media types.
The media attribute can accept several values.
Possible Operators
Value | Description |
---|---|
and | Specifies an AND operator |
not | Specifies a NOT operator |
, | Specifies an OR operator |
Devices
Value | Description |
---|---|
all | Default. Suitable for all devices |
aural | Speech synthesizers |
braille | Braille feedback devices |
handheld | Handheld devices (small screen, limited bandwidth) |
projection | Projectors |
Print preview mode/printed pages | |
screen | Computer screens |
tty | Teletypes and similar media using a fixed-pitch character grid |
tv | Television type devices (low resolution, limited scroll ability) |
Values
Value | Description |
---|---|
width | Specifies the width of the targeted display area. "min-" and "max-" prefixes can be used. Example: media="screen and (min-width:500px)" |
height | Specifies the height of the targeted display area. "min-" and "max-" prefixes can be used. Example: media="screen and (max-height:700px)" |
device-width | Specifies the width of the target display/paper. "min-" and "max-" prefixes can be used. Example: media="screen and (device-width:500px)" |
device-height | Specifies the height of the target display/paper. "min-" and "max-" prefixes can be used. Example: media="screen and (device-height:500px)" |
orientation | Specifies the orientation of the target display/paper. Possible values: "portrait" or "landscape" Example: media="all and (orientation: landscape)" |
aspect-ratio | Specifies the width/height ratio of the targeted display area. "min-" and "max-" prefixes can be used. Example: media="screen and (aspect-ratio:16/9)" |
device-aspect-ratio | Specifies the device-width/device-height ratio of the target
display/paper. "min-" and "max-" prefixes can be used. Example: media="screen and (aspect-ratio:16/9)" |
color | Specifies the bits per color of target display. "min-" and "max-" prefixes can be used. Example: media="screen and (color:3)" |
color-index | Specifies the number of colors the target display can handle. "min-" and "max-" prefixes can be used. Example: media="screen and (min-color-index:256)" |
monochrome | Specifies the bits per pixel in a monochrome frame buffer. "min-" and "max-" prefixes can be used. Example: media="screen and (monochrome:2)" |
resolution | Specifies the pixel density (dpi or dpcm) of the target display/paper. "min-" and "max-" prefixes can be used. Example: media="print and (resolution:300dpi)" |
scan | Specifies scanning method of a tv display. Possible values are "progressive" and "interlace". Example: media="tv and (scan:interlace)" |
grid | Specifies if the output device is grid or bitmap. Possible values are "1" for grid, and "0" otherwise. Example: media="handheld and (grid:1)" |
source: w3.org
Media types
Introduction to media types
One of the most important features of style sheets is that they specify how a document is to be presented on different media: on the screen, on paper, with a speech synthesizer, with a braille device, etc.Certain CSS properties are only designed for certain media (e.g., the 'page-break-before' property only applies to paged media). On occasion, however, style sheets for different media types may share a property, but require different values for that property. For example, the 'font-size' property is useful both for screen and print media. The two media types are different enough to require different values for the common property; a document will typically need a larger font on a computer screen than on paper. Therefore, it is necessary to express that a style sheet, or a section of a style sheet, applies to certain media types.
Specifying media-dependent style sheets
There are currently two ways to specify media dependencies for style sheets:- Specify the target medium from a style sheet with the @media or @import at-rules.
@import url("fancyfonts.css") screen; @media print { /* style sheet for print goes here */ }
- Specify the target medium within the document language. For
example, in HTML 4 ([HTML4]), the "media" attribute on the LINK
element specifies the target media of an external style sheet:
<head> <title>Link to a target medium</title> <link href="foo.css" media="print, handheld" rel="stylesheet" type="text/css"></link> </head> <body> The body... </body> </html>
The @media rule
An @media rule specifies the target media types (separated by commas) of a set of statements (delimited by curly braces). Invalid statements must be ignored per 4.1.7 "Rule sets, declaration blocks, and selectors" and 4.2 "Rules for handling parsing errors." The @media construct allows style sheet rules for various media in the same style sheet:@media print { body { font-size: 10pt } } @media screen { body { font-size: 13px } } @media screen, print { body { line-height: 1.2 } }Style rules outside of @media rules apply to all media types that the style sheet applies to. At-rules inside @media are invalid in CSS2.1.
Recognized media types
The names chosen for CSS media types reflect target devices for which the relevant properties make sense. In the following list of CSS media types the names of media types are normative, but the descriptions are informative. Likewise, the "Media" field in the description of each property is informative.- all
- Suitable for all devices.
- braille
- Intended for braille tactile feedback devices.
- embossed
- Intended for paged braille printers.
- handheld
- Intended for handheld devices (typically small screen, limited bandwidth).
- Intended for paged material and for documents viewed on screen in print preview mode. Please consult the section on paged media for information about formatting issues that are specific to paged media.
- projection
- Intended for projected presentations, for example projectors. Please consult the section on paged media for information about formatting issues that are specific to paged media.
- screen
- Intended primarily for color computer screens.
- speech
- Intended for speech synthesizers. Note: CSS2 had a similar media type called 'aural' for this purpose. See the appendix on aural style sheets for details.
- tty
- Intended for media using a fixed-pitch character grid (such as teletypes, terminals, or portable devices with limited display capabilities). Authors should not use pixel units with the "tty" media type.
- tv
- Intended for television-type devices (low resolution, color, limited-scrollability screens, sound available).
Media types are mutually exclusive in the sense that a user agent can only support one media type when rendering a document. However, user agents may use different media types on different canvases. For example, a document may (simultaneously) be shown in 'screen' mode on one canvas and 'print' mode on another canvas.
Note that a multimodal media type is still only one media type. The 'tv' media type, for example, is a multimodal media type that renders both visually and aurally to a single canvas.
@media and @import rules with unknown media types (that are nonetheless valid identifiers) are treated as if the unknown media types are not present. If an @media/@import rule contains a malformed media type (not an identifier) then the statement is invalid.
Note: Media Queries supercedes this
error handling.
For example, in the following snippet, the rule on the P element applies
in 'screen' mode (even though the '3D' media type is not known).
@media screen, 3D { P { color: green; } }
Note.
Future updates of CSS may extend the list of media types. Authors
should not rely on media type names that are not yet defined
by a CSS specification.
Media groups
This section is informative, not normative.Each CSS property definition specifies which media types the property applies to. Since properties generally apply to several media types, the "Applies to media" section of each property definition lists media groups rather than individual media types. Each property applies to all media types in the media groups listed in its definition.
CSS 2.1 defines the following media groups:
- continuous or paged.
- visual, audio, speech, or tactile.
- grid (for character grid devices), or bitmap.
- interactive (for devices that allow user interaction), or static (for those that do not).
- all (includes all media types)
Media Types | Media Groups | |||
---|---|---|---|---|
continuous/paged | visual/audio/speech/tactile | grid/bitmap | interactive/static | |
braille | continuous | tactile | grid | both |
embossed | paged | tactile | grid | static |
handheld | both | visual, audio, speech | both | both |
paged | visual | bitmap | static | |
projection | paged | visual | bitmap | interactive |
screen | continuous | visual, audio | bitmap | both |
speech | continuous | speech | N/A | both |
tty | continuous | visual | grid | both |
tv | both | visual, audio | bitmap | both |
Some scenarios of usage of the media attribute
- A solution to print Webpages (http://ozinisle.blogspot.sg/2009/12/media-attribute-of-link-tag-solution-to.html)
Problems associated with using media attribute
source : http://friendlybit.com/css/media-attribute/
Problem number 1: Presentation and content are coupled
One of the big selling points of CSS is the separation of presentation from content. And while I believe that’s still a good thing to have, not everyone agrees about it’s usefulness. Some time ago Jeff Croft wrote an article about how unusual it is to only change the CSS and not the HTML of a site. While he’s mostly making a point of why we should use a certain CSS framework, there’s a good point hidden there: Right now it isn’t possible to separate things completely.Designers have of course known this for centuries. They will tell you: you need to adapt the design to the content you’re designing. If you’re building a site for a shampoo, you might use water and bubbles in your design. If you build a web development blog, you use an image of blueish sky… ehm… Well, you get the point. Good design adapts to the content. They are coupled.
You can’t just switch out the content and expect the design to still work. Sure, you can make small adjustments, and make these available as alternate stylesheets, but larger changes just doesn’t work. The problem with the media attribute is that it’s made for big design changes (switching media), but with no changes in content. How often can you just restyle a word document to get a powerpoint slide? What about converting that slide to something nicely viewable on a mobile phone? That’s what the media attribute is there for.
My point is: switching to another media needs much more than just a change in design. You need to change the content to fit that media too. And if you change the content, why not change what stylesheet you link to? This is why I rarely use the “handheld” or “projection” values.
Problem number 2: Load time
You could think that the browser only loads the one stylesheet that matches the media it’s currently showing. Not true. All stylesheets, no matter what media they are tied to, are loaded at startup.This means that the more media types you account for, the longer the load time for any of them will be. Very annoying.
Problem number 3: User agent support
While being able to design for specific user agents might sound like a good idea, the media attribute still requires user agents to support it. If a large part of user agents refuse to apply your style using the media attribute, why not use another method directly? Just to get an idea of how messy support currently is, you can read the css-discuss summary of the handheld problems:“Some current phones apply “screen” styles as well as “handheld” styles, others ignore both, and in some cases the phone carrier runs pages through a proxy that strips styles out even if the phone could recognize them, so it’s a crapshoot figuring out what will get applied”As you see, the value of the media attribute isn’t entirely obvious. Sure, you might be able to use it for print with good results, but that’s not all it’s there for. Right?
Alternative to using the media attribute
- CSS3 Media queries instead of the media attribute
- @media Ajax London, here I come
- Inline CSS should not be allowed in strict doctypes
CSS Media queries
source: w3.org
Example
<link rel="stylesheet" href="/stylesheets/homedark.css" type="text/css"
media="screen and (max-device-width: 480px)">
Inside your stylesheet, it would be something like this
@media only screen and (max-device-width: 480px)
{
//Your styles here
}
Description and documentation-
- http://www.w3.org/TR/css3-mediaqueries/
- http://www.htmlgoodies.com/beyond/css/introduction-to-css-media-queries.html
- http://css-tricks.com/css-media-queries/
- How To Use CSS3 Media Queries To Create a Mobile Version of Your Website
- http://cssmediaqueries.com/
- A super easy video tutorial : www.youtube.com/watch?v=FNIe-Y2V0hg