html - 使用 css 固定表格前两列的位置

标签 html css

我有一个动态生成的表格,表格列的数字可能很大,因此用户需要水平滚动才能看到所有列的数据。我的任务是,当用户想要查看其他列的数据时,我必须固定前 2 列。

enter image description here

到目前为止,我可以对前 2 列进行绝对定位,但其他列的定位不正确。我的最后输出如下: enter image description here

我的代码如下:

HTML

<table id="teamTable" class="table data-table table-hover" ts-wrapper>
    <thead>
       <tr>
          <th class="table-col-xs table-col-border" rowspan="2">
              <input type="checkbox">
           </th>
           <th class="table-col-border" ts-default rowspan="2">
                Name
           </th>
           <th ng-repeat="" class="table-col-sm table-col-border text-center table-th-removable">
               column 3
           </th>
           <th ng-repeat="" class="table-col-sm table-col-border text-center table-th-removable">
               column 4
           </th>
           ................................   
      </tr>
       <tr>
           <th ng-repeat="" class="table-col-sm table-col-border text-center">
              column 3
           </th>
           <th ng-repeat="" class="table-col-sm table-col-border text-center">
              column 4
           </th>
            ..............................
       </tr>
    </thead>

     <tbody>
       <tr ng-repeat="" ts-repeat>
          <td class="table-col-xs table-col-border" rowspan="2">
              <input type="checkbox">
           </td>
           <td class="table-col-border" ts-default rowspan="2">
                User  Name
           </td>
           <td ng-repeat="" class="table-col-sm table-col-border text-center table-th-removable">
               column 3
           </td>
           <td ng-repeat="" class="table-col-sm table-col-border text-center table-th-removable">
               column 4
           </td>
           ................................   
      </tr>
    </tbody>
 </table>

CSS

/* first make some space using 'margin-left' */
table#teamTable {
   margin-left: 125px;
   margin-bottom: 20px;
}

/* first table heading */
table#teamTable > thead > tr:first-child > th:nth-child(1) {
   margin-left: -126px;
   position: absolute;
   z-index: 9999;
   overflow: hidden;
   background-color: #fff;
   height: 45%;
} 

/* second table heading */
table#teamTable > thead > tr:first-child  > th:nth-child(2) {
   position: absolute;
   margin-left: -76px;
   z-index: 9999;
   overflow: hidden;
   background-color: #fff;
   height: 45%;
   width: 75px;
} 

/* rest of the table heading */ 
table#teamTable > thead > tr:first-child  > th:nth-child(n+3) {
   position: relative;
} 

table#teamTable > thead > tr:first-child  > th  {
   position: relative;
} 

/* first table column */
table#teamTable > tbody > tr > td:nth-child(1) {
   position: absolute;
   margin-left: -126px;
   z-index: 9999;
   overflow: hidden;
   background-color: #fff;
   height: 50px;
} 

 /* second table column */
table#teamTable > tbody > tr > td:nth-child(2) {
   position: absolute;
   margin-left: -76px;
   width: 75px;
   z-index: 9999;
   overflow: hidden;
   background-color: #fff;
   height: 50px;
} 

/* rest of the table column */
table#teamTable > tbody > tr > td:nth-child(n + 3) {
   position: relative; 
}

plnkr code

最佳答案

用预定义的大小设置 tbody 的样式并使用 overflow-y: scroll;

检查这个 fiddle 和逻辑http://jsfiddle.net/kabircse/bo8bu0u3/1/

你也可以使用 fixedheadertable 固定的表头

  http://www.fixedheadertable.com/

另一个固定的例子http://codepen.io/ajkochanowicz/pen/KHdih

关于html - 使用 css 固定表格前两列的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38443436/

相关文章:

html - 背景图像最小宽度和定位

php - 有一个数字 1 出现在我的 HTML 页面上,即使我没有把它放在那里

php - 如何从 mysql 数据中删除 <br> 以外的 html 标签?

javascript - 如何检查同一事件的多个元素

html - 将 header 修复到溢出 : auto; 的容器顶部

html - 如何阻止 div 换行?

html - CSS 背景图像无法在视口(viewport)下方重复

html - 在没有空间的情况下将图像居中对齐

html - 图像在页面加载时向左移动

html - 将 div 置于页面中心,而不管其包含的元素如何