Contain Your Excitement

Eric Portis ★ SmashingConf NYC ★ 2017-06-13

Thanks [intro person]

Who am I?

Headshot
arrow

Eric!

Element queries!

Container?

“Element?” “Container?”

Same-same.

= media queries for elements

Media queries are a whole-viewport-solution...

...designed to solve whole-document problems.


	<link rel="stylesheet" type="text/css" 
		media="print" href="serif.css">
	<link rel="stylesheet" type="text/css" 
		media="screen" href="sans-serif.css">
	

	@media print {
		* { font-family: serif }
	}
	@media screen {
		* { font-family: sans-serif }
	}
	

	@media screen and (min-width: 400px) {
		...
	}
	

Responsive pages


.block {
	background-color: lightblue; 
}

@media (min-width: 797px) {
	.block {
		background-color: deeppink;
	}
}

.block:min-width(100px) {
	background-color: deeppink;
}

People have been asking

for element queries

A screenshot of http://blog.andyhume.net/responsive-containers/

Responsive Containers by Andy Hume

We need native CSS media queries at the element/component/widget level, not just the viewport. Make it so, internetz.

— Nicolas (@necolas) February 7, 2013

Great read about the #1 missing tool for responsive design: Element queries http://t.co/EARts4HWWr (viewport queries are still valuable)

— Brad Frost 🇺🇸 (@brad_frost) April 4, 2013

Ahh, Spring—when a young man’s fancy turns to thoughts of element queries, what that syntax might look like, and how one might prototype it.

— Mat Marquis (@wilto) May 14, 2014

Monthly tweet about how _super_ helpful natively supported element queries would be.

— Tim Kadlec (@tkadlec) July 28, 2014

Why element queries?

Less CSS to write, more modular & maintainable CSS/HTML, smarter responsive images, web components, etc.

— Tim Kadlec (@tkadlec) July 28, 2014

Coulda used ElementQueries about 45 times today.

— Dave Rupert (@davatron5000) January 22, 2015

Does anyone know what the current state of #CSS Element Queries is? Any updates on whether cyclic dependency can be solved/worked around?

— Sara Soueidan 🐦 (@SaraSoueidan) May 3, 2015

If I had to choose only one new CSS feature to be implemented asap, it would be container queries. So much NEED. #respond16

— Sara Soueidan 🐦 (@SaraSoueidan) April 8, 2016

All I want in 2017 on the Web is Container Queries. Seriously.

— Sara Soueidan 🐦 (@SaraSoueidan) December 23, 2016

hypothesis

If element queries were real, over 50% of the media queries we write would be element queries.

— Chris Coyier (@chriscoyier) November 23, 2015

I wonder when and if we’ll ever get Container Queries in CSS. It’s only possibly the most useful RWD technique. The wait is depressing.

— Sara Soueidan 🐦 (@SaraSoueidan) June 18, 2016

are container queries here yet

dot tumblr dot corn

— Ethan Marcotte (@beep) April 21, 2016

FUTURE BROWSER v154.2 release notes
* JS engine optimizations—technically now a time machine
* nothing on container queries yet—stop asking

— Zach Leatherman (@zachleat) June 29, 2016

The fact that we’ll probably get stable, cross browser WebVR before container queries is a good example of what’s wrong with our industry.

— Zach Leatherman (@zachleat) October 25, 2016

I want my
element
element
element
I want my element
element
element
element
Chilis element queries, Chilis element queries
barbecue sauce

— Brad Frost 🇺🇸 (@brad_frost) August 17, 2016

Be our witness? @chriscoyier and I just bet $20 that element queries would get implemented. I said no, he said yes. :)

— Nicole Sullivan (@stubbornella) June 19, 2015

@grigs DO CONTAINER QUERIES NEXT pic.twitter.com/fVT8NXGnqQ

— Ethan Marcotte (@beep) May 11, 2017

What’s the hold up?

Please excuse a wildly simplified explanation of

the rendering pipeline

A big ol' diagram

🔁

Problem #1:

Circular dependencies


.block:min-width(10em) {
	width: 5em;
}

.container {
	display: flex;
}

.block:min-width(3em) {
	border: 0.25em solid deeppink;
}
Super
califragilistic
expiali
docious

.container {
	display: flex;
}

.block:min-width(3.25em) {
	letter-spacing: 0.1em;
}

Element Queries

Container!


.wrapper {
	border: 0.0625em solid deeppink;
}

.block:container(width > 10em) {
	width: 5em;
}

Two solutions...

(closest) Container (whose width is not influenced by its children) Queries

screenshot of Martin Auswoger’s blog post about container query solutions

Conceived of + implemented by Martin Auswo¨ger

screenshot of the CSS Containment Module Level 1 draft

contain: strict;

Spec’d in CSS Containment Module Level 1
and implemented in Blink

🏚

Problem #2:

Breaks layout engine architecture

On Thu, 21 Mar 2013 at 3:08 PM, Elliott Sprehn wrote:

> This doesn't seem very easy to implement because it breaks the sequential
> processing model:
> 
> 1. resolve style on elements such that you have a flattened set of
> properties.
> 2. layout the elements if things are different then the last time we did
> step 1.

> This would be a big undertaking in Webkit so I don't think it falls
> anywhere in the near future.


On Thu, Mar 21, 2013 at 6:34 PM, Elliott Sprehn wrote:

> I think there's a high implementation cost for this kind of thing

> It certainly could be done given enough time and someone very
> motivated...
Section 3.2 of the HTML design principles, which reads, “In case of conflict, consider users over authors over implementors over specifiers over theoretical purity.”

From the HTML Design Principles

🐌

Problem #3:

Fundamentally slow

🐇

...and it needs to be FAST

16 milliseconds

A screenshot of smashingconf.com with the dev tools open

5ms best / 35ms worst

A big ol' diagram
EQCSS homepage screenshot
A screenshot of the pricing comparison which performed so badly

40ms best / 150ms worst

Two screenshots of the Shopify admin page, showing how it responsively adapts

Shopify implemented (and then un-implemented) container queries

@_lemonmade @snookca Any insight into why Shopify moved away from JS-based container queries in the admin? FoUC, or?

— Eric Portis (@etportis) June 1, 2017

We had problems with forced reflows and layout thrashing, especially on page load. It was probably partially our implementation of them tho.

— Dominic McPhee (@dfmcphee) June 2, 2017

But were they ever a joy to use. I miss them.

— Scott Vinkle (@svinkle) June 2, 2017

Paths forward

Two summaries of layout performance, showing the enormous speedup enabled by `contain: layout;`.

contain: strict;
to the rescue?

Asynchronicity

Knock. there? Async container queries Who’s knock!

A screenshot of the ResizeObserver spec

I ResizeObserver

JavaScript!?

What about CSS?

🎩🐇

Houdini

Houdini isn't ready yet.

No.

The Prolyfills are Alright

Fight jank!

Load container query libraries in the <head>

Why wait? Iterate!

issue screenshot

For extra credit...

document your use cases!

Carpe query.

Thanks!