今天学习了用CSS对网页进行简单的布局,感觉还不错,下面来分享一下。
首先用div给页面分块是基础,网页布局就是将网页合理的分成一个一个块。
接下来写CSS层叠样式,给每个要定义样式的Div定义一个Class或id,在对应的Class里面写好样式,这里最主要的用到的属性有margin,padding,width,height,background,border等等。
下面来一个页面布局实例:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>css2,布局练习</title>
<style type="text/css">
body{margin: 0;padding:0;
}
.backgroud{width:100%;height:1060px;
background-image: url("img/p1.jpg"); background-position:center; background-repeat: no-repeat;}
.head{ width: 800px;height: 100px;margin: 0 auto; background-color: #ccc}
.content{ width: 70%;height: 400px;margin: 40px auto; background-color: #ccc; opacity:0.5;}
.con1{ width: 100px;height: 100%; background-color: #444;float: left;}
.con2{ width: 700px;height: 100%; background-color: #999;float: left;}
.con3{ width: 100px;height: 100%; background-color: #444;float: right;}
.footer{}
}
</style>
</head>
<body>
<div class="backgroud">
<div class="head">
</div>
<div class="content" >
<div class="con1">
</div>
<div class="con3">
cont3
</div>
<div class="con2">
<div class="text">文字测试,哈哈</div>
</div>
</div>
</div>
</body>
</html>
首先body{margin: 0;padding:0;}我把body的内外边距变成零,这里可以让整版的背景彰显出来,好看。同时center让内容居中。
接下了是定义页头,一头是灰色背景.head{ width: 800px;height: 100px;margin: 0 auto; background-color: #ccc}宽度为800px.
主体部分分了三列
.content{ width: 70%;height: 400px;margin: 40px auto; background-color: #ccc; opacity:0.5;}
.con1{ width: 100px;height: 100%; background-color: #444;float: left;}
.con2{ width: 700px;height: 100%; background-color: #999;float: left;}
.con3{ width: 100px;height: 100%; background-color: #444;float: right;}
这里的content为内容的载体,opacity:0.5;可以让内容变透明。