【问题标题】:Make <select> in table cell occupy entire row height使表格单元格中的 <select> 占据整个行高
【发布时间】:2021-01-04 13:43:50
【问题描述】:

是否可以在表格单元格中放置下拉菜单,并让菜单(未下拉时)占据单元格的整个高度?只使用 html 和 css?

我希望用户能够单击单元格中的任意位置以激活下拉菜单。

行高变化不可预测,因此不可能硬编码一个恒定的高度。

下面是一个演示 .html 文件。这是我在 Firefox 84.0 中得到的。我希望第二行的下拉菜单占据该行的整个高度。

!DOCTYPE html>
<html lang="en-us">
<head> <meta charset="UTF-8">
<style>
table {
  border: 3px solid lightblue;
  border-collapse: collapse;
  padding: 0;
  background-color: lightgrey;
}
th, td { 
  padding: 1px;
  border: 1px solid red;
  font-size: small;
} 
select {
  border: 0;
}
</style>
</head>

<body>
<table>
  <tr>
    <td>Short and to the point.</td>
    <td>
      <select> <option>Apple</option> <option>Orange</option> </select>
    </td>
  </tr>
  <tr>
    <td>A multi-line (or wrapped)<br />message<br />makes this row higher</td>   
    <td>
      <select> <option>Apple</option> <option>Orange</option> </select>
    </td>
  </tr>
</table>
</body>
</html>

【问题讨论】:

    标签: html css


    【解决方案1】:

    这样的事情怎么样。我在选择中添加了一些 CSS 和 100% 宽度高度。我只添加了第二个选择,但您可以将其添加到 css 以将其应用于所有选择。

    table {
        width: 100%;
        border: solid 1px lightblue;
        border-collapse: collapse;
        height: 100%;
    }
    
    table th td {
        padding: 1px;
        border: 1px solid red;
        font-size: small;
    }
    
    table td {
        border: solid 1px lightblue;
        height: 100%;
    }
    
    select {
        border: 0;
        width: 100%;
        height: 100%;
    }
    <table>
      <tr>
        <td>Short and to the point.</td>
        <td>
          <select> <option>Apple</option> <option>Orange</option> </select>
        </td>
      </tr>
      <tr>
        <td>A multi-line (or wrapped)<br />message<br />makes this row higher</td>   
        <td>
          <select> <option>Apple</option> <option>Orange</option> </select>
        </td>
      </tr>
    </table>

    【讨论】:

    • 您可能需要检查浏览器兼容性——width: 100%; 和 height: 100%; 在 Firefox 中显示元素大小没有变化。
    • @karolus 现在检查火狐。稍微改变了css部分。
    • @A Paul:我做了更多的测试,并在编辑中进行了修改——这是工作代码。
    • @A Paul:您能批准建议的编辑吗?我通过将 height: 100% 添加到包含元素中找到了解决方法,并通过将内联 CSS 移动到 head 部分来清理代码。
    • 问题:似乎使下拉填充行高的关键更改是 table { height:100%; ...} 但是为什么这能解决问题呢?表格高度:100% 是否意味着使整个表格(所有行组合)与封闭对象的高度匹配(在这种情况下为 )?
    猜你喜欢
    • 1970-01-01
    • 2017-08-03
    • 2020-12-02
    • 1970-01-01
    • 2011-04-27
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多