具有流体宽度的 CSS 表格布局
我一直在寻找一种用 CSS 生成以下 HTML 表格的方法:
<style type="text/css">
table.frm tr td { vertical-align: top; padding-right: 10px; }
</style>
<table class="frm">
<tr>
<td rowspan="2">Label 1:</td>
<td><input type="text" /></td>
<td rowspan="2">Label 2:</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Validation Message 1</td>
<td>Validation Message 2</td>
</tr>
<tr>
<td rowspan="2">Label 3:</td>
<td><input type="text" /></td>
<td rowspan="2">Label 4:</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Validation Message 3</td>
<td>Validation Message 4</td>
</tr>
</table>
当我尝试用 div 替换它时遇到的问题是我无法对齐两个列和行。我尝试使用浮动来对齐列,但后来我失去了行的垂直对齐方式。但是,如果我使用清除来对齐行,我就会失去列的水平对齐方式。
我见过的许多将表格转换为 div 的示例都使用固定宽度或百分比宽度,但我希望布局具有与表格相同的流畅行为,因为验证消息可能会或可能不会出现,并且标签/字段将具有不同的宽度尺寸。
有没有设计师可以向我展示如何在没有表格的情况下实现这种布局?
I've been looking for a way to produce the following HTML table with CSS:
<style type="text/css">
table.frm tr td { vertical-align: top; padding-right: 10px; }
</style>
<table class="frm">
<tr>
<td rowspan="2">Label 1:</td>
<td><input type="text" /></td>
<td rowspan="2">Label 2:</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Validation Message 1</td>
<td>Validation Message 2</td>
</tr>
<tr>
<td rowspan="2">Label 3:</td>
<td><input type="text" /></td>
<td rowspan="2">Label 4:</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Validation Message 3</td>
<td>Validation Message 4</td>
</tr>
</table>
The problem I've been having when I try to replace this with divs is that I can't align both the columns and the rows. I've tried using floats to align the columns, but then I loose the vertical alignment of the rows. But if I use a clear to align the rows, I loose the horizontal alignment of the columns.
Many of the examples I've seen for converting tables to divs use fixed or percentage widths, but I want the layout to have the same fluid behavior of the table since the validation messages may or may not appear and the labels/fields will have varying sizes.
Is there a designer out there who can show me how this layout can be achieved without tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是
display
(CSS2) 的问题,但它需要 IE7+。请参阅此小提琴示例:标记:
样式表:
This is not a problem with
display
(CSS2), but it requires IE7+. Please see this example fiddle:Markup:
Style sheet:
您面临的问题是您将每个单元格视为自己的小数据块。你需要着眼于大局。那个数据集合是什么?它实际上是一个事物列表吗?它属于 ul 还是 dl?
当它根本不是一个表格时,不要试图让它看起来像一个具有不同元素的表格。并且表格不应该用于布局。
The problem you're facing is you are looking at each cell as its own little block of data. You need to look at the bigger picture. What is that collection of data? Is it actually a list of things? Does it belong in a ul or dl?
Quit trying to make this look like a table with different elements when it's not a table at all. And tables should never be used for layout.
流体是一个非常模糊的术语,流体是指它的宽度可以像表格在不同尺寸的屏幕上一样扩展和收缩,或者当浏览器窗口调整大小时?或者您是否希望将列宽全部调整为与具有最长/最大内容的单元格相同的宽度?
第一个是这样完成的
,它应该模拟两行,每行两列,宽度基于屏幕尺寸。
第二个是根据内容大小调整列。
即两列,每列两行。将“最小宽度”和“最大宽度”设置为您需要的任何值。我认为我没有理由测试这样的东西,所以如果它不起作用,你可以尝试为每一行设置“display: inline”。
如果这些都不起作用,请尝试发布您的测试代码。我确信你可以用 div 制作表格。几年前,在没有使用表格之后,我无意中创建了一个网站,该网站应该使用 div 中的表格。你想要做的事情都可以完成,只需使用 CSS 即可。
Fluid is very vague term, by fluid do you mean its width can expand and contract like a table does for different size screens, or when the browser window is resized? Or do you want he column widths to all adjust to same width as the cell with the longest/largest content?
The first is done like so
That should simulate two rows with two columns in each row and widths are based on the screen size.
The second with the columns adjusting based on the content size.
That is two columns with with two rows each. Set the "min-width" and "max-width" to whatever you need. I don't think I have ever had a reason to test anything like this, so if it doesn't work you could try setting "display: inline" for each row.
And If neither of these work try posting your test code. I am certain you can make a tables out of divs. A while back after several years of not using tables I inadvertently made a site, that should have used tables, out of divs. What your looking to do can be done it is just a matter of working the CSS.