I have simple example that applies style when div height is less than 400px.
<html>
<head>
<style>
.card-container {
container-type: size;
container-name: sidebar;
}
.card-container {
background: #ffe4e8;
width: 200px;
height: 100px;
}
@container sidebar (max-height: 400px) {
.card-container div:first-child {
background: green;
}
}
</style>
</head>
<body>
<div class="card-container">
<div>
1
</div>
</div>
</body>
</html>
How can I make the same but with tbody height?
<html>
<head>
<style>
.table-container tbody {
container-type: size;
container-name: sidebar;
}
@container sidebar (max-height: 400px) {
.table-container tbody {
background: green;
}
}
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table class="table-container">
<thead>
<tr>
<th>123456789</th>
</tr>
</thead>
<tbody>
<tr><td>kkk</td></tr>
<tr><td>kkk</td></tr>
<tr><td>kkk</td></tr>
<tr><td>kkk</td></tr>
<tr><td>kkk</td></tr>
<tr><td>kkk</td></tr>
<tr><td>kkk</td></tr>
<tr><td>kkk</td></tr>
<tr><td>kkk</td></tr>
</tbody>
</table>
</body>
</html>
Maybe I’m doing something wrong or CSS container queries works only with div?
I need to apply style to table depending of tbody height.
Of course I can get tbody height with JavaScript and too apply style, but think that CSS and container queries is more clear.
2
Answers
CSS container queries are a powerful tool, but as of now, they are mainly supported for elements that have an explicit block-level(like the
tbody
element) container.But you can achieve this by add a js script that check if the
tbody
element height is less than 400px to add a class namespecial-size
.This is an example:
Internal table boxes cannot have size containment applied, and are explicitly excluded in the CSS Containment Level 2 Specification.
Furthermore the specification provides an explanatory note: