2016-10-14 128 views
1

等宽按钮我有一个(简化的)HTML结构是这样的:填满一个表格单元格完全与任何数量的在CSS

<div class="table"> 
    <form class="tr"> 
     <span class="td"> 
      <input type="submit" value="Button 1"/> 
      <input type="submit" value="Button 2"/> 
      <input type="submit" value="Button 3"/> 
     </span> 
    </form> 
</div> 

的提交按钮内部产生动态服务器端与JSP,所以他们的人数可能会有所不同,通常在1-3。

这里的简化CSS我迄今为止:

.table { 
    display: table; 
} 
.table .tr { 
    display: table-row; 
} 
.table .tr .td { 
    display: table-cell; 
    padding: 5px; 
    border: 1px solid black; 
} 
.table .tr .td input[type=submit] { 
    width: 100%; 
} 

然而,设置的按钮width=100%使得每个按钮一样宽的细胞,使它们竖直地包裹和显示彼此相邻。

我想要的是扩大所有按钮的宽度,以便填充它们包含的单元格,但一个单元格中的所有按钮应具有相同的宽度。这必须适用于每个单元的不同数量的按钮,我想要一个不依赖于JavaScript的解决方案。

这里是我怎么想它看起来另一个可视化:

+----------------------------------------------+ 
| [ Button 1 ] [  B2  ] [ Btn. 3 ] | 
+----------------------------------------------+ 
| [  Button A  ] [  Button B  ] | 
+----------------------------------------------+ 
| [   only one wide button   ] | 
+----------------------------------------------+ 
+0

多少,每行MAX? –

+0

@AllDani正如我所说,在1-3范围内。不知道我是否可能需要在未来添加更多。 –

+0

Flexbox是唯一想到的,但JavaScript有更好的跨浏览器兼容性和支持:http://caniuse.com/#feat=flexbox ** VS ** http://caniuse.com/#search=javascript –

回答

3

这是一个flexbox一个完美的情况下 - 适用display: flextd

.table .tr .td { 
    display: flex; 
} 

让我知道您的意见在这。谢谢!

.table { 
 
    display: table; 
 
} 
 
.table .tr { 
 
    display: table-row; 
 
} 
 
.table .tr .td { 
 
    display: flex; 
 
    padding: 5px; 
 
    border: 1px solid black; 
 
} 
 
.table .tr .td input[type=submit] { 
 
    width: 100%; 
 
}
<div class="table"> 
 
    <form class="tr"> 
 
    <span class="td"> 
 
      <input type="submit" value="Button 1"/> 
 
      <input type="submit" value="Button 2"/> 
 
      <input type="submit" value="Button 3"/> 
 
     </span> 
 
    <span class="td"> 
 
      <input type="submit" value="Button 1"/> 
 
      <input type="submit" value="Button 2"/> 
 
     </span> 
 
    <span class="td"> 
 
      <input type="submit" value="Button 1"/> 
 
     </span> 
 
    </form> 
 
</div>

+1

您的解决方案没有直接工作,因为我需要我的'.td'元素具有'display:table-cell',否则所有单元格将垂直对齐而不是行和列。不过,你让我走上了正轨。我现在将每个'.td'元素的内容封装在一个' ...'中,而不是直接更改'.td'元素的'display'样式。现在按预期工作。 –

+0

很酷...很高兴我可以帮助:) – kukkuz

相关问题