简体   繁体   English

固定表格布局,但一列调整为内容? (仅HTML和CSS)

[英]Fixed table layout, but one column adjusting to content? (HTML and CSS only)

In the snippet below, is it possible to get label column to adjust to its content? 在下面的代码段中,是否有可能使标签列调整为其内容? Currently it receives no width at all :(. 当前,它根本不接收宽度:(。

For this question I am only interested in possible solutions that use HTML tables and CSS, without the help of Javascript libraries. 对于这个问题,我只对使用HTML表格和CSS的可能解决方案感兴趣,而没有Javascript库的帮助。

 table { width: 100%; table-layout: fixed; border-collapse: collapse; } td { border: 1px solid black; margin: 0px; padding: 0px; } input[type='text'] { width: 100%; box-sizing: border-box; } 
 <table> <tr> <td style="width: 100%;"></td> <td style="width: auto;">Label:</td> <td style="width: 5px;"></td> <td style="width: 200px;"><input type='text' /></td> <td style="width: 100%;"></td> </tr> </table> 

Context: I am used to program desktop application grid layouts that consist of growing columns, fixed-width columns and columns that adjust to the preferred size of the content. 上下文:我习惯于对桌面应用程序网格布局进行编程,该布局由不断增长的列,固定宽度的列和调整为内容的首选大小的列组成。 Usually it is also possible to provide resize weights for the growing columns. 通常,还可以为增长中的色谱柱提供调整大小的砝码。

For layout I would suggest using awesome new css feature: css grids . 对于布局,我建议使用真棒的CSS新功能: CSS网格 This way you have way more control over your layout, it's semantically correct, elegant and easier to maintain. 这样,您就可以更好地控制布局,从语义上讲正确,优雅且易于维护。 With css grids you can actually do what you want so much, though using different unit (fr instead of %). 使用css网格,您实际上可以做很多事情,尽管使用不同的单位(fr代替%)。 Don't use table if it's content is not pure data. 如果表的内容不是纯数据,请不要使用。 And if you do use table, I would suggest not messing with columns width too much to make it easier to be responsive. 而且,如果您确实使用表格,我建议不要过多地修改列的宽度,以使其更易于响应。

Css grids are quite new feature and it's actually the first legit way of doing layout. CSS网格是一个相当新的功能,它实际上是进行布局的第一种合法方法。 It may be confused with older, common way of doing layout, like bootstrap grid . 它可能会与较旧的常见布局方式(例如bootstrap grid)相混淆。 These are not the same and quite different! 这些不一样,也很不一样! Pick any way of doing grids you like more, but definitely don't use tables for the layout. 选择任何您更喜欢的网格处理方式,但绝对不要在布局中使用表格。 :) :)

Well, just make sure that sum of all columns width is no more than 100%. 好吧,只要确保所有列宽度的总和不超过100%。 width: auto sets width of whatever space is left, but there's nothing left when other columns takes >100% of width. width: auto设置剩余空间的宽度,但是当其他列的宽度> 100%时,则什么也没有。

Try this: 尝试这个:

<table class="t">
  <tr>
    <td style="width: 40%;">a</td>
    <td style="width: auto;">b</td>
    <td style="width: 50px;">c</td>
    <td style="width: 40%;">d</td>
  </tr>
</table>

If table width is 1000px, then 1% of width is 10px. 如果表格宽度为1000像素,则宽度的1%为10像素。 In that case 40% takes 400px, 50px takes... 50px, and auto takes what's left, which is (1000px - 400px - 400px - 50px) == 150px . 在这种情况下, 40%占用400px, 50px占用... 50px,而auto占用剩余的空间,即(1000px - 400px - 400px - 50px) == 150px

If you had 2 columns with width: auto , then these columns width would be 75px, because 150px (space left) / 2 (number of columns width: auto ) == 75px. 如果您有2列width: auto列,那么这些列的宽度将为75px,因为150px(剩余空间)/ 2(列数的width: auto )== 75px。

If I had 4 columns with widths: 50%, 50%, 50px, auto, then no matter how wide table is, the space left is kind of -50px (which is what makes width: auto look like width: 0 ). 如果我有4个宽度为50%,50%,50px,自动的列,那么无论表格有多宽,剩下的空间都为-50px (这就是使width: auto看起来像width: 0 )的原因。

Make sure your "space left" is more than 0. One column with width: 100% and you're out of it. 确保您的“剩余空间”大于0。一列的width: 100%并且您不在其中。

Looks like CSS grid does exactly what I was looking for: 看起来CSS网格完全符合我的要求:

 .container { width: 100%; display: grid; grid-template-columns: 1fr auto 5px 200px 1fr; border: 1px solid black; border-width: 1px 0 0 1px; // top + left } .container > DIV { border: 1px solid black; border-width: 0 1px 1px 0; // right + bottom } input[type='text'] { width: 100%; } 
 <div class="container"> <div></div> <div>Label:</div> <div></div> <div><input type='text' /></div> <div></div> </div> 

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

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