2017-06-13 95 views
0

我有一个带有内容和按钮的<td>元素。内容的width应该是除了将被修复的按钮的宽度之外的全部。该按钮应该对齐到内容的右侧。我怎样才能做到这一点,下面不工作:对齐td元素右边的按钮

<table border="1px"> 
 
    <tr> 
 
    <td> 
 
     <div> 
 
     <div style="width: auto; overflow: auto;"> 
 
      <span> 
 
      <form> 
 
      <textarea></textarea> 
 
      </form> 
 
      </span> 
 
     </div> 
 
     <div style="float: right; width: 32px;"> 
 
      <button type="button" class="btn btn-primary"> 
 
      Click 
 
      </button> 
 
     </div> 
 
     </div> 
 
    </td> 
 
    <td>Another cell</td> 
 
    </tr> 
 
</table>

回答

0

基于Gerard's answer以下工作最适合我:

<table border="1px"> 
 
    <tr> 
 
    <td> 
 
     <div style="display: inline-flex; align-items: center; width: 100%;"> 
 
     <div> 
 
      <span> 
 
      <form> 
 
      <textarea></textarea> 
 
      </form> 
 
      </span> 
 
     </div> 
 
     <div style="padding: 5px;"> 
 
      <button type="button"> 
 
      Click 
 
      </button> 
 
     </div> 
 
     </div> 
 
    </td> 
 
    <td>Another cell</td> 
 
    </tr> 
 
</table>

1

,你可以降低按键的字体大小,使其适合需要的宽度(32PX)里面你已经设置父DIV

并不真正了解这个网站背后的结构你的逻辑,但这里是您的解决方案

<table border="1px"> 
 
    <tr> 
 
    <td> 
 
     <div> 
 
     <div style="width:calc(100% - 32px);overflow: auto;float:left"> 
 
      <span> 
 
      <form> 
 
      <textarea></textarea> 
 
      </form> 
 
      </span> 
 
     </div> 
 
     <div style="float: right;"> 
 
      <button type="button" class="btn btn-primary" style="width:32px;font-size:8px;" title="Select Financial Instrument"> 
 
      Click 
 
      </button> 
 
     </div> 
 
     </div> 
 
    </td> 
 
    <td>Another cell</td> 
 
    </tr> 
 
</table>

+0

不起作用,您的代码将按钮放在输入字段下方,我希望它在右边。 – Roland

+0

哦。没有正确理解这个问题。认为溢出是问题。使用'calc()'来计算左边的div宽度,'width:calc(100% - 32px)'并且添加float left。改变了我的答案。虽然已经太晚了:) –

1

这是你所需要的?

<table border="1px"> 
 
    <tr> 
 
    <td> 
 
     <div style="display: flex;"> 
 
     <form> 
 
      <textarea></textarea> 
 
     </form> 
 
     <button type="button" class="btn btn-primary" title="Select Financial Instrument"> 
 
      Click 
 
     </button> 
 
     </div> 
 
    </td> 
 
    <td>Another cell</td> 
 
    </tr> 
 
</table>

2

<table border="1px"> 
 
    <tr> 
 
    <td> 
 
     <div style="display:inline-block"> 
 
     <div style="display: inherit;width: calc(100% - 32px);overflow: auto;"> 
 
      <span> 
 
      <form> 
 
      <textarea></textarea> 
 
      </form> 
 
      </span> 
 
     </div> 
 
     <div style="float: right; width: 32px;"> 
 
      <button type="button" class="btn btn-primary" title="Select Financial Instrument" style="width:100%; word-wrap: break-word;"> 
 
      Click 
 
      </button> 
 
     </div> 
 
     </div> 
 
    </td> 
 
    <td>Another cell</td> 
 
    </tr> 
 
</table>

+0

工作很好,谢谢!是否有可能摆脱第二个'div'中的'calc'? – Roland

+0

没问题... calc有什么问题吗?让我知道......我会更新它:) –

+0

'calc'没问题我只想让逻辑尽可能简单,因此避免调用函数。 – Roland

1

您可以使用<table>并放置在一个<td>的形式和其他的按钮。然后你可以修复包含按钮的<td>的宽度。这将迫使第一个<td>根据宽度<table>调整其宽度。

检查此jsFiddle,尝试更改主表的宽度,并查看<textarea>(和<td>)如何相应地调整宽度。

更新:

这样吧,用你的HTML结构没有变化:

* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box }
<table border="1px" style="width: 450px"> 
 
    <tr> 
 
    <td> 
 
     <div style="display: table; width: 100%"> 
 
     <div style="display: table-row"> 
 
      <div style="display: table-cell"> 
 
      <span style="display: block; width: 100%"> 
 
       <form style="display: block; width: 100%"> 
 
       <textarea style="display: block; width: 100%"></textarea> 
 
       </form> 
 
      </span> 
 
      </div><!-- table-cell --> 
 
      <div style="display: table-cell; width: 32px;"> 
 
      <button type="button" class="btn btn-primary"> 
 
       Click 
 
      </button> 
 
      </div><!-- table-cell --> 
 
     </div><!-- table-row --> 
 
     </div><!-- table --> 
 
    </td> 
 
    <td>Another cell</td> 
 
    </tr> 
 
</table>

+0

这是可能的,但我想避免这个解决方案,因为按钮属于这个特定的表格元素的语义。 – Roland