2017-05-06 76 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

相关问题