2017-05-06 85 views
0

我有一張表格,其中省略號應該應用於第一個單元格(第一個單元格應該填充剩餘空間),但是表格會在父div之外。我怎樣才能使它工作?省略號在表格單元格中不起作用

.wrapper { 
 
    width:200px; 
 
    background: wheat; 
 
} 
 

 
.el-cell { 
 
    width:99%; 
 
    display: block; 
 
} 
 
.table { 
 
    width: 100%; 
 
} 
 

 
.no-wrap { 
 
    white-space: nowrap; 
 
} 
 

 
.el { 
 
    overflow: hidden; 
 
    text-overflow: ellipsis; 
 
    white-space: nowrap; 
 
}
<div class="wrapper"> 
 
    <table class="table"> 
 
    <tr> 
 
     <td class="el-cell"> 
 
     <div class="el"> 
 
      <span>first cell should ellipsis aaaaaaa bbbb ccccccc</span> 
 
     </div> 
 
     </td> 
 
     <td> 
 
     <span class="no-wrap">2nd cell</span> 
 
     </td> 
 
    </tr> 
 
    </table> 
 
</div>

回答

1

添加position: relative.el-cellposition: absolutespan具有屬性top: 0left: 0

.wrapper { 
 
    width:200px; 
 
    background: wheat; 
 
} 
 

 
.el-cell { 
 
    width:99%; 
 
    position: relative; 
 
} 
 

 
.el span{ 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    width: 100%; 
 
    overflow: hidden; 
 
    text-overflow: ellipsis; 
 
    white-space: nowrap; 
 
}
<div class="wrapper"> 
 
    <table class="table"> 
 
    <tr> 
 
     <td class="el-cell"> 
 
     <div class="el"> 
 
      <span>first cell should ellipsis aaaaaaa bbbb ccccccc</span> 
 
     </div> 
 
     </td> 
 
     <td> 
 
     <span class="no-wrap">2nd cell</span> 
 
     </td> 
 
    </tr> 
 
    </table> 
 
</div>

0

只需添加table-layout: fixed

+0

'表格的佈局:fixed' brokes寬度99% - 細胞填充表的一半。 – pwas

+0

爲什麼不爲其他單元格設置1%的寬度?你真的需要在每個單元格內有這麼多元素嗎?什麼是最終目標?請記住,200px的1%太小(2px)。 – Dimcho

+0

因爲'99%'使得第一個單元格填充整個可用空間 - 所以第二個單元格不是1% - 它具有其子節點的寬度。 – pwas