I have a table
style that renders fine when its given enough space:
However, when the width of the parent container is not wide enough the table is hidden:
I can fix this by adding display: block
on the table
. This will add a horizontal scrollbar:
However, this causes the header to not take up available space when the parent container is very wide:
Is there a way I can get the scrollbar to appear when the parent container is too small, get the header to take up the available space and maintain the look and feel of the table?
:root {
--global-title-color: black;
--global-content-background-color: lightgreen;
--global-background-color: lightblue;
--global-border-color: red;
--global-border-radius: 5px;
--global-border-width-1: 1px;
--global-font-size-1: 20px;
--global-font-weight-bold: bold;
--global-space-fixed-2: 5px;
--global-space-fixed-3: 10px;
}
.container {
display: block;
background: yellow;
resize: horizontal;
overflow: hidden;
min-height: 150px;
}
table {
color: var(--global-title-color);
background-color: var(--global-content-background-color);
border-collapse: separate;
border-color: var(--global-title-color);
border-style: solid;
border-radius: var(--global-border-radius);
border-width: 0 var(--global-border-width-1) var(--global-border-width-1)
var(--global-border-width-1);
border-spacing: 0;
overflow: auto;
width: 100%;
thead {
th {
color: var(--global-background-color);
background-color: var(--global-title-color);
font-weight: var(--global-font-weight-bold);
font-size: var(--global-font-size-1);
padding: var(--global-space-fixed-2) var(--global-space-fixed-3);
vertical-align: bottom;
}
th:first-child {
border-top-left-radius: var(--global-border-radius);
}
th:last-child {
border-top-right-radius: var(--global-border-radius);
}
}
tbody {
td {
border-top: var(--global-border-width-1) solid var(--global-border-color);
min-width: 100px;
padding: var(--global-space-fixed-2) var(--global-space-fixed-3);
vertical-align: top;
}
tr:nth-child(2n) {
background-color: var(--global-background-color);
}
tr:last-child {
td:first-child {
border-bottom-left-radius: var(--global-border-radius);
}
td:last-child {
border-bottom-right-radius: var(--global-border-radius);
}
}
}
}
<div class="container">
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Facilis voluptatum inventore iure blanditiis ab ipsum nostrum repellat cum tempore. Quas harum dolores totam voluptatem deserunt et praesentium nihil placeat. Voluptas.</p>
<table>
<thead>
<tr>
<th>SDK</th>
<th>Default namespaces</th>
</tr>
</thead>
<tbody>
<tr>
<td>Microsoft.NET.Sdk</td>
<td><code class="language-inline-text">System.Collections.Generic</code></td>
</tr>
<tr>
<td>Microsoft.NET.Sdk.Web</td>
<td><code class="language-inline-text">System.Net.Http.Json</code></td>
</tr>
</tbody>
</table>
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Facilis voluptatum inventore iure blanditiis ab ipsum nostrum repellat cum tempore. Quas harum dolores totam voluptatem deserunt et praesentium nihil placeat. Voluptas.</p>
</div>
All code is also available in the CodePen below:
https://codepen.io/muhammadrehansaeed/pen/JjydLVV?editors=1100