【发布时间】:2018-04-04 12:14:25
【问题描述】:
我创建了一个带有 ::before 伪元素的锚点,用于 SVG 图标。当::before 是两者 position: relative 并且在使用top/left 时定位为负值或在使用bottom/right 时定位为正值时,text-overflow: ellipsis 功能会中断。
a {
display: block;
border: 1px solid blue;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
width: 200px;
}
a::before {
content: '';
display: inline-block;
width: 20px;
height: 20px;
border: 1px solid red;
position: relative;
}
a.test2::before, a.test3::before {
left: -1px;
}
a.test3::before {
position: absolute;
}
<a class="test1">some very long text which overflows</a>
<a class="test2">some very long text which overflows</a>
<a class="test3">some very long text which overflows</a>
例如,在每个示例中,文本应该溢出并得到一个省略号,但第二个示例中断(至少在 Chrome 61、macOS 中)。
Mac 上的 Firefox 56 似乎没有有这个问题。
使用position: absolute 或其他一些变体可以解决问题,但我找不到太多关于为什么会发生这种情况的解释。
【问题讨论】: