用Class组合实现Div+CSS布局教程


现在编写xhtml,我们强烈不推荐此种方法,应该是id,class综合应用。此文章只是讲述一种思维,并非建站过程中的方法!
用标准件的方式来组装网页DIV布局。我把class分为2种:布局class;风格class。
布局class是骨架,风格class是衣服。
举个例子:比如布局中的左栏。首先它的属性有:是左栏,宽度,背景颜色,字体颜色等。

1、首先会定义一个class,比如:.layout,主要用来控制页面整个的大小
.layout{width:98%;margin:0 auto;text-align:left;}

2、然后会定义3个基本布局Class(l,m,r)
.l{float:left}
.m{width:auto}
.r{float:right}
我把2栏布局也归类于3栏布局,因为3栏布局中,左右栏的宽度分别为0的时候,3栏就变成了2栏.
我们写基本布局代码的时候,最好还是写成3栏格式.

3、对应布局Class,定义需要的风格Class,比如宽度,高度,背景颜色等等这些都属于风格元素
.class_l{background:#ff0;margin-right: -150px;width:150px;}
.class_m{background:#f00;margin:0 140px 0 150px;}
.class_r{background:#00f;margin-left: -140px;width:140px;}

布局class只有一套,风格class可以定义很多。比如,要中栏里面在做一个小的2栏布局,就可以再定义一个风格class。
.mid_l{background:#ff0;margin-right: -100px;width:100px;}
.mid_m{background:#f00;margin:0 0 0 100px;}

4、将布局class和风格class结合起来,在代码这样引用:
<div class="l class_l"></div>
<div class="l mid_l"></div>
将2个class都引用,中间用空格隔开,前面的是布局class,后面的是风格class,后面还可以继续空格引用,如果需要再特殊定义,你可以给这个div取一个id来定义。
其他的一些常用的风格class也可以写成通用的,比如隐含可以定义为
.hide{display:none} 然后需要的时候,class="xxx hide"来引用
<!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=gb2312" /> <title>dw8.cn - 组合CLASS</title> <style> body, html { background:#fff;     color: #000;     font-size:12px;     margin:0;     line-height:150%;padding:0px;     text-align:center; }  /*布局的大框架,用来控制整体页面大小,尺寸由自己控制*/ #layout{width:98%;margin:0 auto;text-align:left;} /*页面左,中,右布局*/ .l{float:left} .r{float:right} .m{width:auto} /*页面左,中,右布局的class*/ .lclass{background:#ff0;margin-right: -150px;width:150px;} .mclass{background:#f00;margin:0 140px 0 150px;} .rclass{background:#00f;margin-left: -140px;width:140px;}  .l1{background:#dd0;margin-right: -200px;width:200px;} .m1{background:#ee0;margin:0 0 0 200px;} .l2{background:#dd0;margin-right: -50px;width:50px;} .m2{background:#ee0;margin:0 0 0 50px;} </style> </head> <body> <div id="layout">  <div class="l lclass">  Left<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />  </div>  <div class="r rclass">   <div class="l l2">Here</div>   <div class="m m2">There</div>  Right  <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />  </div>  <div class="m mclass">  Middle   <div class="l l1">Here1</div>   <div class="m m1">There1</div>   <br /><br /><br /><br /><br /><br /><br /><br /><br /><br />  </div>  </div> </body> </html>

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3