Wrappers
… that scope parts of your CSS
… based on device data
<link
rel="stylesheet"
media="screen and (min-width: 16em)"
href="example.css" />
@media screen and (min-width: 16em) {
/* example css */
}
@import url(example.css)
screen and (min-width: 16em);
@document url(https://example.com) {
/* custom CSS for example.com */
}
Preprocessors, PostCSS, and some CSS-in-JS tools allow you to nest a query inside a selector block.
.selector {
@media (min-width: 36em) {
/* CSS */
}
}
screen and (min-width: 16em)
media type: screen
media feature: (min-width: 16em)
(Useful Ones)
(Deprecated Ones)
Some key-value pairs
Some keyword-only
width
min-width
max-width
device-width
πmin-device-width
πmax-device-width
π.card {
width: 100%;
}
@media (min-width: 45em) {
.card {
width: 50%;
}
}
height
min-height
max-height
device-height
πmin-device-height
πmax-device-height
π.nav {
height: 120px;
}
@media (max-height: 40em) {
.nav {
height: 70px;
}
}
resolution
min-resolution
max-resolution
-webkit-device-pixel-ratio
π.picture {
background-image: url(pic-1x.jpg);
}
@media (min-resolution: 150dpi) {
.picture {
background-image: url(pic-2x.jpg);
}
}
portrait
landscape
aspect-ratio
min-aspect-ratio
max-aspect-ratio
device-aspect-ratio
πmin-device-aspect-ratio
πmax-device-aspect-ratio
π.media--16-9 {
height: 100vh;
width: 177.778vh;
}
@media (min-aspect-ratio: 16/9) {
.media--16-9 {
height: 56.25vw;
width: 100vw;
}
}
color
(boolean)color-index
(integer)min-color-index
(integer)max-color-index
(integer)monochrome
(boolean)min-monochrome
(integer)max-monochrome
(integer)color-gamut
(keyword): srgb
, p3
, rec2020
a {
color: #304FFE; /* custom blue */
text-decoration: none;
}
@media (monochrome) {
a {
text-decoration: underline;
}
}
progressive
interlace
Just for television sets
grid
(1 || 0)Screen is bitmap or grid layout
inverted-colors
pointer
hover
any-pointer
any-hover
light-level
scripting
and
not
only
,
(or)()
(group logic)@media screen and (min-width: 36em) { }
@media not screen { }
@media only screen and (min-width: 36em) { }
@media print, (monochrome) { }
@media ((min-width: 36em)
and (aspect-ratio: portrait)),
((max-width: 35.9375em)
and (aspect-ratio: landscape)) { }
Pick a direction and stick with it (as much as possible).
Start small and override up.
em
is most consistent with user/browser zoom
px
is “easiest” if you’re mixing min
and max
As much as you can, use the same set of limited breakpoints.
Let your content decide where breakpoints go.
Adjust your browser size to the point where your content breaks.
That’s a breakpoint.
Friendly reminder that frameworks donβt set breakpoint values: content does.
— James Steinbach (@jdsteinbach) April 27, 2015
Don’t pick breakpoints based on specific device sizes; put breakpoints in the gaps between clusters of common device sizes.
Some components will need a breakpoint that’s not one of your “major” breakpoints: create tweakpoint for that component.
Watch for a future talk about creating responsive components without media queries!
Someday container queries might be a thing, but that day is not yet on the calendar.
Window.matchMedia()
var mq = matchMedia("(min-width: 36em)");
if (mq.matches) {
// do some code specific to that breakpoint
}
MediaQueryList.addListener()
var mq = matchMedia("(min-width: 36em)");
mq.addListener(mqTriggeredFunction);
function mqTriggeredFunction(event) {
if (event.matches) {
// do some code specific to that breakpoint
}
}
Load a JS file that tests for all the conditions/features you need then adds a class to `html` so you can scope CSS selectors.
@supports (display: grid) {
display: grid;
}
Accepts CSS property-value pair: the browser evaluates both the property and the value.
(display: grid) and (filter: blur(3px))
(display: grid) or (filter: blur(3px))
@supports (grid-gap: 20px) {
/* Grid styles */
}
@supports (margin: env(safe-area-inset-left)) {
/* iPhone X styles */
}
All modern browsers but IE support feature queries.
@supports (top: 0) {
/* Code for not-IE browsers */
}
Use them when you can do something with new CSS… that requires you to change properties in your fallback/original CSS.
jdsteinbach