Quantcast
Channel: 牛魔王的世界观 » css
Viewing all articles
Browse latest Browse all 7

如何实现三列等高布局(纯CSS)

$
0
0

在w3c上看到一个很不错的帖子,纯css方法实现div三列等高,作者的思路确实很不错,特收录在此,希望做前端开发的朋友遇到此问题能够用到。

DIV等高布局,我想很多人都遇见过,我也看过不少的方法,有的是通过背景图片,实现假象的等高效果;还有的用js实现等等。这些都是方法,但是现在都不用以上的方法,来个真正的纯css实现等高!

现在我们来看看真正的 CSS 实现的等高布局,其方法主要是采用“隐藏容器溢出”、“正内补丁”和“负外补丁”结合的方法实现的。 下面来看看实际的例子(三列等高),以下面的 XHTML 代码为例——以上内容来自web前端开发网,W3C

其中起着关键作用的代码已经高亮显示,大家可以删除高亮部分的css代码查看不同的演示效果。

HTML代码部分:

<body>
<div id=”wrap”>
<div id=”left”>
<p style=”height:500px”>style=”height:500px”</p>
</div>
<div id=”center”>
<p style=”height:600px”>style=”height:600px”</p>
</div>
<div id=”right”>
<p style=”height:700px”>style=”height:700px”</p>
</div>
</div>
</body>

CSS代码部分:

<style type=”text/css”>
* {
margin:0;
padding:0;
}
#wrap {
overflow:hidden;
width:1000px;
margin:0 auto;
}
#left, #center, #right {
margin-bottom:-10000px;
padding-bottom:10000px;
}
#left {
float:left;
width:250px;
background:#777;
}
#center {
float:left;
width:500px;
background:#888;
}
#right {
float:right;
width:250px;
background:#999;
}
p {color:#FFF;text-align:center}
</style>

HTML代码在线运行:

<!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>CSS等高布局</title>
<style type="text/css">
* {
	margin:0;
	padding:0;
}
#wrap {
	overflow:hidden;
	width:1000px;
	margin:0 auto;
}
#left, #center, #right {
	margin-bottom:-10000px;
	padding-bottom:10000px;
}
#left {
	float:left;
	width:250px;
	background:#777;
}
#center {
	float:left;
	width:500px;
	background:#888;
}
#right {
	float:right;
	width:250px;
	background:#999;
}
p {color:#FFF;text-align:center}
</style>
</head>
<body>
<div id="wrap">
	<div id="left">
		<p style="height:500px">
			style="height:500px"
		</p>
	</div>
	<div id="center">
		<p style="height:600px">
			style="height:600px"
		</p>
	</div>
	<div id="right">
		<p style="height:700px">
			style="height:700px"
		</p>
	</div>
</div>
</body>
</html>

运行代码复制代码另存代码(提示:可以编辑后运行)

原文地址:http://www.w3cfuns.com/thread-2851-1-1.html


Viewing all articles
Browse latest Browse all 7

Trending Articles