我有四个div,每个div都有一个包含许多行的列表。它们都是相对定位的左侧浮动,因此都是连续的。我想在div“容器”中垂直居中。我怎么能这样做?
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<style type="text/css">
.titlefont{
font-family:"Lucida Console", Monaco, monospace;
}
div#saucelistdiv{
float:left;
width:auto;
margin:10px;
vertical-align:middle;
height:auto;
}
div#meatlistdiv{
float:left;
width:auto;
margin:10px;
vertical-align:middle;
height:auto;
}
div#container{
height:600px;
border:2px solid red;
width:auto;
}
#grainlistdiv{
float:left;
width:auto;
margin:10px;
vertical-align:middle;
height:auto;
}
div#veggielistdiv{
float:left;
width:auto;
margin:10px;
vertical-align:middle;
height:auto;
}
#tablelist{
border:2px solid;
}
</style>
<body>
<center>
<div id="container">
<div id="saucelistdiv"><table id="tablelist">
<span class="titlefont">SAUCES</span>
<tr><td><input type="radio" name="rbbase" value="Pizza Sauce" checked="checked" />Pizza Sauce</td></tr>
<tr><td><input type="radio" name="rbbase" value="Cheese Sauce" />Cheese Sauce</td></tr>
<tr><td><input type="radio" name="rbbase" value="Barbecue Sauce" />Barbecue Sauce</td></tr>
<tr><td><input type="radio" name="rbbase" value="Salsa" />Salsa</td></tr>
<tr><td><input type="radio" name="rbbase" value="Alfredo Sauce" />Alfredo Sauce</td></tr>
<tr><td><input type="radio" name="rbbase" value="Taco Sauce" />Taco Sauce</td></tr>
<tr><td><input type="radio" name="rbbase" value="Tzatziki (Cucumber) Sauce" />Tzatziki (Cucumber) Sauce</td></tr>
<tr><td><input type="radio" name="rbbase" value="Chicken Gravy" />Chicken Gravy</td></tr>
<tr><td><input type="radio" name="rbbase" value="Beef Gravy" />Beef Gravy</td></tr>
<tr><td><input type="radio" name="rbbase" value="Ranch Dressing" />Ranch Dressing</td></tr>
<tr><td><input type="radio" name="rbbase" value="1000 Island Dressing" />1000 Island Dressing</td></tr>
<tr><td><input type="radio" name="rbbase" value="Parmesan Peppercorn Dressing" />Parmesan Peppercorn Dressing</td></tr>
<tr><td><input type="radio" name="rbbase" value="Spaghetti Sauce" />Spaghetti Sauce</td></tr>
<tr><td><input type="radio" name="rbbase" value="Chili" />Chili</td></tr>
<tr><td><input type="radio" name="rbbase" value="Syrup" />Syrup</td></tr>
<tr><td><input type="radio" name="rbbase" value="Garlic Butter" />Garlic Butter</td></tr>
<tr><td><input type="radio" name="rbbase" value="Mayonnaise" />Mayonnaise</td></tr>
</table>
</div>
<div id="meatlistdiv">
<span class="titlefont">MEATS</span>
<table class="center" id="tablelist">
<tr><td><input type="checkbox" name="cbing" value="Sausage" />Sausage<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Pepperoni" />Pepperoni<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Diced Ham" />Diced Ham<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Beef" />Beef<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Bacon" />Bacon<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Lamb" />Lamb<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Bratwurst" />Bratwurst<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Chicken" />Chicken<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Shrimp" />Shrimp<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Crab" />Crab<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Seasoned Beef" />Seasoned Beef<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Corned Beef" />Corned Beef<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Turkey" />Turkey<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Eggs" />Eggs<br /></td></tr>
</table>
</div>
<div id="grainlistdiv">
<span class="titlefont">GRAINS</span>
<table class="center" id="tablelist">
<tr><td><input type="checkbox" name="cbing" value="Elbow Macaroni" />Elbow Macaroni<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Spaghetti Noodles" />Spaghetti Noodles<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Oyster Crackers" />Oyster Crackers<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Goldfish" />Goldfish<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Mini Pancakes" />Mini Pancakes<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Pretzels" />Pretzels<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Stuffing" />Stuffing<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Taco Chips" />Taco Chips<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Ramen Noodles" />Ramen Noodles<br /></td></tr>
</table>
</div>
<div id="veggielistdiv">
<span class="titlefont">VEGGIES/OTHER</span>
<table class="center" id="tablelist">
<tr><td><input type="checkbox" name="cbing" value="Lettuce" />Lettuce<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Tomatoes" />Tomatoes<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Onions" />Onions<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Green Peppers" />Green Peppers<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Green Olives" />Green Olives<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Black Olives" />Black Olives<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Pineapple" />Pineapple<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Sauerkraut" />Sauerkraut<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Broccoli" />Broccoli<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Cauliflower" />Cauliflower<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Jalapeno Peppers" />Jalapeno Peppers<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Pickles" />Pickles<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Cucumbers" />Cucumbers<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Carrots" />Carrots<br /></td></tr>
<tr><td><input type="checkbox" name="cbing" value="Mushrooms" />Mushrooms<br /></td></tr>
</table>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
垂直对齐仅适用于表格。所以你可以使用div作为表, 用这种方式:
div#container
{
height: 600px;
border: 2px solid red;
width: auto;
//add these rows
display: table-cell;
vertical-align: middle;
}
答案 1 :(得分:0)
我相信如果您将float:left;
更改为display:inline-block;
,那么您将获得您正在寻找的效果。
答案 2 :(得分:0)
您也可以使用一些javascript(这使用JQuery)来实现这一点:
(document).ready( function () {
$("#container").children().each( function () {
var parentHeight = $(this).parent().height();
var myHeight = $(this).height();
$(this).css('top', (parentHeight - myHeight) / 2);
});
});
答案 3 :(得分:0)
如果你给div#container一个宽度大小并将margin设置为auto,它应该可以工作:
div#container{
height:600px;
border:2px solid red;
width:700px;
margin:auto;
}
*编辑 - 好吧,我不确定我是否真的理解了这个问题,但确实让它看起来不错: - )