<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Stefan Matei</title>
    <description>The latest articles on Forem by Stefan Matei (@nonsalant).</description>
    <link>https://forem.com/nonsalant</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F133289%2F0acb6746-f681-442f-a341-380fe498d59f.jpg</url>
      <title>Forem: Stefan Matei</title>
      <link>https://forem.com/nonsalant</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nonsalant"/>
    <language>en</language>
    <item>
      <title>#TIL: Display table and inline are incompatible with container queries</title>
      <dc:creator>Stefan Matei</dc:creator>
      <pubDate>Thu, 23 Jan 2025 00:28:50 +0000</pubDate>
      <link>https://forem.com/nonsalant/til-display-table-and-inline-are-incompatible-with-container-queries-48l7</link>
      <guid>https://forem.com/nonsalant/til-display-table-and-inline-are-incompatible-with-container-queries-48l7</guid>
      <description>&lt;p&gt;I had a table in a web component and needed to hide some unimportant columns on small widths.&lt;/p&gt;

&lt;p&gt;If you’re curious, this is how you’d hide the second and third columns in a table (for both table headings and normal cells):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nd"&gt;:is&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nd"&gt;:is&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;2&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;3&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is how I tried to use container queries to only do that in narrow places (less than 600px in this case):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;table&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c"&gt;/* ! this part doesn’t actually work */&lt;/span&gt;
  &lt;span class="py"&gt;container-type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* hide 2nd and 3rd columns when small */&lt;/span&gt;
&lt;span class="nd"&gt;:is&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nd"&gt;:is&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;2&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;3&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;@container&lt;/span&gt; &lt;span class="err"&gt;(width&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt; &lt;span class="err"&gt;600px)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem was that these are the only &lt;code&gt;display&lt;/code&gt; values actually compatible with container queries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;block&lt;/li&gt;
&lt;li&gt;inline-block&lt;/li&gt;
&lt;li&gt;flex&lt;/li&gt;
&lt;li&gt;inline-flex&lt;/li&gt;
&lt;li&gt;grid&lt;/li&gt;
&lt;li&gt;inline-grid&lt;/li&gt;
&lt;li&gt;flow-root&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Among the ones that are not are some that make sense:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;inline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some that are obvious:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;contents&lt;/li&gt;
&lt;li&gt;none&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A few I’ve never used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ruby&lt;/li&gt;
&lt;li&gt;ruby-base&lt;/li&gt;
&lt;li&gt;ruby-text&lt;/li&gt;
&lt;li&gt;ruby-base-container&lt;/li&gt;
&lt;li&gt;ruby-text-container&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And &lt;em&gt;all&lt;/em&gt; of the table ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;table&lt;/li&gt;
&lt;li&gt;table-row-group&lt;/li&gt;
&lt;li&gt;table-header-group&lt;/li&gt;
&lt;li&gt;table-footer-group&lt;/li&gt;
&lt;li&gt;table-row&lt;/li&gt;
&lt;li&gt;table-cell&lt;/li&gt;
&lt;li&gt;table-column-group&lt;/li&gt;
&lt;li&gt;table-column&lt;/li&gt;
&lt;li&gt;table-caption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I had never considered this. &lt;/p&gt;

&lt;p&gt;The solution is to put the container on the parent element of the table, and the reason I mentioned at the start that this was inside a web component (which is the table’s direct parent) is that web components are, by default, &lt;code&gt;display: inline&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But, unlike with a table, there’s no problem changing a component’s display to block. &lt;/p&gt;

&lt;p&gt;This works (and doesn’t break any of the table’s semantics):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nd"&gt;:host&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="py"&gt;container-type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* hide 2nd and 3rd columns when small */&lt;/span&gt;
&lt;span class="nd"&gt;:is&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;td&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="nt"&gt;th&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nd"&gt;:is&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;2&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;3&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;@container&lt;/span&gt; &lt;span class="err"&gt;(width&lt;/span&gt; &lt;span class="err"&gt;&amp;lt;&lt;/span&gt; &lt;span class="err"&gt;600px)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I’m using &lt;code&gt;:host&lt;/code&gt; as the selector above, because I’m doing it from inside the Shadow DOM (and the scope of container queries is limited to the shadow DOM bound), but you can use your component’s hyphenated tag name if you’re using Light DOM. &lt;/p&gt;

</description>
      <category>css</category>
      <category>containerqueries</category>
      <category>webcomponents</category>
    </item>
  </channel>
</rss>
