【问题标题】:Responsive table with first column minimum width; second column stretch; third column always at the end; how can i do that?具有第一列最小宽度的响应表;第二列拉伸;第三列总是在最后;我怎样才能做到这一点?
【发布时间】:2020-04-30 08:05:19
【问题描述】:

我需要一个 html 表格

下面的模型显示了应该涵盖哪些情况。

  1. 表格不应溢出屏幕,即使在小型设备上(移动支持)
  2. 第一行应尽可能小(宽度); smalles 宽度是其中最大元素的宽度
  3. 第二列应覆盖其余空间
  4. 第三列应始终位于末尾,并且应尽可能小(与第一列一样)
  5. 行高应该是动态的
  6. 一行的所有单元格应该在一行/应该有相同的高度

我已经用flextable 试过了

  • table 我遇到的问题是第二列没有响应(固定表格)或溢出屏幕,因为第二列的内容太大并且没有中断或缩小
  • flexbox 我没有得到相同的第一列,即使宽度不同,内容长度也不同

一些特价商品

  • 第一列应该包含时间,问题是3:0010:00 的宽度不同,我不想要静态宽度,因为那样会浪费空间或者对于其他语言环境来说太小了李>
  • 第二列应包含项目(div(称为芯片的自定义组件)),如果它们知道最大宽度,则可以缩小和破坏
  • 第三列用于操作 - 总是在第二列有足够空间的情况下,宽度尽可能小

2020 年 15 月 1 日编辑

第二列中的元素不应中断,表格不应在移动视图上滚动。 否则第二列中的所有项目都应该被截断。

我已经找到了解决方案(如下),但如果有人在没有网格的情况下得到相同的结果 - 不客气!

【问题讨论】:

  • 当你说全宽是什么意思?
  • 父容器全宽(宽度:100%)
  • 如果您尝试将一列设为 100% 并在其旁边添加其他 2 列,您将溢出。
  • 左列必须是动态的还是你可以给它分配一个静态值?
  • 它必须是动态的

标签: html css html-table flexbox


【解决方案1】:

如果我理解正确,你可以试试这个:

<table class='table'>
  <thead>
    <th class='time'>time</th>
    <th class='text'>information</th>
    <th class='actions'>actions</th>
  </thead>
  <tbody>
    <tr>
      <td>04:00</td>
      <td>
          <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer blandit consequat dui, id lacinia est. Integer blandit, felis nec condimentum ornare, ex diam cursus leo, id rutrum dolor nibh vel libero. Integer ut augue semper, convallis libero id, finibus ipsum. Cras nec metus bibendum, dictum mi ut, ullamcorper lectus.           </span>
      </td>
      <td>
        <button>Action</button>
      </td>
    </tr>
  </tbody>
</table>


.table
  text-align: left
  width: 100%
  .time 
    min-width: 30px
    max-width: 50px
  .actions
    min-width: 80px //This value is a example

您可以测试:https://codepen.io/AlissonRGalindo/pen/VwYdMRm

【讨论】:

  • 将此html添加到td:&lt;div style=" white-space: nowrap; "&gt;some dudes loooooooooooooooooooooong name&lt;/div&gt;与此css:white-space: nowrap; text-overflow: ellipsis;它不起作用。它在移动设备上没有响应。
【解决方案2】:

您还可以使用表格方法,而不会失去响应性,并且即使未指定宽度,第二列也会拉伸。

在我的代码中,表格延伸到其父级(即主体)的 100%。确保您的表的父级将具有定义的宽度。

<table cellspacing="0">
  <tr>
    <th>Time column</th>
    <th>Column 2</th>
    <th>Column 3</th>
  </tr> 
  <tr>
   <td>3:00</td>
    <td>Longer text for a longer column</td>
    <td><input type="checkbox"></td>
  </tr>
  <tr>
    <td>11:00</td>
    <td>Longer text for a longer column</br>and a row break</td>
    <td><input type="checkbox"></td>
  </tr>
  <tr>
   <td>06:00</td>
    <td><div>Here's a div containing a <span style="color: red;">span</span></div></td>
    <td><input type="checkbox"></td>
  </tr>
</table>

table, td, th {
  border: 1px solid white;
  background: orange;
}

table {
  width: 100%;
}

tr td:first-of-type, tr td:last-of-type { 
  width: 1px;
  text-align: center;
}

这个codePen也有样式,你可以通过调整窗口大小来测试表格的响应能力:https://codepen.io/ialexandru/pen/mdyKqRX

【讨论】:

  • 问题是不要对第一列和最后一列使用静态宽度(以 % 或其他形式)。第一列和最后一列的宽度必须是其中最宽的内容的宽度,如果有空格,也要考虑换行。
  • 我想我现在明白了,如果你为第一列和最后一列设置一个非常低的宽度值,那么列将尽可能小,但不小于最宽单词的宽度。再次检查上面的 codePen。似乎它可能适用于 1% 的宽度。我知道您提到您不想要任何静态值,但必须设置宽度,因此浏览器应该知道这两列必须非常小才能使第二列变大。尝试用一个字符替换 Time column 文本和小时,看看它是如何工作的。
  • 将此 html 添加到 td:&lt;div style=" white-space: nowrap; "&gt;some dudes loooooooooooooooooooooong name&lt;/div&gt; 与此 css:white-space: nowrap; text-overflow: ellipsis; 它不起作用。它在移动设备上没有响应。
  • 您将无法使用 white space: nowrap; 从该表中获得响应式布局,因为 div 的内容将始终写在一行上并且第二列将拉伸以使文本有足够的空间容纳。
【解决方案3】:

此 html 和 css 将符合您的要求。请注意,表周围的父 div 是必需的。

更新

  • 您可以在 https://jsfiddle.net/uwpLrd1e/ 上找到 JSFiddle
  • &lt;th&gt; 默认将文本居中。使用text-align:left 将其左对齐。
  • 根据需要添加内边距、颜色和边框。
  • 请使用内边距(不是边距),因为边距会超出宽度。

.

<div class="table-wrapper">
  <table>
    <tbody>
      <tr>
        <td>23:59</td>
        <td>Second column</td>
        <td>[]</td>
      </tr>
      <tr>
        <td>1:11</td>
        <td>Column 2 on row 2</td>
        <td>[X]</td>
      </tr>
    </tbody>
  </table>
</div>

/* table wrapper is required!! */
.table-wrapper {
  max-width: 100%;
}
.table-wrapper,
.table-wrapper * {
  box-sizing: border-box;
}
table, thead, tbody, tr {
  width: 100%;
}
table {
  max-width: 100%;
  border-spacing: 0;
  border-collapse: collapse;
  background-color: transparent;
}
th, td {
  vertical-align: top;
  border-left: 1px solid #ccc;
  line-height: 1;
  hyphens: auto;
  white-space: normal;
}
th:first-child,
td:first-child {
  border-left: 0;
}
/* stretch middle column (of three columns) */
th:nth-child(2),
td:nth-child(2) {
  width: 99.99%;
}
/* mobile support */
@media (max-width: 767.9px) {
  table {
    display: block;
    border: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    -ms-overflow-style: -ms-autohiding-scrollbar;
  }
  th, td {
    font-size: 95%;
  }
}

【讨论】:

  • 你有测试链接吗?
  • 将此 html 添加到 td:&lt;div style=" white-space: nowrap; "&gt;some dudes loooooooooooooooooooooong name&lt;/div&gt; 与此 css:white-space: nowrap; text-overflow: ellipsis; 它不起作用。它没有反应。
  • white-space: nowrap; 从不提供响应式布局,因为所有内容都将保留在一行上。仅当您考虑到后果时才使用此属性。
  • 我在 TD 中测试了&lt;div style="white-space: nowrap; text-overflow: ellipsis;"&gt;some dudes loooooooooooooooooooooong name&lt;/div&gt;,它完全响应,因为“表格 X 轴”上的滚动条可用。这是在&lt;table&gt; 设计中执行此操作的唯一方法,因为无法堆叠&lt;td&gt; 单元。
  • 请再次结帐jsfiddle.net/3wo2umvz/2。在 TD 中添加了word-break: break-all
【解决方案4】:

您还可以将table-layout 设置为auto(默认值)并将width 设置为0 用于第一个和最后一个td,因此它只会扩展到其所需的宽度内容。中间的td 将跨越整个剩余空间。 它需要为表设置一个width

例子

table {
  width: 100%;
  border: solid 1px;
  table-layout:auto;
}

th,td {
  border: solid 1px;
  padding: 0.25em
}

tr > :first-child,
tr > :last-child {
  width: 0 ; /* !! make sure table-layout is set to auto */
  /* white-space : nowrap; could be handy here */
}
<table>
  <tr>
    <td><input value="input"></td>
    <td> use room left</td>
    <td><input type="checkbox"></td>
  </tr>
</table>
<hr>
<table>
  <tr>
    <th>A.1</th>
    <td> use room left</td>
    <td>(....)</td>
  </tr>
  <tr>
    <th>A.2</th>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <th>A.3</th>
    <td> use room left</td>
    <td>( / )</td>
  </tr>
</table>
<hr>
<p>to clear some question via a visual: </p>

<table>
  <tr>    
    <td>What if only 2 cells </td>
    <td> </td>
  </tr>
</table>
<hr>

<table>
  <tr>  
    <td> </td>  
    <td>What if only 2 cells </td>
  </tr>
</table>
<hr>

<table>
  <tr>    
    <td>What if only 1 cell</td>
  </tr>
</table>

https://developer.mozilla.org/en-US/docs/Web/CSS/table-layout

table-layout CSS 属性设置用于布置&lt;table&gt; 单元格、行和列的算法。

自动

默认情况下,大多数浏览器使用自动表格布局算法。 调整表格及其单元格的宽度以适应内容。

【讨论】:

  • 将此 html 添加到 td:&lt;div style=" white-space: nowrap; "&gt;some dudes loooooooooooooooooooooong name&lt;/div&gt; 与此 css:white-space: nowrap; text-overflow: ellipsis; 它不起作用。它没有反应。
  • 是的,我没想到。但我需要一个解决方案。我发布了一个使用网格布局而不是 flex 或 table 的解决方案。
  • @DanielDäschle 如果您的内容需要一个表格结构,网格仍然可以应用于它;)
【解决方案5】:

最终解决方案:

CSS 网格(93.43% 支持)

.flex {
    display: flex;
    flex-wrap: wrap;
    overflow: hidden;
}

.item {
    margin-right: .6em;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

.my-grid {
    display: grid;
    grid-template-columns: max-content auto max-content;
    grid-gap: .6rem;
}
<div class="my-grid">

    <!-- first row -->
    <span>first column</span>
    <div class="flex">
        <div class="item">some div with very looooong content that truncate</div>
        <div class="item">some div with very looooong content that truncate</div>
    </div>
    <div>here some actions</div>

    <!-- second row -->
    <span>first column</span>
    <div class="flex">
        <div class="item">some div with very looooong content that truncate</div>
        <div class="item">some div with very looooong content that truncate</div>
    </div>
    <div>here some actions</div>

</div

【讨论】:

  • 您可以在表格中使用 display:contents over thead/tbody 和 tr 来做到这一点。
  • 什么意思?
  • 一个响应式示例,没有省略号,因为表格可能非常紧凑,省略号可能只是一种痛苦;)codepen.io/gc-nomade/pen/VwwweeX,响应式的其他可能性,滚动? codepen.io/gc-nomade/details/abboqbR(只是说响应式可能意味着很多事情;))
  • 内容在小屏幕上不可见。没有滚动条可以看到它。 jsfiddle.net/712c0qhb
  • 我并不讨厌它。我还没有时间测试一切。我检查了我提供的这个例子也不能完全正常工作。我必须看看我的项目。我不想滚动,我希望动作在移动设备上始终可见,我也不希望在中间列滚动,它们应该中断(除了一个 div,这应该是椭圆)这是我真实世界的例子:@ 987654324@
猜你喜欢
  • 1970-01-01
  • 2021-12-01
  • 2015-08-08
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 2020-05-24
  • 2018-02-28
  • 2021-09-19
相关资源
最近更新 更多