Equal-height scaling cells in an html table

做~自己de王妃 提交于 2019-12-23 10:42:15

问题


I'm having some trouble with HTML tables when I make this design: The left-hand cell is a rowspan="2" cell, and the right two are using height="50%" attributes. Below is the expected behavior:

    +-------------+-----------------+
    |             |                 |
    |             |   Equal-height  |
    |             |   cell #1       |
    |             |                 |
    | Scaling-    +-----------------+
    | height cell |                 |
    |             |   Equal-height  |
    |             |   cell #2       |
    |             |                 |
    +-------------+-----------------+

What actually happens:

    +-------------+-----------------+
    |             |   Equal-height  |
    |             |   cell #1       |
    |             +-----------------+
    |             |                 |
    | Scaling-    |                 |
    | height cell |   Equal-height  |
    |             |   cell #2       |
    |             |                 |
    |             |                 |
    +-------------+-----------------+

In short, the top of the right-hand two cells is reduced as small as possible, and the bottom one fills the rest of the space. There is an ugly workaround using embedded tables, but I was wondering if there was a more elegant solution. This can also be circumvented by assuming a fixed height for the left-hand cell, and forcing the size (in pixels) for the right-hand cells. This defeats the purpose of a scaling-height cell, though.


回答1:


Really late...how about using javascript as follows where height is a number you set?

function SetCellHeight(height) {
    document.getElementById('ReferenceCell').style.height = height + 'px';
    document.getElementById('Cell1').style.height = (0.5 * height) + 'px';     
    document.getElementById('Cell2').style.height = (0.5 * height) + 'px';
}

<body onload="SetCellHeight(height)">

<table border=1>
<tr>
<td id="ReferenceCell" rowspan="2">First Column</td>
<td id="Cell1">Cell #1</td>
</tr>
<tr>
<td id="Cell2">Cell #2</td>
</tr>
</table>

Alternatively, the user can set height as follows:

function SetCellHeight() {
    var height = document.forms.tdHeightForm.heightinput.value.trim();
    document.getElementById('ReferenceCell').style.height = height + 'px';
    document.getElementById('Cell1').style.height = (0.5 * height) + 'px';     
    document.getElementById('Cell2').style.height = (0.5 * height) + 'px';
}

<form id="tdHeightForm"><input type="text" name="heightinput"><button type="button" 
onclick="SetCellHeight()">Change Height</button></form>



回答2:


To use the height:50% tag, the table needed to have a height. Again, the problem was to due with the element on the left being arbitrarily-sized. So, a fixed height (say 150px) might be a problem if the text on the left was less than 150 px. Thus, setting a 0px height table solved this problem.

<table height="0px" border="1px">
  <tr>
    <td rowspan="2">
      Blah<br>
      Blah<br>
      Blah<br>
      Blah<br>
      Blah<br>
      Blah<br>
      Blah<br>
      Blah<br>
    </td>
    <td height="50%">
      Text
    </td>
  </tr>
  <tr>
    <td height="50%">
      More text
    </td>
  </tr>
</table>



回答3:


<table border=1>
<tr>
<td rowspan="2" style="height:300px;width:100px;text-align:center">First Column</td>
<td style="height: 150px;width:100px;text-align:center">Cell #1</td>
</tr>
<tr>
<td style="height:150px;width:100px;text-align:center">Cell #2</td>
</tr>
</table>


来源:https://stackoverflow.com/questions/15056338/equal-height-scaling-cells-in-an-html-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!