Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

html - Make <select> in table cell occupy entire row height

Is it possible to put a drop-down menu in a table cell, and have the menu (when not dropped down) occupy the entire height of the cell? Using just html & css?

I want the user to be able to click anywhere in the cell to activate the drop-down.

The row heights vary unpredictably, so hard-coding a constant height isn't possible.

Below is a demo .html file. Here's what I get in Firefox 84.0. I want the drop-down in the second row to occupy the full height of the row.

enter image description here

!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>
question from:https://stackoverflow.com/questions/65557564/make-select-in-table-cell-occupy-entire-row-height

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about something like this. I have added some css and 100% width height to the select. I have only added to the second select, but you can make that to the css to apply it for all select.

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...