【发布时间】:2017-06-12 12:08:10
【问题描述】:
这是我的代码的链接:
https://jsfiddle.net/Deepview/hr111npf/19/
html:
<div style="width: 300px; height:54px; border: 1px solid">
<div style="display:table; width:100%">
<div style="display:table-row; width:100%">
<div id="div1" style="display: table-cell; width 50px; background-color: #c0c0c0;">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a8/Face-kiss.svg/50px-Face-kiss.svg.png"/>
</div>
<div id="div2" style="display: table-cell; width: 100%; background-color: #ffff00; vertical-align: middle; " >
<div class="caption">This is a long sentence that should get clipped and ellipsis shown</div>
</div>
<div id="div3" style="display: table-cell; width: 14px; background-color: #b8eaff; vertical-align: middle">
<img src="http://reports.tofg.com/i/themes/theme_51/css/blue/images/icons-png/arrow-l-black.png"/>
</div>
<div id="div4" style="display: table-cell; width: 14px; background-color: #ffcccc; vertical-align: middle">
<img src="https://www.monstermoto.com/img/arrow_red.png"/>
</div>
</div>
</div>
</div>
css:
.caption {
font-size: 1.03em;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
padding-left: 5px;
width: 100%;
max-width: 230px;
}
外部 div 的宽度必须保持固定在 300 像素。
我有一个由单行 4 列组成的 div 表。第一列有一个宽度固定的图像。最后两列是也具有固定宽度的图像。但是,必须始终显示最右边的最后一个图像,而它旁边的图像可能会显示或隐藏。如果它是隐藏的,它不能占用任何宽度空间(好像单元格甚至不存在一样)。
第二列包含文本。此文本的长度可能会有所不同。如果文本太长,将显示省略号。如果第 3 列中的图像可见,则必须缩短其左侧文本的宽度。如果图像不可见,则需要增加文本宽度,使其在最后一列中的图像旁边结束。
在保留省略号的同时,我无法成功让文本的宽度自动调整。另一个问题是,如果文本比示例中的大得多,最后两列中的图像会被推到表格之外。
关于如何让第二列自动调整其宽度而不导致最后两列中的图像移动或丢失省略号的任何提示?
我可以用 javascript 解决这个问题,但我正在寻找一个普通的 css 解决方案。
【问题讨论】: