[CSS]Table scrollable version CSS

NoSmoking

Table scrollable verticalement (à partir de IE 10)

Structure HTML de base

<table class="table-scroll">
  <thead>
    <tr>
      <td></td> <td></td> <td></td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td> <td></td> <td></td>
    </tr>
    <!-- la suite des données de la table -->
  </tbody>
  <tfoot>
    <tr>
      <td colspan="5"></td>
    </tr>
  </tfoot>
</table>

Le rendu

PrénomNomDateEntrepriseFonction
PatriciaRay05/01/1981GeveeMechanical Systems Engineer
JamesThomas01/30/1978SkynoodleNurse Practicioner
GeraldHawkins01/22/1981VoonixNurse Practicioner
GaryHamilton08/19/1981ZoomboxAdministrative Officer
SarahFernandez04/13/1974PixobooPayment Adjustment Coordinator
BrendaCarpenter07/01/1978AvaveoStaff Accountant II
JoanAlvarez06/25/1966ZazioAssociate Professor
WandaHanson11/04/1984BrightdogCompensation Analyst
MartinTaylor02/17/1969YouspanEngineer III
BeverlyStanley11/25/1970DabtypeAssistant Media Planner
ChristophCasbourne9/13/1957Tejon Ranch CoServices
TrudaBertelmot11/11/1985Clipper Realty Inc.Accounting
DallonGoulding3/16/1977Goldman Sachs Group, Inc. (The)Research and Development
YoshikoDanielsky7/10/1997FB Financial CorporationBusiness Development
BerteDmitrienko11/17/1995Argan, Inc.Marketing
ValentijnMeadmore8/2/1993MGM Resorts InternationalHuman Resources
SuzyFlewett10/25/1980Ceragon Networks Ltd.Research and Development
MordecaiJerrams10/13/1965Silicom LtdTraining
ClausSamwyse11/25/1974Verizon Communications Inc.Accounting
RafaeliaPedrol4/18/1975Schneider National, Inc.Research and Development
LitaLisimore6/8/1988Simmons First National CorporationProduct Management
NoakMcPeice12/6/1974Eyegate Pharmaceuticals, Inc.Product Management
CraggyPortugal12/18/1989Roper Technologies, Inc.Sales
MadelleBartoszinski12/20/1958Honeywell International Inc.Legal
FrasquitoHarriagn9/24/1998ASB Bancorp, Inc.Accounting
BetteannFreegard5/5/1954Brookfield Property Partners L.P.Marketing
JacindaSheepy10/22/1989Landmark Infrastructure Partners LPEngineering
SigismundArnholtz9/16/1965Ellington Residential Mortgage REITResearch and Development
MyrleCromack5/7/1951First Cash, Inc.Research and Development
ZorinaMomford11/1/1961Cimpress N.VMarketing
Ceci n'est qu'un <tfoot>.

Le CSS pour le scroll

Il est important de fixer une min-width et une max-width aux <td> pour inhiber leur comportement naturel au dimensionnement.

/*========================*/
/* pour l'effet de scroll */
/*========================*/
.table-scroll thead > tr,
.table-scroll tfoot > tr,
.table-scroll tbody {
  display: block;
}
.table-scroll tbody {
  height: 11.5em;
  overflow: auto;
  overflow-x: hidden;
}
/* définitions des largeurs */
.table-scroll td:nth-child(1) ,
.table-scroll td:nth-child(2) ,
.table-scroll td:nth-child(4) {
  max-width: 6em;
  min-width: 6em;
}
.table-scroll td:nth-child(3) {
  max-width: 8em;
  min-width: 8em;
}
.table-scroll td:nth-child(5) {
  width: 20em;
}
.table-scroll td:last-child {
  padding-right: 1.5em;       /* de la place pour la scrollBar */
  border-right: 0;
}

Le CSS cosmétique

Le CSS suivant est juste là pour « styler » la <table>.

.table-scroll {
  border: 1px solid #888;
  border-collapse: separate;
  border-spacing: 0;
  empty-cells: hide;
  font: .8em Verdana,sans-serif;
  background-color: #EFEFEF;
}
.table-scroll td {
  padding: .5em;
  border-right: 1px solid #888;
  white-space: nowrap;
  word-break: normal;
}
.table-scroll td +td {
  border-left: 1px solid #FFF;
}
/* fond pour la ligne d'entête */
.table-scroll thead tr {
  background: linear-gradient(to bottom, transparent, rgba(0, 0, 0, 0.2) )repeat scroll 0% 0% #EEE;
}
.table-scroll thead td {
  border-top: 1px solid #FFF;
  text-shadow: 1px 1px 0 #FFF;
}
.table-scroll tbody {
  border: 1px solid #888;
  border-width: 1px 0;
  background-color: #FFF;     /* annule couleur de fond de la TABLE */
}
.table-scroll tbody td {
  border-top: 1px solid #CCC;
}
/* couleur sur le :hover */
.table-scroll tbody tr:hover,
.table-scroll tbody td:hover {
  background-color: #EFEFFF;
  cursor: pointer;
}