html - 无法使省略号与显示一起使用 : table-cell

标签 html css overflow css-tables ellipsis

我有一个用 css display: table 制作的布局。它只有两列。其中一列包含 ul元素。这是li的元素基本上是带有标题行和描述行的搜索结果项。我想在描述行上应用省略号以将其保留在一行中,因此如果文本溢出,它的文本不会影响整个列的宽度:

enter image description here

左边是长文本发生的情况;理应如此。

现在,当我尝试应用省略号内容(包括 white-space: nowrap )时,文本会弄乱列的宽度,使其适合文本宽度,并且省略号不会显示。

.table { display: table; width: 100%; border: 1px solid #f00; margin-bottom: 20px }
.table-row { display: table-row }
.table-cell { display: table-cell; }
.table-cell:first-child { width: 30%; }
.table-cell ul { padding: 0; margin: 0 }
.table-cell ul li { list-style: none; }
.item { border: 1px solid #000; }
.item-desc { 
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
Table 1 - The issue
<div class="table">
  <div class="table-row">
    <div class="table-cell">
      <ul>
        <li>
          <div class="item">
            <div class="item-name">Item 1</div>
            <div class="item-desc">
              Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 
              Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 
              Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="table-cell">Nothing here</div>
  </div>
</div>

Table 2 - How it should work
<div class="table">
  <div class="table-row">
    <div class="table-cell">
      <ul>
        <li>
          <div class="item">
            <div class="item-name">Item 1</div>
            <div class="item-desc">
          Item 1 Item 1 Item 1 Item...
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="table-cell">Nothing here</div>
  </div>
</div>

表1是我当前得到的css;表 2 应该是这样的。

问题似乎是缺乏元素 div 宽度的定义,这应该尊重 table-cell容器。我不知道如何开展这方面的工作。我该如何改进.item-desc使省略号起作用并保持列的原始宽度?

最佳答案

表格使用table-layout:fixed - 请参阅下面的演示:

.table {
  display: table;
  width: 100%;
  border: 1px solid #f00;
  margin-bottom: 20px;
  table-layout: fixed; /* ADDED THIS */
}
.table-row {
  display: table-row
}
.table-cell {
  display: table-cell;
}
.table-cell:first-child {
  width: 30%;
}
.table-cell ul {
  padding: 0;
  margin: 0
}
.table-cell ul li {
  list-style: none;
}
.item {
  border: 1px solid #000;
}
.item-desc {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
<div class="table">
  <div class="table-row">
    <div class="table-cell">
      <ul>
        <li>
          <div class="item">
            <div class="item-name">Item 1</div>
            <div class="item-desc">
              Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1 Item 1
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="table-cell">Nothing here</div>
  </div>
</div>

关于html - 无法使省略号与显示一起使用 : table-cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42203730/

相关文章:

c - 为什么在 mac os 上 memchr() 会发生越界

HTML/CSS Plotly 绘图大小

php - 如何阻止网页包装内容?

javascript - iFrame 元素未在容器内调整大小的问题

html - 影响其他表单的复选框

html - 如果用户点击关闭按钮,总是隐藏 div

css - MVCGrid.net - 将列标题和排序图标保持在一行中

css - 溢出-y空白空间错误?

javascript - 通过单击任何单元格或表格数据及其左侧和顶部标题进行查找

Android - Html.fromHtml 句柄背景颜色