我遇到了一些垂直的CSS定位问题。我想要创建一个具有以下布局的页面:
B AAA
AAA
AAA
C AAA
A是一个数据网格(表格) B对齐到网格的顶部 C对齐到网格的底部
我如何使B和C正确地对齐?
编辑:关于Div / Table标记的混淆,对不起!我希望尽可能使用纯CSS来完成此操作,但如果这需要很多工作,则我将接受dittodhole的解决方案,使用表格。
我遇到了一些垂直的CSS定位问题。我想要创建一个具有以下布局的页面:
B AAA
AAA
AAA
C AAA
A是一个数据网格(表格) B对齐到网格的顶部 C对齐到网格的底部
我如何使B和C正确地对齐?
编辑:关于Div / Table标记的混淆,对不起!我希望尽可能使用纯CSS来完成此操作,但如果这需要很多工作,则我将接受dittodhole的解决方案,使用表格。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css" media="screen">
#wrapper{ position: relative; }
#b, #c{ position: absolute; left: 0; width: 80px; background: #ccc; }
#a{ margin-left: 80px; }
#b{ top: 0; }
#c{ bottom: 0; }
</style>
</head>
<body>
<div id="wrapper">
<div id="b">Contents of B</div>
<table border="0" id="a">
<tr><th>Header</th><th>Header</th><th>Header</th></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
</table>
<div id="c">
Contents of C
</div>
</div>
</body>
</html>
为什么感觉这么靠不住 - 这其实很简单:
<table>
<tr>
<td style="vertical-align: top;">B</td>
<td rowspan="2">grid</td>
</tr>
<tr>
<td style="vertical-align: bottom;">C</td>
</tr>
</table>
我找不到任何解决这个问题的方法适用于我。我找到的最接近的解决方案只适用于只有1行的情况。我正在使用ASP.NET日历控件,并希望在每个日期单元格底部放置一个div。我想出的解决方案是让JQuery猜测最大单元格的高度,并分别设置底部div的margin-top以使其正常工作。locationInfo是要移动到每个单元格底部的内容。num_events查找上面列出的条目数。
var line_height = 24; // line height plus padding
$(document).ready(function () {
// find out the most events in one day
var most_events = 0;
$(".locationInfo").each(function () {
// get number of events in this cell
var parent_td = $(this).parent();
var num_events = parent_td.children().length - 2;
if (num_events > most_events) {
most_events = num_events;
}
});
//alert(most_events);
// loop through each (location info) div and reposition by setting margin-top
// this could be improved further by finding the most_events for each week instead of the whole month
$(".locationInfo").each(function () {
//$(this).css("top", 50);
parent_td = $(this).parent();
num_events = parent_td.children().length - 2;
$(this).css("margin-top", (5 + (most_events * line_height)) - (num_events * line_height));
});
});
这是解决方案:
<style type="text/css">
table { width:500px; border-collapse:collapse}
th, td { border:1px solid black; vertical-align: top;}
th { width:100px; }
td { background:#ccc; }
.wrap { position:relative; height:100%; padding-bottom:1em; background:#aaa; height:200px;}
.manage { text-align:right; position:absolute; bottom:0; right:0; }
p{ margin: 0 0 5px 0; }
</style>
<table>
<tr>
<th>Mauris tortor nulla, sagittis ut, faucibus eu, imperdiet ut, libero.</th>
<td><div class="wrap">
<p><a href="http://www.pronexo.com">www.pronexo.com</a></p>
<div class="manage">Edit | Delete</div></div></td>
</tr>
<tr>
<th>Cras diam.</th>
<td><div class="wrap"><p>Mauris tortor nulla, sagittis ut, faucibus eu, imperdiet ut, libero. Sed elementum. Praesent porta, tellus ut dictum ullamcorper, est ante condimentum metus, non molestie lorem turpis in sapien. Aenean id enim. Nullam placerat blandit ante. Aenean ac ligula.</p><div class="manage">Edit | Delete</div></div></td>
</tr>
</table>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css" media="screen">
#wrapper{ position: relative; border: 1px solid #fff; }
#b, #c{ position: absolute; left: 0; background: #ccc; margin: 0; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" charset="utf-8">
$(function(){
var b_width = $('#b').width();
var a_height = $('#a').height();
var c_height = $('#c').height();
var c_top = a_height - c_height;
$('#a').css({"margin-left": b_width + "px"});
$('#c').css({top: c_top + "px"});
})
</script>
</head>
<body id="">
<div id="wrapper">
<div id="b">Contents of B</div>
<table border="0" id="a">
<tr><th>Header</th><th>Header</th><th>Header</th></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td><td>Data</td></tr>
</table>
<div id="c">
Contents of C
</div>
</div>
</body>
</html>