现在的位置: 首页 > 综合 > 正文

css常用布局

2012年07月20日 ⁄ 综合 ⁄ 共 5735字 ⁄ 字号 评论关闭
  1. CSS布局常用的方法:float : none | left | right 
  2. 取值:
  3. none :  默认值。对象不飘浮
  4. left :  文本流向对象的右边
  5. right :  文本流向对象的左边
  6. 它是怎样工作的,看个一行两列的例子
  7. xhtml:
  8. <div id="warp">
  9. <div id="column1">这里是第一列</div>
  10. <div id="column2">这里是第二列</div>
  11. <div class="clear"></div>
  12. </div>
  13. CSS:
  14. #wrap{ width:100%; height:auto;}
  15. #column1{ float:left; width:40%;}
  16. #column2{ float:right; width:60%;}
  17. .clear{ clear:both;}
  18. position : static | absolute | fixed | relative
  19. 取值:
  20. static :  默认值。无特殊定位,对象遵循HTML定位规则
  21. absolute :  将对象从文档流中拖出,使用 left , right , top , bottom 等属性相对于其最接近的一个最有定位设置的父对象进行绝对定位。如果不存在这样的父对象,则依据 body 对象。而其层叠通过 z-index 属性定义
  22. fixed :  未支持。对象定位遵从绝对(absolute)方式。但是要遵守一些规范
  23. relative :  对象不可层叠,但将依据 left , right , top , bottom 等属性在正常文档流中偏移位置
  24. 它来实现一行两列的例子
  25. xhtml:
  26. <div id="warp">
  27. <div id="column1">这里是第一列</div>
  28. <div id="column2">这里是第二列</div>
  29. </div>
  30. CSS:
  31. #wrap{ position:relative;/*相对定位*/width:770px;}
  32. #column1{ position:absolute; top:0; left:0; width:300px;}
  33. #column2{position:absolute; top:0; right:0; width:470px;}
  34. 他们的区别在哪?
  35. 显然,float是相对定位的,会随着浏览器的大小和分辨率的变化而改变,而position就不行了,所以一般情况下还是float布局!
  36. CSS常用布局实例
  37. 一列
  38. 单行一列
  39. body { margin: 0px;   padding: 0px;  text-align: center;  }
  40. #content {  margin-left:auto;  margin-right:auto;  width: 400px;  width: 370px; }
  41. 两行一列
  42. body {  margin: 0px;   padding: 0px;   text-align: center;}
  43. #content-top { margin-left:auto;   margin-right:auto; width: 400px;  width: 370px;}
  44. #content-end {margin-left:auto; margin-right:auto;  width: 400px;   width: 370px;}
  45. 三行一列
  46. body {  margin: 0px; padding: 0px;  text-align: center;  }
  47. #content-top {  margin-left:auto;  margin-right:auto;  width: 400px;   width: 370px;  }
  48. #content-mid { margin-left:auto; margin-right:auto;  width: 400px;  width: 370px; }
  49. #content-end { margin-left:auto; margin-right:auto; width: 400px;  width: 370px; }
  50. 两列
  51. 单行两列
  52. #bodycenter { width: 700px;margin-right: auto; margin-left: auto;overflow: auto;  }
  53. #bodycenter #dv1 {float: left;width: 280px;}
  54. #bodycenter #dv2 {float: right;width: 410px;}
  55. 两行两列
  56. #header{    width: 700px; margin-right: auto;margin-left: auto; overflow: auto;}
  57. #bodycenter { width: 700px; margin-right: auto; margin-left: auto; overflow: auto; }
  58. #bodycenter #dv1 { float: left; width: 280px;}
  59. #bodycenter #dv2 { float: right;width: 410px;}
  60. 三行两列
  61. #header{    width: 700px;margin-right: auto; margin-left: auto;  }
  62. #bodycenter {width: 700px; margin-right: auto; margin-left: auto;  }
  63. #bodycenter #dv1 {  float: left;width: 280px;}
  64. #bodycenter #dv2 { float: right;  width: 410px;}
  65. #footer{     width: 700px;  margin-right: auto; margin-left: auto;  overflow: auto;  }
  66. 三列
  67. 单行三列
  68. 绝对定位
  69. #left { position: absolute; top: 0px;  left: 0px; width: 120px;  }
  70. #middle {margin: 20px 190px 20px 190px; }
  71. #right {position: absolute;top: 0px; right: 0px;  width: 120px;}
  72. float定位
  73. xhtml:
  74. <div id="warp">
  75. <div id="column">
  76. <div id="column1">这里是第一列</div>
  77. <div id="column2">这里是第二列</div>
  78. <div class="clear"></div>
  79. </div>
  80. <div id="column3">这里是第三列</div>
  81. <div class="clear"></div>
  82. </div>
  83. CSS:
  84. #wrap{ width:100%; height:auto;}
  85. #column{ float:left; width:60%;}
  86. #column1{ float:left; width:30%;}
  87. #column2{ float:right; width:30%;}
  88. #column3{ float:right; width:40%;}
  89. .clear{ clear:both;}
  90. float定位二
  91. xhtml:
  92. <div id="center" class="column">
  93.   <h1>This is the main content.</h1>
  94. </div>
  95. <div id="left" class="column">
  96.   <h2>This is the left sidebar.</h2>
  97. </div>
  98. <div id="right" class="column">
  99.   <h2>This is the right sidebar.</h2>
  100. </div>
  101. CSS:
  102. body {margin: 0;padding-left: 200px;padding-right: 190px;min-width: 240px;}
  103. .column {position: relative;float: left;}
  104. #center {width: 100%;}
  105. #left {width: 180px; right: 240px;margin-left: -100%;}
  106. #right {width: 130px;margin-right: -100%;}
  107. 两行三列
  108. xhtml:
  109. <div id="header">这里是顶行</div>
  110. <div id="warp">
  111. <div id="column">
  112. <div id="column1">这里是第一列</div>
  113. <div id="column2">这里是第二列</div>
  114. <div class="clear"></div>
  115. </div>
  116. <div id="column3">这里是第三列</div>
  117. <div class="clear"></div>
  118. </div>
  119. CSS:
  120. #header{width:100%; height:auto;}
  121. #wrap{ width:100%; height:auto;}
  122. #column{ float:left; width:60%;}
  123. #column1{ float:left; width:30%;}
  124. #column2{ float:right; width:30%;}
  125. #column3{ float:right; width:40%;}
  126. .clear{ clear:both;}
  127. 三行三列
  128. xhtml:
  129. <div id="header">这里是顶行</div>
  130. <div id="warp">
  131. <div id="column">
  132. <div id="column1">这里是第一列</div>
  133. <div id="column2">这里是第二列</div>
  134. <div class="clear"></div>
  135. </div>
  136. <div id="column3">这里是第三列</div>
  137. <div class="clear"></div>
  138. </div>
  139. <div id="footer">这里是底部一行</div>
  140. CSS:
  141. #header{width:100%; height:auto;}
  142. #wrap{ width:100%; height:auto;}
  143. #column{ float:left; width:60%;}
  144. #column1{ float:left; width:30%;}
  145. #column2{ float:right; width:30%;}
  146. #column3{ float:right; width:40%;}
  147. .clear{ clear:both;}
  148. #footer{width:100%; height:auto;}
  149. PS:这里列出的是常用的例子,而非研究之用,对一每个盒子,我都没有设置margin,padding,boeder等属性,是因为我个人觉得,含有宽度定位的时候,最好不好用到他们,除非必不得已,因为如果不是这样的话,解决浏览器兼容问题,会让你头疼,而且产生一系列CSS代码,我觉得这样的效率和效果都不好!
  150. CSS布局高级技巧
  151. margin和padding总是有可能要用到,而产生的问题如何解决呢?由于浏览器解释容器宽度的方法不同:
  152. IE 6.0 Firefox Opera等是
  153. 真实宽度=width+padding+border+margin
  154. IE5.X
  155. 真实宽度=width-padding-border-margin
  156. 很明显,第一种下很完美的布局在第二种情况下后果是很凄惨的!
  157. 解决的方法是 hack
  158. div.content {
  159. width:400px; //这个是错误的width,所有浏览器都读到了
  160. voice-family: "/"}/""; //IE5.X/win忽略了"/"}/""后的内容
  161. voice-family:inherit;
  162. width:300px; //包括IE6/win在内的部分浏览器读到这句,新的数值(300px)覆盖掉了旧的
  163. }
  164. html>body .content { //html>body是CSS2的写法
  165. width:300px; //支持CSS2该写法的浏览器(非IE5)有幸读到了这一句
  166. }
  167. div.content {
  168. width:300px !important; //这个是正确的width,大部分支持!important标记的浏览器使用这里的数值
  169. width(空格)/**/:400px; //IE6/win不解析这句,所以IE6/win仍然认为width的值是300px;而IE5.X/win读到这句,新的数值(400px)覆盖掉了旧的,因为!important标记对他们不起作用
  170. }
  171. html>body .content { //html>body是CSS2的写法
  172. width:300px; //支持CSS2该写法的浏览器有幸读到了这一句
  173. }
  174. 列等高技巧
  175. n行n列布局,每列高度(事先并不能确定哪列的高度)的相同,是每个设计师追求的目标,做法有:背景图填充、加JS脚本的
  176. 方法和容器溢出部分隐藏和列的负底边界和正的内补丁相结合的方法。
  177. 背景图填充法:
  178. xhtml:
  179. <div id="wrap">
  180. <div id="column1">这是第一列</div>
  181. <div id="column1">这是第二列</div>
  182. <div class="clear"></div>
  183. </div>
  184. css:
  185. #wrap{ width:776px; background:url(bg.gif) repeat-y 300px;}
  186. #column1{ float:left; width:300px;}
  187. #column2{ float:right; width:476px;}
  188. .clear{ clear:both;}
  189. 就是将一个npx宽的一张图片在外部容器纵向重复,定位到两列交错的位置纵向重复,在视觉上产生了两列高度一样的错觉

 

抱歉!评论已关闭.