<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tableau Sticky + Scroll Snap Fix</title>
<style>
/* Conteneur principal pour le scroll */
.table-wrapper {
width: 100%;
max-height: 400px;
overflow: auto;
border: 1px solid #ccc;
scroll-snap-type: both mandatory;
}
table {
width: 100%;
table-layout: fixed; /* Fixe la largeur des colonnes */
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: center;
height: 40px;
border: 1px solid #ccc;
white-space: nowrap;
min-width: 120px;
}
/* Sticky Header pour les premières lignes */
th {
background-color: #f2f2f2;
position: sticky;
top: 0;
z-index: 10; /* Assure que les en-têtes restent visibles */
border-bottom: 2px solid #aaa;
}
/* Sticky First Column */
td:first-child, th:first-child {
position: sticky;
left: 0;
background-color: white;
z-index: 20; /* Priorité maximale pour la première colonne */
width: 120px; /* Fixe la largeur de la première colonne */
min-width: 120px;
border-right: 2px solid #ccc; /* Bordure entre les colonnes */
}
/* Sticky Second Column */
td:nth-child(2), th:nth-child(2) {
position: sticky;
left: 120px; /* Décale après la première colonne */
background-color: white;
z-index: 19; /* Priorité juste en dessous de la première colonne */
width: 120px; /* Fixe la largeur de la deuxième colonne */
min-width: 120px;
border-right: 2px solid #ccc; /* Bordure entre les colonnes */
}
/* Sticky Third Column */
td:nth-child(3), th:nth-child(3) {
position: sticky;
left: 240px; /* Décale après la deuxième colonne (120px pour la première + 120px pour la deuxième) */
background-color: white;
z-index: 18; /* Priorité encore plus faible que les deux premières colonnes */
width: 120px; /* Fixe la largeur de la troisième colonne */
min-width: 120px;
border-right: 2px solid #ccc; /* Bordure entre les colonnes */
}
/* Modifications pour la première ligne */
tr:first-child th {
z-index: 15; /* Assure que les cellules de la première ligne sont toujours au-dessus des autres cellules */
background-color: #f2f2f2;
}
/* Alternance des couleurs pour lisibilité */
tbody tr:nth-child(odd) {
background-color: #f9f9f9;
}
/* Colonnes après la troisième : fixées à une largeur */
th:nth-child(n+4), td:nth-child(n+4) {
width: 150px; /* Fixe une largeur pour les autres colonnes */
min-width: 150px; /* Largeur minimale pour les autres colonnes */
}
</style>
</head>
<body>
<div class="table-wrapper">
<table>
<thead>
<tr>
<th></th> <!-- Coin haut gauche -->
<th>plouf</th>
<th>cretin</th> <!-- Nouvelle colonne ajoutée -->
<?php for ($i = 1; $i <= 50; $i++): ?>
<th>Col <?php echo $i; ?></th>
<?php endfor; ?>
</tr>
</thead>
<tbody>
<?php for ($i = 1; $i <= 50; $i++): ?>
<tr>
<td>Row <?php echo $i; ?></td>
<td>Data <?php echo $i . "-2"; ?></td>
<td>cretin <?php echo $i; ?></td> <!-- Nouvelle cellule ajoutée -->
<?php for ($j = 1; $j <= 50; $j++): ?>
<td>Data <?php echo $i . "-" . $j; ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</tbody>
</table>
</div>
</body>
</html>