两个 div 元素并排溢出的文本

Text overflow with two div elements side by side

我制作这个 jsfiddle 是为了展示一些接近我目前拥有的东西。

我正在寻找的是位于右侧 div 旁边的行左侧 div 的省略号,这样它就不会重叠(我意识到 position 是可能不会在这里工作)。两者都有动态文本。如有必要,我愿意更改上述内容。

编辑: 我需要第一行的第一部分只在一行上,当它与第二部分重叠时溢出。到目前为止,答案尚未提供执行此操作的方法。如果 div 的内容是静态的,我会设置静态宽度。我 可能 能够为第二个 div 设置静态宽度,因为其中内容的可能性是有限的。

注意:列宽的大小根据浏览器尺寸而变化。此外,由于页面上存在其他 containers/elements,因此它是绝对定位的。 (可能不太理想)

另一个注意事项:该专栏可能有很多 row,所以我只显示几个作为示例。

这是一些示例代码:

.col {
    background-color: pink;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: 400px;
}

.row {
    background-color: #9595E4;
    border-bottom: 1px solid black;
    padding: 3px 8px;
}
p {
    margin: 0;
    padding: 0;
}

.row-top {
    position: relative;
    height: 19px;
    line-height: 19px;
}

.row-top .one {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    height: auto;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    font-weight: bold;
}

.row-top .two {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: auto;
    width: auto;
    height: auto;
    font-size: 0.8em;
    color: #ccc;
}
<div class="col">
    <div class="row">
        <div class="row-top">
            <div class="one">
                This is the first line, part one, which overlaps
            </div>
            <div class="two">
                1st line, part two
            </div>
        </div>
        <p>This is the second line</p>
    </div>
    <div class="row">
        <div class="row-top">
            <div class="one">
                This content could be really really really really long
            </div>
            <div class="two">
                1st line, part two
            </div>
        </div>
        <p>This is the second line</p>
    </div>
    <div class="row">
        <div class="row-top">
            <div class="one">
                Or it could be short
            </div>
            <div class="two">
                1st line, part two
            </div>
        </div>
        <p>This is the second line</p>
    </div>
</div>

css 的解决方案:https://css-tricks.com/line-clampin/

使用 jquery 插件的解决方案:http://dotdotdot.frebsite.nl/

您声明了一列并在其中声明两行,这在技术上是错误的。相反,您应该声明一行并创建任意多的列(在本例中只有两列,因为您只希望左右元素并排放置)。最近我回答了这样一个场景

我能够通过这个 jsfiddle

实现我所需要的

代码如下:

.col {
    background-color: pink;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: 400px;
}

.row {
    background-color: #9595E4;
    border-bottom: 1px solid black;
    padding: 3px 8px;
}
p {
    margin: 0;
    padding: 0;
}

.row-top {
    position: relative;
    height: 19px;
    line-height: 19px;
}

.row-top .one {
    position: absolute;
    top: 0;
    right: 125px;
    bottom: 0;
    left: 0;
    width: auto;
    height: auto;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    font-weight: bold;
}

.row-top .two {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: auto;
    width: auto;
    height: auto;
    font-size: 0.8em;
    color: #ccc;
    max-width: 125px;
}
<div class="col">
    <div class="row">
        <div class="row-top">
            <div class="one">
                This is the first line, part one, which overlaps
            </div>
            <div class="two">
                1st line, part two
            </div>
        </div>
        <p>This is the second line</p>
    </div>
    <div class="row">
        <div class="row-top">
            <div class="one">
                This is the first line, part one, which overlaps
            </div>
            <div class="two">
                1st line, part two
            </div>
        </div>
        <p>This is the second line</p>
    </div>
</div>

我在第二个 div 上添加了 max-width: 125px,并在第一个 div 上将 right 更改为 125px 以进行补偿。问题在于第二个 div 的内容小于或大于 125px。