【问题标题】:Field <select> in <td>, with long text, crushing the table<td> 中的字段 <select>,带有长文本,压垮了表格
【发布时间】:2017-02-08 18:04:38
【问题描述】:

我有一张桌子,在&lt;td&gt; 我有一些输入和选择。当&lt;option&gt; 中的&lt;select&gt; 有很长的文本时,它会扩展&lt;td&gt;,所以它会压碎桌子。像往常一样,表 td = th 内容长度,td 的最小宽度 = th 中最大单词的长度。如果我将宽度设为 td 或 th - 它不起作用,只需选择宽度即可。但是我在选项中有动态内容,所以为它制作静态宽度会很糟糕。我怎样才能使选择没有扩展 td 并且同样具有宽度? Plnkr example

【问题讨论】:

  • 你所说的“压垮桌子”是什么意思?
  • 如果选项中的内容非常大 - td 将是相同的大小,所以表格视图超出边缘
  • 将max-width 添加到元素的样式中
  • @SAM 我猜你是对的,但是我有一个动态表,所以如果我设置 max-width = 100px 并且它对 4 列表有好处 - 对于 2 列它会很糟糕(跨度>
  • 考虑改变表格的方向,使其从左到右而不是当前从上到下,因此选择将排列在一列而不是当前行中。或使用表删除并使用例如DL/DT/DD 代替。

标签: html angularjs css sass


【解决方案1】:

也将宽度应用于选择元素:

<select style="width:50px;"></select>

【讨论】:

    【解决方案2】:

    由于表格元素随内容增长,您需要给它们一个宽度,所以如果您像这样更新 CSS 它将起作用

    table {
      width: 100%;
      border-collapse: collapse;
    }
    td {
      width: 25%;
    }
    td select {
      width: 100%;
    }
    

    示例 sn-p

    table th, table td {
      border:1px solid grey;
    }
    table {
      width: 100%;
      border-collapse: collapse;
    }
    td {
      width: 25%;
    }
    td select {
      width: 100%;
    }
        <table>
          <thead>
            <tr>
              <th>Group Name</th>
              <th style='width:50px'>Members</th>
              <th>Genre</th>
              <th>From</th>
            </tr>
          </thead>
          
          <tbody>
            <tr>
              <td>
                <select>
                  <option>Red Hot Chili Peppers</option>
                  <option>Depeche Mode</option>
                  <option>very-very-very long text, it will be crush the table</option>
                </select>
              </td>
              <td>
                <select>
                  <option>4</option>
                  <option>3</option>
                  <option>very-very-very long text, it will be crush the table</option>
                </select>
              </td>
              <td>
                <select>
                  <option>funk-rock/rock/alternative</option>
                  <option>Synthpop/new-wave</option>
                  <option>very-very-very long text, it will be crush the table</option>
                </select>
              </td>
              <td>
                <select>
                  <option>USA</option>
                  <option>UK</option>
                  <option>very-very-very long text, it will be crush the table</option>
                </select>
              </td>
            </tr>
          </tbody>
        </table>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 2023-04-01
      • 1970-01-01
      • 2022-01-09
      • 2014-06-22
      • 2015-02-02
      相关资源
      最近更新 更多