我不知道为什么这个简单的CSS不能工作…

.app a { 高度:18 px; 宽度:140 px; 填充:0; 溢出:隐藏; 位置:相对; Margin: 0 5px 0 5px; text-align:中心; 文字修饰:没有; 文本溢出:省略; 空白:nowrap;} 颜色:# 000; } < div class = "应用" > <a href="">Test Test Test Test Test Test Test</a> . < / div >

应该在第四次“测试”前后切断


当前回答

公认的答案是棒极了。但是,您仍然可以使用% width并获得text-overflow: ellipsis。解决方法很简单:

display: inline-block; /* for inline elements e.g. span, strong, em etc */
text-overflow: ellipsis;
width: calc(80%); /* The trick is here! */

似乎无论何时使用calc,最终值都是以绝对像素表示的,因此对于1000px宽度的容器,将80%转换为800px之类的值。因此,不要使用width: [YOUR PERCENT]%,而是使用width: calc([YOUR PERCENT]%)。

其他回答

把这些写在你的css规则中。

display: block; /* or in-line block according to your requirement */
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;

包括info后面写的四行,以便省略号起作用

.app a
{
 color: #fff;
 font: bold 15px/18px Arial;
 height: 18px;
 margin: 0 5px 0 5px;
 padding: 0;
 position: relative;
 text-align: center;
 text-decoration: none;
 width: 140px;

 /* 
 Note: The Below 4 Lines are necessary for ellipsis to work.
 */

 display: block;/* Change it as per your requirement. */
 overflow: hidden;
 text-overflow: ellipsis;
 white-space: nowrap;
}

还请确保,直接的封闭元素具有固定的宽度,并且希望应用省略号的跨度具有display:block

你只需要添加一行css:

.app a {
   display: inline-block;
}

锚,跨度…标签默认是内联元素,如果内联元素宽度属性不起作用。因此,您必须将元素转换为内联块或块级元素