好的,一两周前我遇到了一个简单的布局问题。也就是说,页面的各个部分需要一个标题:
+---------------------------------------------------------+
| Title Button |
+---------------------------------------------------------+
很简单的东西。问题是表格仇恨似乎已经在网络世界中占据了主导地位,当我问 为什么对 HTML 表单使用定义列表(DL,DD,DT)标记而不是表格?的时候,我想起了这一点。现在,表格 vs div/CSS 的一般主题已经被讨论过了,例如:
因此,本文不打算对 CSS 与布局表进行一般性讨论。这只是一个问题的解决方案。我使用 CSS 尝试了以上各种解决方案,包括:
由于各种原因,这些解决方案都不令人满意。例如,相对定位导致 z-index 问题,我的下拉菜单出现在内容下面。
所以我又回到了:
<style type="text/css">
.group-header { background-color: yellow; width: 100%; }
.group-header td { padding: 8px; }
.group-title { text-align: left; font-weight: bold; }
.group-buttons { text-align: right; }
</style>
<table class="group-header">
<tr>
<td class="group-title">Title</td>
<td class="group-buttons"><input type="button" name="Button"></td>
</tr>
</table>
而且效果很好。它很简单,尽可能向后兼容(甚至可能在 IE5上也能正常工作) ,而且它正常工作。不要搞定位或者花车。
那么,没有表的情况下,有人能做到这一点吗?
要求如下:
另外,我今天偶然发现了几篇有趣的文章:
编辑: 让我来详细解释一下浮动的问题,这类工作:
<html>
<head>
<title>Layout</title>
<style type="text/css">
.group-header, .group-content { width: 500px; margin: 0 auto; }
.group-header { border: 1px solid red; background: yellow; overflow: hidden; }
.group-content { border: 1px solid black; background: #DDD; }
.group-title { float: left; padding: 8px; }
.group-buttons { float: right; padding: 8px; }
</style>
</head>
<body>
<div class="group-header">
<div class="group-title">This is my title</div>
<div class="group-buttons"><input type="button" value="Collapse"></div>
</div>
<div class="group-content">
<p>And it works perfectly. It's simple, as backward compatibile as it gets (that'll work probably even on IE5) and it just works. No messing about with positioning or floats.</p>
<p>So can anyone do the equivalent without tables that is backwards compatible to at least FF2 and IE6?</p>
<p>On a side note, I came across a couple of interesting articles today:</p>
</div>
</body>
</html>
感谢 蚂蚁 P的 overflow: hidden
部分(仍然不明白为什么)。这就是问题所在。假设我希望标题和按钮垂直居中。这是有问题的,因为元素的高度不同。比较一下:
<html>
<head>
<title>Layout</title>
<style type="text/css">
.group-header, .group-content { width: 500px; margin: 0 auto; }
.group-header { border: 1px solid red; background: yellow; overflow: hidden; }
.group-content { border: 1px solid black; background: #DDD; }
.group-header td { vertical-align: middle; }
.group-title { padding: 8px; }
.group-buttons { text-align: right; }
</style>
</head>
<body>
<table class="group-header">
<tr>
<td class="group-title">This is my title</td>
<td class="group-buttons"><input type="button" value="Collapse"></td>
</tr>
</table>
<div class="group-content">
<p>And it works perfectly. It's simple, as backward compatibile as it gets (that'll work probably even on IE5) and it just works. No messing about with positioning or floats.</p>
<p>So can anyone do the equivalent without tables that is backwards compatible to at least FF2 and IE6?</p>
<p>On a side note, I came across a couple of interesting articles today:</p>
</div>
</body>
</html>
效果非常好。