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

Text overflow ellipsis on dynamic width element

我在尝试让 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 的 class 元素占据 .col-3 的剩余宽度。任何朝着正确方向的推动都将不胜感激。这里也是 link 到 JSFiddle

只需向元素添加 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%;
}

#Edit – Flexbox 版本

所以,这是一个 flexbox 显示。您将需要使用这样的库为 IE8 填充: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 Example

请尝试让父级有“min-width: 0;”。根据:卡洛 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>