Specifying the Media for a Style

The media attributes specifies when a style should be applied.

 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style media="screen" type="text/css">
a {
      background-color: grey;
      color: white;
      padding: 0.5em;
}
</style>
<style media="print">
a {
      color: Red;
      font-weight: bold;
      font-style: italic
}
</style>
</head>
<body>
      <a href="http://java2s.com">Visit java2s.com</a>
</body>
</html>
  

The allowed values for the media attribute:

DeviceDescription
allApply this style to any device (default).
auralApply this style to speech synthesizers.
brailleApply this style to Braille devices.
handheldApply this style to handheld devices.
projectionApply this style to projectors.
printApply this style in print preview and when the page is printed.
screenApply this style when showing on a computer screen.
ttyApply this style to fixed-width devices, such as teletypes.
tvApply this style to televisions.
 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style media="screen AND (max-width:500px)" type="text/css">
a {
      background-color: grey;
      color: white;
      padding: 0.5em;
}
</style>
<style media="screen AND (min-width:500px)" type="text/css">
a {
      color: Red;
      font-style: italic
}
</style>
</head>
<body>
      <a href="http://java2s.com">Visit java2s.com</a>
</body>
</html>
  

You can use AND to combine a device with a feature. You can also use NOT and a comma (,) which represents OR.

The following table lists features for the media Attribute of the style Element

FeatureDescription
width/heightSpecifies the width or height of the browser window. Units are expressed as px for pixels. For example, width:200px
device-width / device-heightSpecifies the width or height of the entire device and not just the browser window. Units are expressed as px for pixels. For example, min-device-height:200px
resolutionSpecifies the pixel density. Units are dpi or dpcm. For example, max-resolution:600dpi
orientationSpecifies the orientation of the device. The supported values are portrait and landscape. Forexample, orientation:portrait
aspect-ratio / device-aspectratioSpecifies the pixel ratio of the browser window or the entire device. Values are expressed as the number of width pixels over the number of height pixels. For example, min-aspect-ratio:16/9
color / monochromeSpecifies the number of bits per pixel of color. For example, min-monochrome:2
color-indexSpecifies the number of colors. For example, max-color-index:256
scanSpecifies the scanning mode of a TV. The supported values are progressive and interlace. Forexample, scan:interlace
gridSpecifies the device type. The supported values are 0 and 1, where 1 is a grid device. For example, grid:0
Home 
  HTML CSS Book 
    HTML  

Document Structure:
  1. The doctype Element
  2. The html Element
  3. The head Element
  4. The body Element
  5. Setting the Document Title
  6. Setting the Base for Relative URLs
  7. Specifying Name/Value Metadata Pairs
  8. Declaring a Character Encoding
  9. Simulate an HTTP Header
  10. Defining CSS Styles
  11. Specifying the Media for a Style
  12. Denoting External Resources
  13. Defining a Favicon for Your Page
  14. Using the Scripting Elements
  15. Loading an External Scripting Library
  16. Deferring Execution of a Script
  17. Executing a Script Asynchronously
  18. The noscript Element
  19. Redirect the user to a different URL if it doesn't support JavaScript.
Related: