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

标签 html css overflow

我在尝试让 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

最佳答案

只需将 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

关于html - 动态宽度元素上的文本溢出省略号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31928710/

相关文章:

html - 100% 高度 div 消失

jquery - 使用 jQuery 查找具有匹配的特定 CSS 属性值集的所有元素?

html - 上边框和左边框相交的像素

jquery - ContentEditable DIV 的 MVC Razor 编辑器模板

html - 元素上的 CSS 动态宽度

CSS 父 DIV 溢出

html - DRY原则的精确性

html - CSS ONLY 动画绘制带有边框半径和透明背景的圆

css - 当多个表格样式可以相互出现时,如何为它们创建样式表?

android - 溢出-x :hidden not working w/Android (Samsung Epic) and possibly others