Ditch the
Media Queries
Modern CSS Replacements �for Better Responsive Code
Hello!
I’m Kathryn Grayson Nanz (@kathryngrayson)
Both are also linked in the Stir Trek Discord #modern-css channel:�https://discord.gg/aDJJBhNUc6
Hot take: writing CSS is the best �part of web development
Everything new in CSS in 2022
What’s coming soon
The end of an era
Responsive Design Approaches
Device-based Breakpoints
Using media queries to serve entirely different versions of your site to people based on their device.
Content-based Breakpoints
Using media queries to adjust the layout when the content no longer fit comfortably on the page.
Fluid Design
Using modern CSS to set the size and layout of elements proportionally in relation to the width of the screen.
The Big Picture
01.
Using CSS Grid and Flexbox �for layout structuring
Comparing CSS Grid and Flexbox
CSS Grid
Flexbox
The Grid
Grid and Margin
Margin
Grid
Columns, Rows, and Modules
Row
Column
Module
Spatial Zones
Spatial Zones
Gaps
Row Gaps
Column Gaps
CSS Grid Example
.grid-item-1 { � grid-column: 2 / span 4; � grid-row: 3 / span 2; �}
CSS Grid Example
.grid { � display: grid; � grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); �}��<div class="grid"> � <div></div> � <div></div> � <div></div> �</div>
CSS Grid Resources
All complaints and jokes �about how hard it is to �center elements in CSS �are now strictly forbidden
.vert-horiz-center { � display: flex; � justify-content: center; � align-items: center; �}
Flexbox Example
.flex { � display: flex; � flex-wrap: wrap; �}
.flex > div { � width: 300px �}��<div class="flex"> � <div></div> � <div></div> � <div></div> �</div>
Flexbox Resources
The Middle Man
02.
CSS properties for responsive �elements on your page
Container Queries
A brand new way to apply conditional styles based on the size of a specific container element on the page.
Unnamed container example
<section class="about">� <h2>About Us</h2>� <div class=”flex”>� <img src=”X” alt=”X”/>� <p>Lorem ipsum...</p>� </div>�</section>
img { max-width: 80% }�h2 {font-size: 3rem; }�.flex { display: flex }�.about {� container-type: size; �}
@container(max-width: 600px){� img { max-width: 50%; }� h2 { font-size: 2rem; }� .flex { flex-wrap: wrap;}�}
HTML CSS
Container Types
.about {� container-type: size [or] inline-size; �}
The container-type property can be set to one of three options:
Style queries are simply none of our business…yet
Named container example
<section class="about">� <h2>About Us</h2>� <div class=”flex”>� <img src=”X” alt=”X”/>� <p>Lorem ipsum...</p>� </div>�</section>
img { max-width: 80% }�h2 {font-size: 3rem; }�.flex { � container-type: size; � container-name: flex;� display: flex; �}�.about {� container-name: about; � container-type: size;�}
@container about (max-width: 600px){� h2 { font-size: 2rem; }�}
.about {� container: about / size;�}
HTML CSS
Or, use the shorthand:
New container query units
@container(max-width: 600px){� p { font-size: clamp(1rem, 1.5cqi, 2rem); }�}
There are new units designed specifically for use relative to containers:
aspect-ratio
.video-wrapper { aspect-ratio: 16/9; }
Did you know that there's a CSS property specifically designed for aspect ratios? �https://css-tricks.com/almanac/properties/a/aspect-ratio/
min and max-height, min and max-width
.img { � width: 100%; � max-width: 50%; � min-width: 200px; �}
You can often use clamp() instead, but these are still good tools to know!
Fully Functional
03.
Using the new(ish) math functions� to do the heavy lifting
clamp()
.img { � /* the format is `clamp(min value, base value, max value)` */ width: clamp(200px, 100%, 50%); �}
calc()
.child { � width: calc(100vw / 3); �}
min() and max()
.img1 { � width: min(30%, 200px) �}
.img2 { � width: max(50vh, 800px) �}
All the Small Things
04.
Switching fixed units for responsive units
Absolute values: not even once
Viewport Units
.page-wrapper { � height: 100vh; � width: 100vw; �}
Viewport Units
h1 { � font-size: 20vmin; �}
Huzzah!
Small, Large, and Dynamic Viewport Units
body {� height: 100dvh;�}
Fine-tuned and specific viewport units for every situation you could ever encounter:�
rem and em
.h2 { font-size: 2.5em }
p { font-size: clamp(1rem, 2.5vw, 2rem); }
You can combine vw with rem and clamp() to create a fluid typography system for your application.
ch and ex
sup { � line-height: 0; � position: relative; � vertical-align: baseline; � bottom: 1ex; �}
input { � width: 4ch; �}
Percentages
.container { � width: 25%; �}
Exceptions
05.
Cases when media queries are �still useful and worth considering
User Preference Media Features
@media (prefers-color-scheme: dark) {� .day.dark-scheme { � background: #333; � color: white; � }� .night.dark-scheme { � background: black; � color: #ddd; � }�}
Changing How Elements Work
@media only screen and (min-width: 600px) {� .nav-icon {� display: none;� }�}
Formatting for Print
@media print { � #header, #footer, #nav { � display: none; � } �}