现代网页设计的一个棘手问题是创建一个所有列高度相等的两列(或更多)布局。我开始了一项任务,寻找一种在纯CSS中实现此目的的方法。
您可以通过在容纳两列(或页面背景)的包装div中使用背景图像来最轻松地实现此目的。
您也可以使用CSS表单元格来实现此操作,但不幸的是,对于此的浏览器支持仍然有些模糊,因此这不是首选解决方案。继续阅读,有更好的方式。
我从网络上的两个页面中得到灵感,尽管我更喜欢我的解决方案,因为它使我更自由地使用圆角和精确的宽度或百分比布局,并且更容易编辑,您的最终布局包含的div不会强制要求您进行负数计算。
== 技巧:==
首先创建背景设计列,然后放置可以容纳常规内容的全宽div。技巧就在于,当内容长度扩展时,在所有父列中创建漂浮列,无论哪个结束列最长,都可以产生推动效果。
在此示例中,我将在带有圆角的居中包装div中使用2列网格。我尝试保持简洁明了,以便轻松复制粘贴。
== 第1步==
创建基本网页。
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
</body>
</html>
== 第二步 ==
在一个浮动的div中创建另一个浮动的div。然后在内部div上应用负边距以使其在视觉上弹出框架。我添加了虚线边框以说明目的。请注意,如果您将外部div向左浮动并给内部div一个向左的负边距,则内部div将在不给您滚动条的情况下进入页面边缘以下。
<!DOCTYPE HTML>
<html>
<head>
<style>
#rightsideBG{
float:right;
background:silver;
width:300px;
border: 3px dotted silver;
}
#leftsideBG{
float:left;
background:gold;
width:100px;
margin-left:-100px;
border: 3px dotted gold;
}
</style>
</head>
<body>
<div id="rightsideBG">
<div id="leftsideBG">
this content obviously only fits the left column for now.
</div>
</div>
</body>
</html>
== 步骤 3 ==
在内部 div 中:创建一个没有背景的 div,它的宽度为所有列的宽度之和。它将推动超出内部 div 的边缘。我添加了虚线边框以说明目的。这将成为您内容的画布。
<!DOCTYPE HTML>
<html>
<head>
<style>
#rightsideBG{
float:right;
background:silver;
width:300px;
border: 3px dotted silver;
}
#leftsideBG{
float:left;
background:gold;
width:100px;
margin-left:-100px;
border: 3px dotted gold;
}
#overbothsides{
float:left;
width:400px;
border: 3px dotted black;
}
</style>
</head>
<body>
<div id="rightsideBG">
<div id="leftsideBG">
<div id="overbothsides">
this content spans over both columns now.
</div>
</div>
</div>
</body>
</html>
== 第四步 ==
添加您的内容。在这个例子中,我放置了两个div,它们位于布局之上。我还去掉了虚线边框。就是这样简单。如果您喜欢,可以使用这段代码。
<!DOCTYPE HTML>
<html>
<head>
<style>
#rightsideBG{
float:right;
background:silver;
width:300px;
}
#leftsideBG{
float:left;
background:gold;
width:100px;
margin-left:-100px;
}
#overbothsides{
float:left;
width:400px;
}
#leftcol{
float:left;
width:80px;
padding: 10px;
}
#rightcol{
float:left;
width:280px;
padding: 10px;
}
</style>
</head>
<body>
<div id="rightsideBG">
<div id="leftsideBG">
<div id="overbothsides">
<div id="leftcol">left column content</div>
<div id="rightcol">right column content</div>
</div>
</div>
</div>
</body>
</html>
== 步骤 5 ==
为了使它更美观,您可以将整个设计居中放置在一个包装 div 中,并给它圆角。除非您使用特殊的修复程序,否则旧版 IE 不会显示圆角。
<!DOCTYPE HTML>
<html>
<head>
<style>
#wrap{
position:relative;
width:500px;
margin:20px auto;
-webkit-border-bottom-right-radius: 20px;
-moz-border-radius-bottomright: 20px;
border-bottom-right-radius: 20px;
}
#rightsideBG{
float:right;
background:silver;
width:300px;
}
#leftsideBG{
float:left;
background:gold;
width:100px;
margin-left:-100px;
}
#overbothsides{
float:left;
width:400px;
}
#leftcol{
float:left;
width:80px;
padding: 10px;
}
#rightcol{
float:left;
width:280px;
padding: 10px;
}
</style>
</head>
<body>
<div id="wrap">
<div id="rightsideBG">
<div id="leftsideBG">
<div id="overbothsides">
<div id="leftcol">left column content</div>
<div id="rightcol">right column content</div>
</div>
</div>
</div>
</div>
</body>
</html>
== 灵感来源 ==