使用rowspan和colspan属性修复表头并可滚动

时间:2015-12-28 05:24:49

标签: javascript html css

我希望将表头修复到一个位置,并允许表的其余内容可滚动(下面的代码)。我在表标题行中使用colspan和rowsapn属性。有没有办法解决它?

<html>
   <body> 
   <div id="secondtable" style="width:100%; height:380px; overflow:auto;">
   <table class="mytable" style="width:100%; border-width:thin;" border="1"      cellpadding="9" cellspacing="0" align="center">    
                <tr class="tt" bgcolor="#0B2B62">
                    <th>No.</th>
                    <th>Store Name</th>
                    <th>Receipts</th>
                    <th colspan=2>Receipts vs Budget</th>
                    <th colspan=3>Receipts vs Last Year</th>
                    <th>Contribution</th>
                    <th colspan=2>Contribution vs Budget</th>
                </tr>
                <tr class="tt" style="background-color:#C2C2C2;">
                    <td></td>
                    <td></td>
                    <td></td>
                    <td><font color="black">Var &pound;</font></td>
                    <td><font color="black">Var %</td>
                    <td><font color="black">Var &pound;</font></td>
                    <td><font color="black">Var %</font></td>
                    <td><font color="black">Var</font></td>
                    <td></td>
                    <td><font color="black">Var &pound;</font></td>
                    <td><font color="black">Var %</font></td>
                </tr>
                <tr class="tt" style="background-color:#D7F2FF">
                    <td>&nbsp;</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
        </table>
</div>
</body>
</html>

//这是css

table {

border-collapse:separate;
border-spacing: 0;
color:white;
background-color:white;
border: 1px solid black;
border-radius: 8px;
-moz-border-radius: 5px;
padding: 5px;
font-family:sans-serif;
}

.mytable{
background-color:#014483;
}

.tt{
border-color:#2B4F81;
}
.ttt{
background-color:#BBDBF4;
border:#2B4F81;
}

5 个答案:

答案 0 :(得分:2)

你可以使用它可以帮助你

.mytable tr th
{
table-layout: fixed;
}

答案 1 :(得分:1)

您可能需要将标题放在带有.sticky类的div中,或者您喜欢的任何内容以及正确的CSS。

<强> HTML

  <div class="sticky">
  <table class="mytable" style="width:100%; border-width:thin;" border="1" cellpadding="9" cellspacing="0" align="center">
      <tr class="tt" bgcolor="#0B2B62">
        <th>No.</th>
        <th>Store Name</th>
        <th>Receipts</th>
        <th colspan=2>Receipts vs Budget</th>
        <th colspan=3>Receipts vs Last Year</th>
        <th>Contribution</th>
        <th colspan=2>Contribution vs Budget</th>
      </tr>
  </table>
  </div>

<强> CSS

.sticky {
  position: fixed;
  top: 2px;
  width: 96.5%;
}

请参阅此处的示例:http://codepen.io/omerblink/pen/wMzLow?editors=110

答案 2 :(得分:0)

这可能会对您有所帮助:

html, body{
  margin:0;
  padding:0;
  height:100%;
}
section {
  position: relative;
  border: 1px solid #000;
  padding-top: 37px;
  background: #500;
}
section.positioned {
  position: absolute;
  top:100px;
  left:100px;
  width:800px;
  box-shadow: 0 0 15px #333;
}
.container {
  overflow-y: auto;
  height: 200px;
}
table {
  border-spacing: 0;
  width:100%;
}
td + td {
  border-left:1px solid #eee;
}
td, th {
  border-bottom:1px solid #eee;
  background: #ddd;
  color: #000;
  padding: 10px 25px;
}
th {
  height: 0;
  line-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  color: transparent;
  border: none;
  white-space: nowrap;
}
th div{
  position: absolute;
  background: transparent;
  color: #fff;
  padding: 9px 25px;
  top: 0;
  margin-left: -25px;
  line-height: normal;
  border-left: 1px solid #800;
}
th:first-child div{
  border: none;
}

答案 3 :(得分:0)

我怀疑你能用一张桌子来实现这个目标。所以我实际上创建了两个表格,一个用于标题,另一个用于body和amp;前者总是固定的。我还假设列数是静态的。这是代码

<!--Header part-->    
<div id="firsttable" style="width:100%;">
        <table class="mytable" style="width:100%; border-width:thin;" border="1" cellpadding="9" cellspacing="0" align="center">
          <thead><tr class="tt " bgcolor="#0B2B62">

            <th id="no">No.</th>
            <th id="sto">Store Name</th>
            <th id="res">Receipts</th>
            <th colspan=2 id="rb">Receipts vs Budget</th>
            <th colspan=3 id="rl">Receipts vs Last Year</th>
            <th id="con">Contribution</th>
            <th colspan=2 id="cb">Contribution vs Budget</th>
          </tr>
          </thead>
          </table>
          </div>

<!-- body part -->

    <div id="secondtable" style="width:100%; height:380px; overflow:auto;">
              <table class="mytable" style="width:100%; border-width:thin;" border="1" cellpadding="9" cellspacing="0" align="center">

              <tr class="tt" style="background-color:#C2C2C2;">
                <td headers="no">A</td>
                <td headers="sto">B</td>
                <td headers="res">C</td>
                <td colspan="2" headers="rb"><div style="color:black">Var &pound;</div><div style="color:black">Var %</div></td>
                <td colspan="3"><div style="color:black">Var &pound;</div><div>Var %</div><div>Var</div></td>
                <td>D</td>
                <td colspan="2"><div style ="color:black">Var &pound;</div><div style="color:black">Var %</div></td>
              </tr>

              <!-- Include multiple tr to get scrollbar -->
            </table>
          </div>

CSS

table {
border-collapse:separate;
border-spacing: 0;
color:white;
background-color:white;
border: 1px solid black;
border-radius: 8px;
-moz-border-radius: 5px;
padding: 5px;
font-family:sans-serif;
}

.mytable{
background-color:#014483;
}

.tt{
border-color:#2B4F81;
}
.ttt{
background-color:#BBDBF4;
border:#2B4F81;
}

td,th{
    max-width:10px;

    }
.mytable div{
    max-width:40%;
    display:inline
    }

WORKING COPY

注意:当此滚动条可见时,您可能会看到标题列与主体列不同步。这是因为滚动条有一个宽度,它将占用一些空间。在这种情况下,您可以调整任何主体列的宽度,使其同步。另外我理解基本上你需要7列可以有子列。所以在正文中我创建了7列和div来复制子列

希望这会有所帮助

答案 4 :(得分:0)

3岁的问题,但是对于仍然对此没有解决方案的人(带行距的表头),我终于有了使用js的方法。

<div id="table">
 <table>
   <thead>
     <!-- your head code -->
   </thead>
   <tbody>
     <!-- your body code -->
   </body>
 </table>
</div>

CSS

#table{
    height:200px; //for example 
    overflow-y:auto; 
}

JS

let statusCard = document.querySelector('#table');
    // add scroll event listener for change head's position 
    statusCard.addEventListener('scroll',e =>{
      let tableHead = document.querySelector('thead');
      let scrollTop =  statusCard.scrollTop;
      tableHead.style.transform = 'translateY(' + scrollTop + 'px)';
})