JSFiddle - React, Tailwind, and code Playground
by Prathameshsb
HTML
<div class="table-container">
<table id="data-table">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>City</th>
</tr>
</thead>
<tbody>
<!-- Table rows will be dynamically populated using JavaScript -->
</tbody>
</table>
</div>
CSS
body {
font-family: 'Arial', sans-serif;
margin: 0;
}
.table-container {
overflow-x: auto;
}
#data-table {
border-collapse: collapse;
width: 100%;
margin: 20px 0;
}
#data-table th, #data-table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
#data-table th {
background-color: #f2f2f2;
}
/* Hide table rows on smaller screens */
@media screen and (max-width: 600px) {
#data-table th, #data-table td {
display: block;
width: 100%;
box-sizing: border-box;
}
#data-table th {
text-align: center;
}
}
JavaScript
document.addEventListener("DOMContentLoaded", function () {
// Dummy data for demonstration
const data = [
{ name: "John Doe", email: "[email protected]", phone: "123-456-7890", city: "New York" },
{ name: "Jane Doe", email: "[email protected]", phone: "987-654-3210", city: "San Francisco" },
// Add more data as needed
];
// Populate the table with data
const tableBody = document.querySelector("#data-table tbody");
data.forEach((item) => {
const row = document.createElement("tr");
Object.values(item).forEach((value) => {
const cell = document.createElement("td");
cell.textContent = value;
row.appendChild(cell);
});
tableBody.appendChild(row);
});
});