[关闭]
@xudongh 2016-11-05T23:32:41.000000Z 字数 3347 阅读 3391

圣杯布局的实现原理

前端开发


圣杯布局是一种非常经典和常用的布局方式,其所指的是三列布局,中间宽度自适应,两边定宽;或者两列布局,主体宽度自适应,左边或右边定宽。


下面主要拿三列的圣杯布局进行分析。

image_1b0qgghq21p6513d610241n1f1nv99.png-6.5kB

  1. <body>
  2. <div id="hd">头部</div>
  3. <div class="container">
  4. <div class="main"> //主内容栏
  5. <p>主内容栏自适应宽度</p>
  6. </div>
  7. <div class="left"> //左边栏
  8. <p>侧边栏1固定宽度</p>
  9. </div>
  10. <div class="right"> //右边栏
  11. <p>侧边栏2固定宽度</p>
  12. </div>
  13. </div>
  14. <div id="ft">底部</div>
  15. </body>
  1. .mian {
  2. background-color: #03a9f4;
  3. float: left;
  4. width:100%;
  5. }
  6. .left {
  7. background-color: #00bcd4;
  8. float: left;
  9. width: 200px;
  10. }
  11. .right{
  12. background-color: #00bcd4;
  13. float: left;
  14. width: 200px;
  15. }

image_1b0qh7eh315gcaasn9u405medm.png-7.9kB

  1. .mian {
  2. background-color: #03a9f4;
  3. float: left;
  4. width:100%;
  5. padding-left: 210px;
  6. }
  7. .left {
  8. background-color: #00bcd4;
  9. float: left;
  10. width: 200px;
  11. margin-left: -100%;
  12. }

image_1b0qhgae611q1s1r18ngat52tq13.png-7.9kB

  1. .left {
  2. background-color: #00bcd4;
  3. float: left;
  4. width: 200px;
  5. margin-left: -100%;
  6. position: relative;
  7. left: 210px;
  8. }

image_1b0qhjsja11pg16ugfnv1tjf15bj1g.png-7.8kB

  1. .mian {
  2. background-color: #03a9f4;
  3. float: left;
  4. width:100%;
  5. padding-left: 210px;
  6. padding-right: 210px;
  7. }
  8. .right {
  9. float: left;
  10. width: 200px;
  11. margin-left: -200px;
  12. position: relative;
  13. right: -210px;
  14. }

image_1b0qho90c6779ct7ig1s5o3ek1t.png-6.6kB


以下圣杯布局各种布局方式的demo:
image_1b0qhrq7p193jok71ctd1ouu5a02a.png-18.6kB

代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <title>圣杯布局</title>
  7. <style type="text/css">
  8. body {background-color: #ffffff; font-size:14px;}
  9. #hd, #ft {padding:20px 3px; background-color: #cccccc; text-align: center;}
  10. .bd-lft, .bd-rgt, .bd-3-lr, .bd-3-ll, .bd-3-rr {margin:10px 0; min-width:400px;}
  11. .main {background-color: #03a9f4; color:#ffffff;}
  12. .aside, .aside-1, .aside-2 {background-color: #00bcd4; color:#ffffff;}
  13. p {margin:0; padding:20px; text-align: center;}
  14. /* 左侧栏固定宽度,右侧自适应 */
  15. .bd-lft {
  16. zoom:1;
  17. overflow:hidden;
  18. padding-left:210px;
  19. }
  20. .bd-lft .aside {
  21. float:left;
  22. width:200px;
  23. margin-left:-100%; /*= -100%*/
  24. position:relative;
  25. left:-210px; /* = -parantNode.paddingLeft */
  26. _left: 0; /*IE6 hack*/
  27. }
  28. .bd-lft .main {
  29. float:left;
  30. width:100%;
  31. }
  32. /* 右侧栏固定宽度,左侧自适应 */
  33. .bd-rgt {
  34. zoom:1;
  35. overflow:hidden;
  36. padding-right:210px;
  37. }
  38. .bd-rgt .aside {
  39. float:left;
  40. width:200px;
  41. margin-left:-200px; /* = -this.width */
  42. position:relative;
  43. right:-210px; /* = -parantNode.paddingRight */
  44. }
  45. .bd-rgt .main {
  46. float:left;
  47. width:100%;
  48. }
  49. /* 左中右 三栏自适应 */
  50. .bd-3-lr {
  51. zoom:1;
  52. overflow:hidden;
  53. padding-left:210px;
  54. padding-right:210px;
  55. }
  56. .bd-3-lr .main {
  57. float:left;
  58. width:100%;
  59. }
  60. .bd-3-lr .aside-1 {
  61. float: left;
  62. width:200px;
  63. margin-left: -100%;
  64. position:relative;
  65. left: -210px;
  66. _left: 210px; /*IE6 hack*/
  67. }
  68. .bd-3-lr .aside-2 {
  69. float: left;
  70. width:200px;
  71. margin-left: -200px;
  72. position:relative;
  73. right: -210px;
  74. }
  75. /* 都在左边,右侧自适应 */
  76. .bd-3-ll {
  77. zoom:1;
  78. overflow:hidden;
  79. padding-left:420px;
  80. }
  81. .bd-3-ll .main {
  82. float:left;
  83. width:100%;
  84. }
  85. .bd-3-ll .aside-1 {
  86. float: left;
  87. width:200px;
  88. margin-left: -100%;
  89. position:relative;
  90. left: -420px;
  91. _left: 0px; /*IE6 hack*/
  92. }
  93. .bd-3-ll .aside-2 {
  94. float: left;
  95. width:200px;
  96. margin-left: -100%;
  97. position:relative;
  98. left: -210px;
  99. _left: 210px; /*IE6 hack*/
  100. }
  101. /* 都在右边,左侧自适应 */
  102. .bd-3-rr {
  103. zoom:1;
  104. overflow:hidden;
  105. padding-right:420px;
  106. }
  107. .bd-3-rr .main {
  108. float:left;
  109. width:100%;
  110. }
  111. .bd-3-rr .aside-1 {
  112. float: left;
  113. width:200px;
  114. margin-left: -200px;
  115. position:relative;
  116. right: -210px;
  117. }
  118. .bd-3-rr .aside-2 {
  119. float: left;
  120. width:200px;
  121. margin-left: -200px;
  122. position:relative;
  123. right: -420px;
  124. }
  125. </style>
  126. </head>
  127. <body>
  128. <div id="hd">头部</div>
  129. <div class="bd-3-lr">
  130. <div class="main">
  131. <p>主内容栏自适应宽度</p>
  132. </div>
  133. <div class="aside-1">
  134. <p>侧边栏1固定宽度</p>
  135. </div>
  136. <div class="aside-2">
  137. <p>侧边栏2固定宽度</p>
  138. </div>
  139. </div>
  140. <div id="ft">底部</div>
  141. </body>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注