简体   繁体   English

HTML 按钮填充剩余宽度

[英]HTML button filling remaining width

I am not a HTML expert, just doing some fun coding once in a while.我不是 HTML 专家,只是偶尔做一些有趣的编码。 What I try to accomplish is to have the button in the "td" filling the remaining width, as simple as possible.我试图完成的是让“td”中的按钮填充剩余的宽度,尽可能简单。

<table width="100%">
<tr>
  <td>My Text</td>
  <td>
    <input name="x" id="y" type="text" size="4" maxlength="4" />
    <input type="button" onclick="..."  value="BlaBla" />
  </td>
</tr>
<tr>
  <td>Other Text</td>
  <td>
    <input name="xx" id="yy" type="text" size="20" />
    <input type="button" onclick="..."  value="MoreBlaBla" />
  </td>
</tr>
</table>

I have tried width 100%, but this gives me an extra line.我试过宽度 100%,但这给了我一条额外的线。 I have checked some answers such as Help with div - make div fit the remaining width here, but since I am not an expert in HTML it is getting too complex.我已经检查了一些答案,例如Help with div - make div fit the remaining width here,但由于我不是 HTML 方面的专家,所以它变得太复杂了。 Guess there is a very simple solution.猜猜有一个非常简单的解决方案。

Try this尝试这个

<td>
                <input name="x" id="y" type="text" style="float:left;" size="4" maxlength="4" />
                <input type="button" onclick="..." style="float:left;width:70%"  value="BlaBla" />
    </td>

Basically, there is no way to specify this using HTML+CSS.基本上,没有办法使用 HTML+CSS 来指定它。 At some point pretty much everyone has wanted this, and it doesn't exist.在某些时候,几乎每个人都想要这个,但它并不存在。

If you are sticking with HTML+CSS, if you have widths specified in some predictable way (either fixed lengths, or percentages), then you can calculate the right percentage (or other measure) to set for the button width.如果您坚持使用 HTML+CSS,如果您以某种可预测的方式指定宽度(固定长度或百分比),那么您可以计算正确的百分比(或其他度量)来设置按钮宽度。 This is probably the best way.这可能是最好的方法。

If that is impossible, your next choice is javascript (which you should enhance with at least one of the many libraries that exist now to make javascript so much easier to use).如果这是不可能的,那么您的下一个选择是 javascript(您应该使用现在存在的众多库中的至少一个来增强它,以使 javascript 更易于使用)。

Try this:尝试这个:

<tr>
<td>My Text</td>
<td>
    <input name="x" id="y" type="text" size="4" style="float:left;width:100px" maxlength="4" />
    <div style="padding-left:100px"><input type="button" style="width:100%" value="BlaBla" /></div>
</td>
</tr>

Well it can be done with JavaScript but it doesn't look that great.好吧,它可以用 JavaScript 完成,但它看起来并不那么好。

A right-aligned, fixed width button looks better IMHO.恕我直言,右对齐、固定宽度的按钮看起来更好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM