marcj / css-element-queries

CSS Element-Queries aka Container Queries. High-speed element dimension/media queries in valid css.
http://marcj.github.io/css-element-queries/
MIT License
4.27k stars 487 forks source link

Can't use display:none #274

Closed flivni closed 4 years ago

flivni commented 5 years ago

I want to conditionally display a div based on its width.

I'd like to do something like this:

.mydiv[min-width~="300px"] {
    display: none;
}

But it doesn't work.

I thought at first this was a duplicate of #46. But, it looks like #46 has been fixed. And perhaps my situation is a bit different.

Demonstration in this fiddle.

marcj commented 4 years ago

That's not logically resolvable. If you hide an element (like in you did) the element loses its dimensions and will never receive an resize event anymore. Without dimensions it's impossible to detect whether the element got bigger or smaller to check if a rule matches. So, what you should do is to put the rule on a parent that is always visible. https://jsfiddle.net/21ekLn5y/2/

flivni commented 4 years ago

Thanks. Your workaround and explanation makes sense. I think my scenario might be a common case -- perhaps it should be documented?