<!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;
box-sizing: border-box; /* Applique box-sizing sur tout le tableau */
}
th, td {
padding: 10px;
text-align: center;
height: 40px;
border: 1px solid #ccc;
white-space: nowrap;
box-sizing: border-box; /* Important pour gérer correctement le padding et la largeur */
}
/* Sticky Header */
th {
background-color: #f2f2f2;
position: sticky;
top: 0;
z-index: 10; /* Placer en dessous de la première ligne */
border-bottom: 2px solid #aaa;
}
/* Sticky First Column */
td:first-child, th:first-child {
position: sticky;
left: 0;
background-color: white;
z-index: 30; /* Priorité plus élevée pour la première colonne */
width: 120px; /* Fixe la largeur de la première colonne */
min-width: 120px;
border-right: none;
}
/* Bordure pour la première colonne */
td:first-child::after, th:first-child::after {
content: "";
position: absolute;
right: 0;
top: 0;
width: 2px;
height: 100%;
background-color: #ccc;
z-index: 35;
}
/* 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: 25; /* Assurez-vous que la deuxième colonne reste derrière la première */
width: 120px; /* Fixe la largeur de la deuxième colonne */
min-width: 120px;
border-right: 1px solid #ccc; /* Ajoute une bordure à droite */
}
/* Bordure pour la deuxième colonne */
td:nth-child(2)::after, th:nth-child(2)::after {
content: "";
position: absolute;
right: 0;
top: 0;
width: 2px;
height: 100%;
background-color: #ccc;
z-index: 30; /* Placer au-dessus du contenu */
}
/* Sticky Third Column */
td:nth-child(3), th:nth-child(3) {
position: sticky;
left: 240px; /* Décale la troisième colonne après la deuxième (120px pour la première + 120px pour la deuxième) */
background-color: white;
z-index: 20; /* Priorité plus faible que la première et deuxième colonnes */
width: 120px; /* Fixe la largeur de la troisième colonne */
min-width: 120px;
border-right: 1px solid #ccc; /* Bordure entre la deuxième et la troisième colonne */
}
/* Bordure pour la troisième colonne */
td:nth-child(3)::after, th:nth-child(3)::after {
content: "";
position: absolute;
right: 0;
top: 0;
width: 2px;
height: 100%;
background-color: #ccc;
z-index: 35; /* Placer au-dessus du contenu */
}
/* Coin supérieur gauche sticky modifié pour intégrer la nouvelle colonne */
th:nth-child(3) {
position: sticky;
top: 0;
left: 240px; /* Décale la troisième colonne après la première et la deuxième */
z-index: 45; /* Priorité maximale pour éviter le recouvrement */
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>