水平滚动但具有固定的第一列的表

时间:2017-05-30 05:25:54

标签: html css

到目前为止,我有一个响应表,如果窗口像jsfiddle那样小,它会水平滚动。我现在需要的是能够修复第一列,只有其余列滚动。我能得到的只是this。第一列现在重叠给我,如果我能让它向左移动会很好但是由于某种原因我无法做到。请HELPPPPP。 :)

html

2 个答案:

答案 0 :(得分:1)

检查此代码。我已经为固定列创建了一个类,并为第二个孩子提供了填充。 Demo

body {
    color: white;
}

.search-table-outter {
    width: auto; 
    max-width: 735px;
    margin-left: 5em;
    margin-right: auto;
    padding-top: 30px;
}
.search-table{
    table-layout: fixed; 
    margin:0px auto 0px auto; 
    background-color: lightblue
}
.search-table, td, th{
    border-collapse:collapse; 
    border-bottom:1px solid white;
    line-height: 10px;
}
th{
    padding:5px 10px; 
    font-size:15px; 
}
td{
    padding:5px 10px; 
    height:35px;
}
.search-table-outter { overflow-x: scroll; }
th, td { min-width: 100px; text-align: center; }

.fixed {
    position: absolute; 
    left: auto; 
    top: auto;
    background-color: lightblue;
    margin: 1px;
}

.search-table tr th:nth-child(2), .search-table tr td:nth-child(2) {
    padding-left: 150px;
}
<div class="container header">
<div class="search-table-outter wrapper">
  <table class="search-table inner">
    <tr>
      <th class="fixed">Time</th>
      <th>Icon</th>
      <th>Description</th>
      <th>Temp</th>
      <th>Precip</th>
      <th>Wind</th>
    </tr>
    <tr>
      <td class="fixed">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="fixed">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="fixed">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="fixed">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="fixed">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="fixed">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="fixed">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="fixed">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
  </table>
</div>
</div>

答案 1 :(得分:0)

可能这就是你要找的东西

你需要将填充设置为第二个td或第二个

.search-table tr th:nth-child(2), .search-table tr td:nth-child(2) {
    padding-left: 100px;
}

&#13;
&#13;
body {
    color: white;
}

.search-table-outter {
    width: auto; 
    max-width: 735px;
    margin-left: 5em;
    margin-right: auto;
    padding-top: 30px;
}
.search-table{
    table-layout: fixed; 
    margin:0px auto 0px auto; 
    background-color: lightblue
}
.search-table, td, th{
    border-collapse:collapse; 
    border-bottom:1px solid white;
    line-height: 10px;
}
th{
    padding:5px 7px; 
    font-size:15px; 
}
td{
    padding:5px 10px; 
    height:35px;
}
.search-table-outter { overflow-x: scroll; }
th, td { min-width: 100px; }

.headcol {
    position: absolute;
    color: black;
    left: auto;
    width: 5em;
    top: auto;
}

.search-table tr th:nth-child(2), .search-table tr td:nth-child(2) {
    padding-left: 100px;
}
&#13;
<div class="container header">
<div class="search-table-outter wrapper">
  <table class="search-table inner">
    <tr>
      <th class="headcol">Time</th>
      <th>Icon</th>
      <th>Description</th>
      <th>Temp</th>
      <th>Precip</th>
      <th>Wind</th>
    </tr>
    <tr>
      <td class="headcol">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="headcol">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="headcol">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="headcol">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="headcol">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="headcol">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="headcol">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
    <tr>
      <td class="headcol">4:11 </td> 
      <td>Cloud </td>
      <td>Partly Cloudy </td>
      <td>86 </td>
      <td>0% </td>
      <td>8.38 mph </td>
    </tr>
  </table>
</div>
</div>
&#13;
&#13;
&#13;

请参阅小提琴https://jsfiddle.net/s8bczc0L/