动态宽度元素的文本溢出省略号

28

我在尝试对一个动态宽度的元素使用text-overflow: ellipsis时遇到了一些问题。我已经研究了其他解决方案,但它们似乎都使用某种形式的静态宽度,而我希望实现完全动态的解决方案。Javascript是可行的选择,但如果可能,我更喜欢将其限制在CSS中。我还有一个限制,即任何解决方案都必须兼容IE8。以下是我目前的代码:

HTML代码:

<div class="container">
    <div class="col-1 cell">
        <h1>Title</h1>
    </div>
    <div class="col-2 cell">
        <nav>
            <ul>
                <li><a href="#">Main #1</a></li>
                <li><a href="#">Main #2</a></li>
                <li><a href="#">Main #3</a></li>
                <li><a href="#">Main #4</a></li>
                <li><a href="#">Main #5</a></li>
            </ul>
        </nav>
    </div>
    <div class="col-3 cell">
        <div class="foo">
            <img src="http://placehold.it/50x50" alt="">
        </div>
        <div class="bar">
            Some overly long title that should be ellipsis
        </div>
        <div class="baz">
            v
        </div>
    </div>
</div>

SCSS:

.container {
    border: 1px solid #ccc;
    display: table;
    padding: 30px 15px;
    table-layout: fixed;
    width: 100%;
}

.cell {
    display: table-cell;
    vertical-align: middle;
}

.col-1 {
    width: 25%;
    
    h1 {
        margin: 0;
        padding: 0;
    }
}

.col-2 {
    width: 50%;
    
    ul {
        margin: 0;
        padding: 0;
    }
    
    li {
        float: left;
        list-style: none;
        margin-right: 10px;
    }
}

.col-3 {
    width: 25%;
    
    .foo {
        float: left;
        
        img {
            display: block;
            margin: 0;
            padding: 0;
        }
    }
    
    .bar {
        float: left;
        height: 50px;
        line-height: 50px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    
    .baz {
        background: #ccc;
        float: left;
        height: 50px;
        line-height: 50px;
        padding: 0 5px;
    }
}
理想情况下,我希望具有类名为.bar的元素占据.col-3的剩余宽度。任何正确方向上的指引将不胜感激。 这里是相关的JSFiddle链接
3个回答

52
只需将max-width: 100%添加到元素中,就能实现你想要的效果。之所以需要设置某种宽度是因为元素会继续扩展,直到你告诉它不能再扩展为止。
这里有一个JSFiddle Example
.bar {
    float: left;
    height: 50px;
    line-height: 50px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    
    /* new css */
    max-width: 100%;
}

编辑- Flexbox 版本

所以,这里是一个 flexbox 显示。你需要使用像这样的库来为 IE8 进行 shim: https://github.com/sfioritto/real-world-flexbox/tree/master/demos/flexie

下面是用于不支持的浏览器的修复方法:

CSS 更新

.container {
    border: 1px solid #ccc;
    display: flex;
    flex-direction: row;
    padding: 30px 15px;
    width: 100%;
}

.cell {
    flex: 1 1 33%;
    vertical-align: middle;
}

.col-1 {
    width: 25%;
    }
.col-1 h1 {
  margin: 0;
  padding: 0;
}

.col-2 {
    width: 50%;
    }
.col-2 ul {
  margin: 0;
  padding: 0;
}

.col-2 li {
  float: left;
  list-style: none;
  margin-right: 10px;
}

.col-3 {
    width: 25%;
    display: flex;
    flex-direction: row;
}
.col-3 .foo {
  flex: 0 1 auto;
}
.col-3 .foo img {
  display: block;
  margin: 0;
  padding: 0;
}
    
.col-3 .bar {
  height: 50px;
  line-height: 50px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1 2 auto;
}
    
.col-3 .baz {
  background: #ccc;
  height: 50px;
  line-height: 50px;
  padding: 0 5px;
  flex: 1 1 auto;
}

JSFiddle 示例


1
刚意识到你可能想要一个 flex 布局。请给我一点时间来编辑。 - Josh Burgess
我做了一些修改,但这让我达到了我需要的目的。谢谢! - Hughes

7
请尝试让父元素设置 "min-width: 0;"。参考:Carlor 在 codepen.io 上的示例 (https://codepen.io/cjulian/pen/wrOyJz)。

.wrapper {
  display: flex;
  width: 100%;
}

.fluid {
  flex: 1 1 100%;
  min-width: 0;
}

.fluid-content {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.static-content {
  width: 200px;
}

/* not required styles */
.wrapper {
  border: 1px solid black;
}

.fluid-content {
  background-color: pink;
}

.static-content {
  background-color: yellow;
}
<h1>text-overflow on a fluid width container</h1>
<div class="wrapper">
  <div class="fluid">
    <div class="fluid-content">
      This div does not have a fixed width. Its content will not wrap.
      If the content does not fit it will be truncated with ellipses.
    </div>
  </div>
  <div class="static">
    <div class="static-content">
      fixed width
    </div>
  </div>
</div>


3
@geertjanknapen 是的,我是这样做的。因为我不理解那个答案,我使用了max-width,但它没有起作用,我点击了示例链接,但示例也无法正常工作。所以我使用了min-width找到了答案,并且它有效。 - Louis Tran
1
@geertjanknapen的回答对我很有帮助,而被接受的答案则没有。 - VityaSchel
这对我也起作用了,有人能解释一下这种交互吗? - pdenya
1
当您在 flex 项目上设置 min-width: 0 时,它会覆盖默认的最小尺寸。这告诉浏览器该项目可以缩小到宽度为 0(如果需要),从而允许该项目比其内容的默认最小尺寸更小,从而允许文本溢出并在元素的宽度动态减小时出现省略号。 - Hyunjune Kim

0
如果您的表格宽度为100%,并且想要在<th><td>内部添加省略号以适应动态字符串长度,则下面的代码对您有帮助。

table {
  width: 100%;
}
table, tr , td, th {
  border: 1px solid;
}
tbody tr td:nth-child(1) {
  width: 80%;
  max-width: 1px;
  text-overflow: ellipsis;
  width: 60cm;
  overflow: hidden;
  white-space: nowrap;
}
      
tbody tr td:nth-child(2) {
  width: 20%;
}
<html>
  
<table>
  <thead>
    <tr>
      <th>Month</th>
      <th>Savings</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="broadcast-notification-title-section">January testing demo testing testing can be done by me to be done</td>
      <td>$100</td>
    </tr>
    <tr>
      <td class="broadcast-notification-title-section">February testing demo</td>
      <td>$80</td>
    </tr>
  </tbody>
  
</table>

 
</html>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接