I came across this code on the following website: https://css-tricks.com/responsive-data-tables/, but unfortunately, it is not functioning as expected for my specific table within a Razor view.
Here is the table code that I am using:
<table class="table table-hover">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]">@Html.DisplayNameFor(model => model.LastName)</a>
</th>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["EmailSortParm"]">@Html.DisplayNameFor(model => model.Email)</a>
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
</tr>
}
</tbody>
</table>
Below is the CSS that I have applied:
table {
width: 100%;
border-collapse: collapse;
}
th {
background: #ccc;
font-weight: bold;
}
td, th {
padding: 6px;
border: 1px solid #ccc;
text-align: left;
}
/*Media Query*/
@media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px)
{
/* Force table to not be like tables anymore */
table, thead, tbody, th, td, tr {
display: block;
}
tr {
border: 1px solid #ccc;
}
td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}
td:before {
/* Now like a table header */
position: absolute;
/* Top/left values mimic padding */
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
}
/*Lable data*/
td:nth-of-type(1):before {
content: "FirstName";
}
td:nth-of-type(2):before {
content: "LastName";
}
td:nth-of-type(3):before {
content: "Email";
}
}
Preview of the table before the media query kicks in:
https://i.sstatic.net/tCaQV.png
Preview of the table after the media query takes effect:
https://i.sstatic.net/SgB6H.jpg
I simplified the code for readability purposes. Do you have any insights on what might be causing the issue?