CSS
HTML
JavaScript
jQuery
PHP
SQL
WordPress
CSS Reference

Quick Reference

The @media Rule describes CSS media queries to be used to define style rules for specific device types or a wide range of screen sizes and capabilities.

Below are three of the most used media queries.

  • The first example targets screens that are 767px and smaller
  • The second example targets screens that are 768px or larger
  • The third example targets screens between 1024px and 1439px inclusive

The rules applicable to each scenario are then written as normal, but inside the media query.

				
					@media screen and (max-width: 767px) {
    .bar {
        display: none;
    }
}
				
			
				
					@media screen and (min-width: 768px) {
    .foo {
        display: none;
    }
}
				
			
				
					@media screen and (min-width: 1024px) and (max-width: 1439px) {
    .foo {
        background-color: white;
    }
}
				
			

Using And with Or

Using a comma between size settings acts as an “or” statement. The following will target screens that are between 1024px and 1439px (inclusive) OR screens that are 1920px or larger.

				
					@media screen and (min-width: 768px) and (max-width: 1439px), (min-width: 1920px) {
    .foo {
        background-color: white;
    }
}
				
			

Orientation: Portrait / Landscape

The screen orientation can also be targeted (works great for tablets to determine how it’s being held by the user).

  • The first example targets screens that are wider than they are tall
  • The second example targets screens that are taller than they are wide
				
					@media screen and (orientation: landscape) {
    .foo {
        background-color: white;
    }
}
				
			
				
					@media screen and (orientation: portrait) {
    .foo {
        background-color: white;
    }
}
				
			

Print and Speech

Just as we can target the “screen”, we can target a device intended to print or read a document.

  • The first example targets printers
  • The second example targets printers where the page is taller than it is wide
  • The third targets speech reading devices
				
					@media print {
    .foo {
        background-color: white;
    }
}
				
			
				
					@media print and (orientation: portrait) {
    .foo {
        background-color: white;
    }
}
				
			
				
					@media speech {
    .foo {
        background-color: white;
    }
}
				
			

Media Types

ValueDescription
allUsed for all media type devices
printUsed for printers
screenUsed for computer screens, tablets, smart-phones etc.
speechUsed for screenreaders that read the page out loud to the user

Media Features

ValueDescription
any-hoverDoes any available input mechanism allow the user to hover over elements?
any-pointerIs any available input mechanism a pointing device, and if so, how accurate is it?
aspect-ratioThe ratio between the width and the height of the viewport
colorThe number of bits per color component for the output device
color-gamutThe approximate range of colors that are supported by the user agent and output device
color-indexThe number of colors the device can display
gridWhether the device is a grid or bitmap
heightThe viewport height
hoverDoes the primary input mechanism allow the user to hover over elements?
inverted-colorsIs the browser or underlying OS inverting colors?
light-levelCurrent ambient light level
max-aspect-ratioThe maximum ratio between the width and the height of the display area
max-colorThe maximum number of bits per color component for the output device
max-color-indexThe maximum number of colors the device can display
max-heightThe maximum height of the display area, such as a browser window
max-monochromeThe maximum number of bits per "color" on a monochrome (grayscale) device
max-resolutionThe maximum resolution of the device, using dpi or dpcm
max-widthThe maximum width of the display area, such as a browser window
min-aspect-ratioThe minimum ratio between the width and the height of the display area
min-colorThe minimum number of bits per color component for the output device
min-color-indexThe minimum number of colors the device can display
min-heightThe minimum height of the display area, such as a browser window
min-monochromeThe minimum number of bits per "color" on a monochrome (grayscale) device
min-resolutionThe minimum resolution of the device, using dpi or dpcm
min-widthThe minimum width of the display area, such as a browser window
monochromeThe number of bits per "color" on a monochrome (grayscale) device
orientationThe orientation of the viewport (landscape or portrait mode)
overflow-blockHow does the output device handle content that overflows the viewport along the block axis
overflow-inlineCan content that overflows the viewport along the inline axis be scrolled
pointerIs the primary input mechanism a pointing device, and if so, how accurate is it?
resolutionThe resolution of the output device, using dpi or dpcm
scanThe scanning process of the output device
scriptingIs scripting (e.g. JavaScript) available?
updateHow quickly can the output device modify the appearance of the content
widthThe viewport width

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.

CSS
HTML
JavaScript
jQuery
PHP
SQL
WordPress

Why 1SMARTchicken?

This site was built and is maintained to benefit my autistic son.
See More →

My Son's Name is Johnny

He was diagnosed as autistic quite late, at age four...
His story

Buy Me a Coffee

Thanks for your support!

Feedback

If you see an error on the page or the code itself is incorrect or incomplete, or just plain wrong, please let us know. We’re always learning. NOTE: we do not sell your information and will not send you spam emails.