具有自适应列宽的可滚动固定标题html表

时间:2019-05-14 11:16:51

标签: css

我想用垂直滚动创建固定的html表,并用%定义列宽。以下是我建立的Codepen,但我无法达到预期的效果,因此机身总是夹在一起。请帮忙。

https://codepen.io/zh1611/pen/XwjmRo

.vtable {
  table-layout: fixed;
  width: 100%; /* must have this set */
}
.vtable tbody {
  display: block;
  width: 100%;
  max-height: 307px;
  overflow: auto;
}
.vtable thead th:first-child,
.vtable tbody tr td:first-child {
  width: 24%;
}
.vtable thead th:last-child,
.vtable tbody tr td:last-child {
  width: 16%;
}
.vtable th,
.vtable td {
  width: 10%;
}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

如果不需要IE-9或旧版浏览器,则可以尝试使用此方法,如果答案不是,则可以使用它:https://github.com/jmosbech/StickyTableHeaders

  

注意:此代码仅支持CSS粘性正确支持的浏览器

这里的代码:

/* PLotly.js 
–––––––––––––––––––––––––––––––––––––––––––––––––– */
/* plotly.js's modebar's z-index is 1001 by default
 * https://github.com/plotly/plotly.js/blob/7e4d8ab164258f6bd48be56589dacd9bdd7fded2/src/css/_modebar.scss#L5
 * In case a dropdown is above the graph, the dropdown's options
 * will be rendered below the modebar
 * Increase the select option's z-index
 */

/* This was actually not quite right -
   dropdowns were overlapping each other (edited October 26)

.Select {
    z-index: 1002;
}*/

/* Grid
–––––––––––––––––––––––––––––––––––––––––––––––––– */
.container {
  position: relative;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 0 20px;
  box-sizing: border-box;
}
.column,
.columns {
  width: 100%;
  float: left;
  box-sizing: border-box;
}

/* For devices larger than 400px */
@media (min-width: 400px) {
  .container {
    width: 85%;
    padding: 0;
  }
}

/* For devices larger than 550px */
@media (min-width: 550px) {
  .container {
    width: 80%;
  }
  .column,
  .columns {
    margin-left: 4%;
  }
  .column:first-child,
  .columns:first-child {
    margin-left: 0;
  }

  .one.column,
  .one.columns {
    width: 4.66666666667%;
  }
  .two.columns {
    width: 13.3333333333%;
  }
  .three.columns {
    width: 22%;
  }
  .four.columns {
    width: 30.6666666667%;
  }
  .five.columns {
    width: 39.3333333333%;
  }
  .six.columns {
    width: 48%;
  }
  .seven.columns {
    width: 56.6666666667%;
  }
  .eight.columns {
    width: 65.3333333333%;
  }
  .nine.columns {
    width: 74%;
  }
  .ten.columns {
    width: 82.6666666667%;
  }
  .eleven.columns {
    width: 91.3333333333%;
  }
  .twelve.columns {
    width: 100%;
    margin-left: 0;
  }

  .one-third.column {
    width: 30.6666666667%;
  }
  .two-thirds.column {
    width: 65.3333333333%;
  }

  .one-half.column {
    width: 48%;
  }

  /* Offsets */
  .offset-by-one.column,
  .offset-by-one.columns {
    margin-left: 8.66666666667%;
  }
  .offset-by-two.column,
  .offset-by-two.columns {
    margin-left: 17.3333333333%;
  }
  .offset-by-three.column,
  .offset-by-three.columns {
    margin-left: 26%;
  }
  .offset-by-four.column,
  .offset-by-four.columns {
    margin-left: 34.6666666667%;
  }
  .offset-by-five.column,
  .offset-by-five.columns {
    margin-left: 43.3333333333%;
  }
  .offset-by-six.column,
  .offset-by-six.columns {
    margin-left: 52%;
  }
  .offset-by-seven.column,
  .offset-by-seven.columns {
    margin-left: 60.6666666667%;
  }
  .offset-by-eight.column,
  .offset-by-eight.columns {
    margin-left: 69.3333333333%;
  }
  .offset-by-nine.column,
  .offset-by-nine.columns {
    margin-left: 78%;
  }
  .offset-by-ten.column,
  .offset-by-ten.columns {
    margin-left: 86.6666666667%;
  }
  .offset-by-eleven.column,
  .offset-by-eleven.columns {
    margin-left: 95.3333333333%;
  }

  .offset-by-one-third.column,
  .offset-by-one-third.columns {
    margin-left: 34.6666666667%;
  }
  .offset-by-two-thirds.column,
  .offset-by-two-thirds.columns {
    margin-left: 69.3333333333%;
  }

  .offset-by-one-half.column,
  .offset-by-one-half.columns {
    margin-left: 52%;
  }
}

/* Base Styles
–––––––––––––––––––––––––––––––––––––––––––––––––– */
/* NOTE
html is set to 62.5% so that all the REM measurements throughout Skeleton
are based on 10px sizing. So basically 1.5rem = 15px :) */
html {
  font-size: 62.5%;
}
body {
  font-size: 1.5em; /* currently ems cause chrome bug misinterpreting rems on body element */
  line-height: 1.6;
  font-weight: 400;
  font-family: "Open Sans", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial,
    sans-serif;
  color: rgb(50, 50, 50);
}

/* Typography
–––––––––––––––––––––––––––––––––––––––––––––––––– */
h1,
h2,
h3,
h4,
h5,
h6 {
  margin-top: 0;
  margin-bottom: 0;
  font-weight: 300;
}
h1 {
  font-size: 4.5rem;
  line-height: 1.2;
  letter-spacing: -0.1rem;
  margin-bottom: 2rem;
}
h2 {
  font-size: 3.6rem;
  line-height: 1.25;
  letter-spacing: -0.1rem;
  margin-bottom: 1.8rem;
  margin-top: 1.8rem;
}
h3 {
  font-size: 3rem;
  line-height: 1.3;
  letter-spacing: -0.1rem;
  margin-bottom: 1.5rem;
  margin-top: 1.5rem;
}
h4 {
  font-size: 2.6rem;
  line-height: 1.35;
  letter-spacing: -0.08rem;
  margin-bottom: 1.2rem;
  margin-top: 1.2rem;
}
h5 {
  font-size: 2.2rem;
  line-height: 1.5;
  letter-spacing: -0.05rem;
  margin-bottom: 0.6rem;
  margin-top: 0.6rem;
}
h6 {
  font-size: 2rem;
  line-height: 1.6;
  letter-spacing: 0;
  margin-bottom: 0.75rem;
  margin-top: 0.75rem;
}

.info_h6 {
  font-size: 1.6rem;
  line-height: 1.2;
  letter-spacing: 0;
  margin-bottom: 0.5rem;
  margin-top: 0.5rem;
}

p {
  margin-top: 0;
}

/* Blockquotes
–––––––––––––––––––––––––––––––––––––––––––––––––– */
blockquote {
  border-left: 4px lightgrey solid;
  padding-left: 1rem;
  margin-top: 2rem;
  margin-bottom: 2rem;
  margin-left: 0rem;
}

/* Links
–––––––––––––––––––––––––––––––––––––––––––––––––– */
a {
  color: #25383c;
  text-decoration: none;
  cursor: pointer;
}
a:hover {
  color: #2b60de;
  text-decoration: none;
}

/* Buttons
–––––––––––––––––––––––––––––––––––––––––––––––––– */
.button,
button,
input[type="submit"],
input[type="reset"],
input[type="button"] {
  display: inline-block;
  height: 38px;
  padding: 0 30px;
  color: #555;
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  line-height: 38px;
  letter-spacing: 0.1rem;
  text-transform: uppercase;
  text-decoration: none;
  white-space: nowrap;
  background-color: transparent;
  border-radius: 4px;
  border: 1px solid #bbb;
  cursor: pointer;
  box-sizing: border-box;
}
.button:hover,
button:hover,
input[type="submit"]:hover,
input[type="reset"]:hover,
input[type="button"]:hover,
.button:focus,
button:focus,
input[type="submit"]:focus,
input[type="reset"]:focus,
input[type="button"]:focus {
  color: #333;
  border-color: #888;
  outline: 0;
}
.button.button-primary,
button.button-primary,
input[type="submit"].button-primary,
input[type="reset"].button-primary,
input[type="button"].button-primary {
  color: #fff;
  background-color: #33c3f0;
  border-color: #33c3f0;
}
.button.button-primary:hover,
button.button-primary:hover,
input[type="submit"].button-primary:hover,
input[type="reset"].button-primary:hover,
input[type="button"].button-primary:hover,
.button.button-primary:focus,
button.button-primary:focus,
input[type="submit"].button-primary:focus,
input[type="reset"].button-primary:focus,
input[type="button"].button-primary:focus {
  color: #fff;
  background-color: #1eaedb;
  border-color: #1eaedb;
}

.trend-button {
  color: #25383c;
  cursor: pointer;
}
.trend-button:hover {
  color: #2b60de;
}

/* Forms
–––––––––––––––––––––––––––––––––––––––––––––––––– */
input[type="email"],
input[type="number"],
input[type="search"],
input[type="text"],
input[type="tel"],
input[type="url"],
input[type="password"],
textarea,
select {
  height: 38px;
  padding: 6px 10px; /* The 6px vertically centers text on FF, ignored by Webkit */
  background-color: #fff;
  border: 1px solid #d1d1d1;
  border-radius: 4px;
  box-shadow: none;
  box-sizing: border-box;
  font-family: inherit;
  font-size: inherit; /*https://stackoverflow.com/questions/6080413/why-doesnt-input-inherit-the-font-from-body*/
}
/* Removes awkward default styles on some inputs for iOS */
input[type="email"],
input[type="number"],
input[type="search"],
input[type="text"],
input[type="tel"],
input[type="url"],
input[type="password"],
textarea {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}
textarea {
  min-height: 65px;
  padding-top: 6px;
  padding-bottom: 6px;
}
input[type="email"]:focus,
input[type="number"]:focus,
input[type="search"]:focus,
input[type="text"]:focus,
input[type="tel"]:focus,
input[type="url"]:focus,
input[type="password"]:focus,
textarea:focus,
select:focus {
  border: 1px solid #33c3f0;
  outline: 0;
}
label,
legend {
  display: block;
  margin-bottom: 0px;
}
fieldset {
  padding: 0;
  border-width: 0;
}
input[type="checkbox"],
input[type="radio"] {
  display: inline;
}
label > .label-body {
  display: inline-block;
  margin-left: 0.5rem;
  font-weight: normal;
}

/* Lists
–––––––––––––––––––––––––––––––––––––––––––––––––– */
ul {
  list-style: circle inside;
}
ol {
  list-style: decimal inside;
}
ol,
ul {
  padding-left: 0;
  margin-top: 0;
}
ul ul,
ul ol,
ol ol,
ol ul {
  margin: 1.5rem 0 1.5rem 3rem;
  font-size: 90%;
}
li {
  margin-bottom: 1rem;
}

/* Spacing
–––––––––––––––––––––––––––––––––––––––––––––––––– */
button,
.button {
  margin-bottom: 0rem;
}
input,
textarea,
select,
fieldset {
  margin-bottom: 0rem;
}
pre,
dl,
figure,
table,
form {
  margin-bottom: 0rem;
}
p,
ul,
ol {
  margin-bottom: 0.75rem;
}

/* Utilities
–––––––––––––––––––––––––––––––––––––––––––––––––– */
.u-full-width {
  width: 100%;
  box-sizing: border-box;
}
.u-max-full-width {
  max-width: 100%;
  box-sizing: border-box;
}
.u-pull-right {
  float: right;
}
.u-pull-left {
  float: left;
}

/* Misc
–––––––––––––––––––––––––––––––––––––––––––––––––– */
hr {
  margin-top: 1rem;
  margin-bottom: 1rem;
  border-width: 0;
  border-top: 1px solid #e1e1e1;
}

hr.style1 {
  border-top: 1px solid #8c8b8b;
}

hr.style2 {
  border-top: 3px double #8c8b8b;
}

hr.style3 {
  border-top: 1px dashed #8c8b8b;
}

hr.style4 {
  border-top: 1px dotted #8c8b8b;
}

hr.style5 {
  background-color: #fff;
  border-top: 2px dashed #8c8b8b;
}

hr.style6 {
  background-color: #fff;
  border-top: 2px dotted #8c8b8b;
}

hr.style7 {
  border-top: 1px solid #8c8b8b;
  border-bottom: 1px solid #fff;
}

hr.style8 {
  border-top: 1px solid #8c8b8b;
  border-bottom: 1px solid #fff;
}
hr.style8:after {
  content: "";
  display: block;
  margin-top: 2px;
  border-top: 1px solid #8c8b8b;
  border-bottom: 1px solid #fff;
}

hr.style9 {
  border-top: 1px dashed #8c8b8b;
  border-bottom: 1px dashed #fff;
}

hr.style10 {
  border-top: 1px dotted #8c8b8b;
  border-bottom: 1px dotted #fff;
}

/* Clearing
–––––––––––––––––––––––––––––––––––––––––––––––––– */

/* Self Clearing Goodness */
.container:after,
.row:after,
.u-cf {
  content: "";
  display: table;
  clear: both;
}

/* Media Queries
–––––––––––––––––––––––––––––––––––––––––––––––––– */
/*
Note: The best way to structure the use of media queries is to create the queries
near the relevant code. For example, if you wanted to change the styles for buttons
on small devices, paste the mobile query code up in the buttons section and style it
there.
*/

/* Larger than mobile */
@media (min-width: 400px) {
}

/* Larger than phablet (also point when grid becomes active) */
@media (min-width: 550px) {
}

/* Larger than tablet */
@media (min-width: 750px) {
}

/* Larger than desktop */
@media (min-width: 1000px) {
}

/* Larger than Desktop HD */
@media (min-width: 1200px) {
}

/* dash specific */
._dash-undo-redo {
  display: none;
}

/* fake link using h5 font */
.summary {
  color: #1d2951;
  text-decoration: none;
  font-size: 2.2rem;
  line-height: 1.5;
  letter-spacing: -0.05rem;
  margin-bottom: 0.6rem;
  margin-top: 0.6rem;
  cursor: pointer;
}

.summary:hover {
  color: #0038a8;
  text-decoration: none;
  cursor: pointer;
}

.summary:focus {
  color: #1d2951;
  text-decoration: none;
  cursor: pointer;
}

.summary:visited {
  color: #0038a8;
  text-decoration: none;
  cursor: pointer;
}

/*.summary:hover + .detail,
.detail:hover {
  display: block;
}*/

.show.detail {
  display: block;
}

.detail {
  display: none;
}

.summary-noclick {
  color: #1d2951;
  text-decoration: none;
  font-size: 2.2rem;
  line-height: 1.5;
  letter-spacing: -0.05rem;
  margin-bottom: 0.6rem;
  margin-top: 0.6rem;
}

#target_par {
  width: 350px;
}


/* Tables
–––––––––––––––––––––––––––––––––––––––––––––––––– */
.mtable {
  width: 100%;
  position: relative;
  overflow: hidden;
  display: inline-block;
}
.mtable thead {
  width: 100%;
  display: inline-block;
  position: relative;
  overflow: visible;
}
.mtable thead th:first-child {
  position: relative;
  display: inline-block;
  min-width: 350px;
  background-color: #fff;
}
.mtable tbody {
  width: 100%;
  position: relative;
  display: block;
  max-height: 307px;
  overflow: scroll;
}
.mtable tbody tr td:first-child {
  position: relative;
  display: block;
  min-width: 350px;
  background-color: #fff;
}
.mtable th,
.mtable td {
  min-width: 180px;
  max-width: 180px;
  padding: 2px 3px;
  text-align: left;
  text-overflow: ellipsis;
  border: 1px solid #e1e1e1;
}

.table-wrap {
  max-height: 320px;
  overflow: auto;
}

.vtable {
  table-layout: fixed;
  width: 100%; /* must have this set */
}

.vtable thead tr th {
  position: sticky;
  top: 0;
  background: #fff;
}
.vtable thead th:first-child,
.vtable tbody tr td:first-child {
  width: 24%;
}
.vtable thead th:last-child,
.vtable tbody tr td:last-child {
  width: 16%;
}
.vtable th,
.vtable td {
  width: 10%;
}
<div class='row ten columns offset-by-one table-wrap'>
  <table class='vtable' id='vtable'>
      <thead>
          <tr>
              <th>Column 1</th>
              <th>Column 2</th>
              <th>Column 3</th>
              <th>Column 4</th>
              <th>Column 5</th>
              <th>Column 6</th>
              <th>Column 7</th>
              <th>Column 8</th>
          </tr>
      </thead>
      <tbody>
          <tr>
              <td>Row 1</td>
              <td>Row 1</td>
              <td>Row 1</td>
              <td>Row 1</td>
              <td>Row 1</td>
              <td>Row 1</td>
              <td>Row 1</td>
              <td>Row 1</td>
          </tr>
          <tr>
              <td>Row 2</td>
              <td>Row 2</td>
              <td>Row 2</td>
              <td>Row 2</td>
              <td>Row 2</td>
              <td>Row 2</td>
              <td>Row 2</td>
              <td>Row 2</td>
          </tr>
          <tr>
              <td>Row 3</td>
              <td>Row 3</td>
              <td>Row 3</td>
              <td>Row 3</td>
              <td>Row 3</td>
              <td>Row 3</td>
              <td>Row 3</td>
              <td>Row 3</td>
          </tr>
          <tr>
              <td>Row 4</td>
              <td>Row 4</td>
              <td>Row 4</td>
              <td>Row 4</td>
              <td>Row 4</td>
              <td>Row 4</td>
              <td>Row 4</td>
              <td>Row 4</td>
          </tr>
          <tr>
              <td>Row 5</td>
              <td>Row 5</td>
              <td>Row 5</td>
              <td>Row 5</td>
              <td>Row 5</td>
              <td>Row 5</td>
              <td>Row 5</td>
              <td>Row 5</td>
          </tr>
          <tr>
              <td>Row 6</td>
              <td>Row 6</td>
              <td>Row 6</td>
              <td>Row 6</td>
              <td>Row 6</td>
              <td>Row 6</td>
              <td>Row 6</td>
              <td>Row 6</td>
          </tr>
          <tr>
              <td>Row 7</td>
              <td>Row 7</td>
              <td>Row 7</td>
              <td>Row 7</td>
              <td>Row 7</td>
              <td>Row 7</td>
              <td>Row 7</td>
              <td>Row 7</td>
          </tr>
          <tr>
              <td>Row 8</td>
              <td>Row 8</td>
              <td>Row 8</td>
              <td>Row 8</td>
              <td>Row 8</td>
              <td>Row 8</td>
              <td>Row 8</td>
              <td>Row 8</td>
          </tr>
          <tr>
              <td>Row 9</td>
              <td>Row 9</td>
              <td>Row 9</td>
              <td>Row 9</td>
              <td>Row 9</td>
              <td>Row 9</td>
              <td>Row 9</td>
              <td>Row 9</td>
          </tr>
          <tr>
              <td>Row 10</td>
              <td>Row 10</td>
              <td>Row 10</td>
              <td>Row 10</td>
              <td>Row 10</td>
              <td>Row 10</td>
              <td>Row 10</td>
              <td>Row 10</td>
          </tr>
          <tr>
              <td>Row 11</td>
              <td>Row 11</td>
              <td>Row 11</td>
              <td>Row 11</td>
              <td>Row 11</td>
              <td>Row 11</td>
              <td>Row 11</td>
              <td>Row 10</td>
          </tr>
      </tbody>
  </table>
</div>