3

I can't seem to get container queries to work. I'm trying to change the styles of HTML tables using container queries, but the styles within the container are not being applied:

table.df-price-table {
  border: 1px solid #ccc;
  border-collapse: collapse;
  margin: 0;
  padding: 0;
  width: 100%;
  table-layout: fixed;
  container-type: inline-size;
  container-name: dftable;
}

table.df-price-table caption {
  font-size: 1.5em;
  margin: .5em 0 .75em;
}

table.df-price-table tr {
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  padding: .35em;
}

table.df-price-table th,
table.df-price-table td {
  padding: .625em;
  text-align: center;
}

table.df-price-table th {
  font-size: .85em;
  letter-spacing: .1em;
  text-transform: uppercase;
}

@container dftable (max-width: 600px) {
  caption {
    font-size: 1.3em;
  }
  thead {
    border: none;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
  }
  tr {
    border-bottom: 3px solid #ddd;
    display: block;
    margin-bottom: .625em;
  }
  td {
    border-bottom: 1px solid #ddd;
    display: block;
    font-size: .8em;
    text-align: right;
  }
  td::before {
    content: attr(data-label);
    float: left;
    font-weight: bold;
    text-transform: uppercase;
  }
  td:last-child {
    border-bottom: 0;
  }
}
<table class="df-price-table">
  <caption>Statement Summary</caption>
  <thead>
    <tr>
      <th scope="col">Account</th>
      <th scope="col">Due Date</th>
      <th scope="col">Amount</th>
      <th scope="col">Period</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td data-label="Account">Visa - 3412</td>
      <td data-label="Due Date">04/01/2016</td>
      <td data-label="Amount">$1,190</td>
      <td data-label="Period">03/01/2016 - 03/31/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Account">Visa - 6076</td>
      <td data-label="Due Date">03/01/2016</td>
      <td data-label="Amount">$2,443</td>
      <td data-label="Period">02/01/2016 - 02/29/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Account">Corporate AMEX</td>
      <td data-label="Due Date">03/01/2016</td>
      <td data-label="Amount">$1,181</td>
      <td data-label="Period">02/01/2016 - 02/29/2016</td>
    </tr>
    <tr>
      <td scope="row" data-label="Acount">Visa - 3412</td>
      <td data-label="Due Date">02/01/2016</td>
      <td data-label="Amount">$842</td>
      <td data-label="Period">01/01/2016 - 01/31/2016</td>
    </tr>
  </tbody>
</table>

2
  • Just to be sure: Which browser and version are you using while testing this? Commented Mar 14, 2023 at 14:28
  • I'm using Chrome Version 110.0.5481.177. Am i right in assuming Container Queries are live now? Or are they still behind a feature flag in the browser?
    – Phil
    Commented Mar 14, 2023 at 14:57

2 Answers 2

4

Known through the w3c, giving an element inline-size containment has no effect if any of the following are true:

  • If the element does not generate a principal box (as is the case with display: contents or display: none)
  • If its inner display type is table
  • If its principal box is an internal table box
  • If its principal box is an internal ruby box or a non-atomic inline-level box
0
3

You'll have to add a wrapper as according to the CSS specification container queries don't work on tables:

.wrapper {
  container-type: inline-size;
  container-name: dftable;
}

table.df-price-table {
  border: 1px solid #ccc;
  border-collapse: collapse;
  margin: 0;
  padding: 0;
  width: 100%;
  table-layout: fixed;
}

table.df-price-table caption {
  font-size: 1.5em;
  margin: .5em 0 .75em;
}

table.df-price-table tr {
  background-color: #f8f8f8;
  border: 1px solid #ddd;
  padding: .35em;
}

table.df-price-table th,
table.df-price-table td {
  padding: .625em;
  text-align: center;
}

table.df-price-table th {
  font-size: .85em;
  letter-spacing: .1em;
  text-transform: uppercase;
}

@container dftable (max-width: 600px) {
  caption {
    font-size: 1.3em;
  }
  thead {
    border: none;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
  }
  tr {
    border-bottom: 3px solid #ddd;
    display: block;
    margin-bottom: .625em;
  }
  td {
    border-bottom: 1px solid #ddd;
    display: block;
    font-size: .8em;
    text-align: right;
  }
  td::before {
    content: attr(data-label);
    float: left;
    font-weight: bold;
    text-transform: uppercase;
  }
  td:last-child {
    border-bottom: 0;
  }
}
<div class="wrapper">
  <table class="df-price-table">
    <caption>Statement Summary</caption>
    <thead>
      <tr>
        <th scope="col">Account</th>
        <th scope="col">Due Date</th>
        <th scope="col">Amount</th>
        <th scope="col">Period</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td data-label="Account">Visa - 3412</td>
        <td data-label="Due Date">04/01/2016</td>
        <td data-label="Amount">$1,190</td>
        <td data-label="Period">03/01/2016 - 03/31/2016</td>
      </tr>
      <tr>
        <td scope="row" data-label="Account">Visa - 6076</td>
        <td data-label="Due Date">03/01/2016</td>
        <td data-label="Amount">$2,443</td>
        <td data-label="Period">02/01/2016 - 02/29/2016</td>
      </tr>
      <tr>
        <td scope="row" data-label="Account">Corporate AMEX</td>
        <td data-label="Due Date">03/01/2016</td>
        <td data-label="Amount">$1,181</td>
        <td data-label="Period">02/01/2016 - 02/29/2016</td>
      </tr>
      <tr>
        <td scope="row" data-label="Acount">Visa - 3412</td>
        <td data-label="Due Date">02/01/2016</td>
        <td data-label="Amount">$842</td>
        <td data-label="Period">01/01/2016 - 01/31/2016</td>
      </tr>
    </tbody>
  </table>
</div>

1
  • 1
    Thanks for taking a look and getting back to me, that's really helpful.
    – Phil
    Commented Mar 15, 2023 at 10:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.