2017-08-06 64 views
0

我需要在固定佈局中放置一個表格並在可滾動容器中固定列寬。該表及其單列應占用colgroup部分中定義的空間。滾動容器中固定佈局的表格不消耗定義的寬度

這是HTML:

<div class="scroll-container"> 

    <table> 
    <colgroup> 
     <col width="100px"> 
     <col width="100px"> 
     <col width="50px"> 
    </colgroup> 

    <tr> 
     <td>width &lt; 100px</td>  
     <td>width &lt; 1 100px</td> 
     <td>width &lt; 150px</td> 
    </tr> 
    </table> 

</div> 

而且這個CSS:

div.scroll-container{ 
    width : 200px; 
    height : 90px; 
    background-color : #ddd; 
    overflow-x: scroll; 
} 

table{ 
    table-layout: fixed; 
} 

td{ 
    border : 1px solid black; 

} 

列的寬度被縮小,在div容器不滾動,而我要實現的對面。

的js小提琴:https://jsfiddle.net/n337p1ck/1/

回答

1

你只需要添加:

width:100%; 

到表

div.container{ 
 
    width : 260px; 
 
    height : 70px; 
 
    background-color : #ccc; 
 
} 
 

 
div.scroll-container{ 
 
    width : 200px; 
 
    height : 90px; 
 
    background-color : #ddd; 
 
    overflow-x: scroll; 
 
} 
 

 
table{ 
 
    table-layout: fixed; 
 
    width:100%; 
 
} 
 

 
td{ 
 
    border : 1px solid black; 
 
    
 
}
<div class="container"> 
 

 
    <table> 
 
    <colgroup> 
 
     <col width="100px"> 
 
     <col width="100px"> 
 
     <col width="50px"> 
 
    </colgroup> 
 

 
    <tr> 
 
     <td>width = 100px</td>  
 
     <td>width = 100px</td> 
 
     <td>width = 50px</td> 
 
    </tr> 
 
    </table> 
 

 
</div> 
 

 
<hr/> 
 

 
<div class="scroll-container"> 
 

 
    <table> 
 
    <colgroup> 
 
     <col width="100px"> 
 
     <col width="100px"> 
 
     <col width="50px"> 
 
    </colgroup> 
 

 
    <tr> 
 
     <td>width &lt; 100px</td>  
 
     <td>width &lt; 1 100px</td> 
 
     <td>width &lt; 150px</td> 
 
    </tr> 
 
    </table> 
 

 
</div>

+0

很大,這很簡單:-)謝謝! – Paul