【问题标题】:Adjust Table Footer Height调整表格页脚高度
【发布时间】:2020-01-09 02:53:35
【问题描述】:

我很难调整表格页脚 (tfoot) 的高度。

@font-face {
  font-family: Poppins;
  src: url(./fonts/poppin/Poppins-Light.otf);
}
body{
  font-family: Poppins,arial;
  font-size: 14px;
  line-height: 1.2em;
}
.dataview{
  width: auto;
  margin: 0 auto;
  table-layout: fixed;
  border-collapse: collapse;
  border: 1px solid black;
}
.dataview thead{
  background-color: cornflowerblue;
  color: yellow;
  font-weight: bold;
}
.dataview thead tr{
  display: block;
  position: relative;
}
.dataview tbody tr:nth-last-child(1){
  border-bottom: none;
}
.dataview tbody{
  display: block;
  overflow-y: scroll;
  overflow-x: hidden;
  width: 100%;
  background-color: #f2f2f2;
}
.dataview th, .dataview td{
  padding: 5px;
  text-align: left;
}
.dataview th{
  height: 30px;
}
.dataview tr{
  border-bottom: 1px solid black
}
.dataview th.right, .dataview td.right{
  text-align:right;
}
.dataview th.center, .dataview td.center{
  text-align:center;
}
.dataview tbody tr:nth-child(even){
  background-color: #ccd8ff;
}
.dataview tbody tr:hover td{
  background-color: yellow;
}
.dataview tfoot td{  
  height: 20px;
  background-color: tomato;
  border-top: 1px solid black;
}
/* clidata-browse */
.dataview#clidata-browse{
  width: auto;
  box-sizing: border-box;
}
.dataview#clidata-browse td{
  height: 45px;
}
.dataview#clidata-browse th:nth-child(1),
.dataview#clidata-browse td:nth-child(1){
  min-width: 350px;
}
.dataview#clidata-browse th:nth-child(2),
.dataview#clidata-browse td:nth-child(2){
  min-width: 350px;
}
.dataview#clidata-browse th:nth-child(3),
.dataview#clidata-browse td:nth-child(3){
  min-width: 100px;
  padding-right:10px;
}
.dataview#clidata-browse tbody{
  height: 380px;
}
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Arrowleaf Sandbox</title>
  <meta name="description" content="The HTML5 Herald">
  <meta name="author" content="SitePoint">
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <table class="dataview" id="clidata-browse">
  <thead>
    <tr>
      <th>Customer Name</th>
      <th>Email Address</th>
      <th class="right">Balance</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        Customer 1<br>
        Customer Address
      </td>
      <td>testcust1@gmail.com</td>
      <td class="right">$3000.00</td>
    </tr>
    <tr>
      <td>
        Customer 2<br>
        Customer Address
      </td>
      <td>testcust2@gmail.com</td>
      <td class="right">$40.00</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>This is a test</td>
    </tr>
  </tfoot>
</table>  
</body>
</html>

尝试调整 css 中 tfoot 标签的高度(包含在 fiddle 中)。我可以毫无问题地增加行高,但如果我希望 tfoot 小于 tbody 行的大小,它不会变小。任何帮助表示赞赏。

My Fiddle

【问题讨论】:

  • 使用最大高度代替高度,一旦达到最大高度就会滚动jsfiddle.net/5naxopjd
  • @SunnyPatel 一个小时前我提出了这个问题,我急于选择答案吗?
  • @SunnyPatel,你做到了!非常好!谢谢。

标签: html css


【解决方案1】:

根本原因

这很简单,与 &lt;tfoot/&gt; 的 2 个竞争 CSS 选择器有关:

.dataview tfoot td{  
  height: 20px;
  background-color: tomato;
  border-top: 1px solid black;
}

.dataview#clidata-browse td{
  height: 45px;
}

我通过检查tfoot td 标签发现了这一点:

说明

后者有更高的CSS Specificity,因为它包含ID选择器#clidata-browse

解决方案

要解决这个问题,您可以做几件事,最简单的方法是将!important 标签添加到.dataview tfoot td 高度CSS 选择器。

.dataview tfoot td {
  height: 20px !important;
  background-color: tomato;
  border-top: 1px solid black;
}

或者,您可以将第二个选择器调整为以下内容:

.dataview#clidata-browse thead td,
.dataview#clidata-browse tbody td {
  height: 45px;
}

避免选择页脚&lt;td/&gt; 元素来改变高度。

【讨论】:

  • important 标志很有用,但会使 css 变得更加复杂,难以在以后进行推理(尤其是在大型项目中或当有多个编辑器时)。我建议避免使用它,而是修改你的 css 以便它适当地级联。见w3.org/TR/CSS2/cascade.html#important-rules。不要使用 !important,而是考虑添加另一个规则:.dataview#clidata-browse tfoot td{height: 20px} 或修改现有规则 .dataview tfoot td, .dataview#clidata-browse tfoot td{height: 20px}
  • @Tai,这是一个重要的(哈哈)经验法则。但是,在确保他的表格页脚高度分开的情况下,这个用例使它变得合适。还有其他解决方案,比如在他的tfoot 选择器中添加一个id(不可扩展),或者为内联样式添加高度(也不可扩展),甚至调整他的第二个选择器以指定tbody 仅限儿童.dataview#clidata-browse tbody td (替代解决方案,可扩展)。
  • 我选择添加第二个选择器而不是放置 !important 标志。我也不喜欢使用它们。
【解决方案2】:

tfoot tag 支持styles atributes,您可以使用它来提高它的高度。 例如&lt;tfoot styles="height:500px"

【讨论】:

    【解决方案3】:

    如果你使用display:block 来让你的body 滚动,那么你也可以使用flexgrid 来重置整个布局。然后可以在桌子本身上设置高度。

    需要滚动而不是滚动的示例(添加边框以显示单元格的位置)

    table {
      height: 300px;
      display: flex;
      flex-direction: column;
      text-align: left;
    }
    
    thead {
      background: turquoise;
      padding-right: 1.1em;
    }
    
    thead tr,
    tbody tr {
      display: grid;
      grid-template-columns: 1fr 1fr 0.5fr;/* set column width here via fr, fit-content(),minmax(),px ,% or any value you that fits your needs best */
    }
    
    th,
    td {
      padding: 0.25em;
      margin: 3px;
      border: 1px solid;
      display: block;
    }
    
    tbody {
      overflow-y: scroll;
      width: 100%;
    }
    
    tfoot {
      background: tomato;
      flex: 1;
      display: flex;
    }
    
    tfoot tr {
      flex: 1;
      margin: 3px;
      border: solid 1px;
      display: flex;
    }
    
    tfoot tr td {
      margin: auto;
      border: none;
    }
    <table class="dataview" id="clidata-browse">
      <thead>
        <tr>
          <th>Customer Name</th>
          <th>Email Address</th>
          <th class="right">Balance</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="3" rowspan="30">This is a test</td>
        </tr>
      </tfoot>
    </table>
    <hr>
    <table class="dataview" id="clidata-browse">
      <thead>
        <tr>
          <th>Customer Name</th>
          <th>Email Address</th>
          <th class="right">Balance</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
        <tr>
          <td>
            Customer 1<br> Customer Address
          </td>
          <td>testcust1@gmail.com</td>
          <td class="right">$3000.00</td>
        </tr>
        <tr>
          <td>
            Customer 2<br> Customer Address
          </td>
          <td>testcust2@gmail.com</td>
          <td class="right">$40.00</td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td colspan="3" rowspan="30">This is a test</td>
        </tr>
      </tfoot>
    </table>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-09
      • 2014-05-26
      • 1970-01-01
      • 2012-04-22
      • 2021-10-14
      • 1970-01-01
      • 2011-05-10
      • 1970-01-01
      相关资源
      最近更新 更多