经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
--------------------------三栏布局之左右宽度固定,中间自适应--------------------------
来源:cnblogs  作者:没结尾的结尾  时间:2018/12/3 9:49:53  对本文有异议

常见的页面布局有

1、左右宽度固定,中间自适应;

2、上下高度固定,中间自适应;

3、左宽度固定,右自适应;

4、上高度固定,下自适应,

下边是我看过网上的视频后写出来的三栏布局-左右宽高固定,中间自适应;

  1. 1 <!DOCTYPE html><!-- html 5 -->
  2. 2 <html>
  3. 3 <head>
  4. 4 <title>左中右三栏布局</title>
  5. 5 <style type="text/css">
  6. 6 html * {
  7. 7 padding: 0;
  8. 8 margin: 0;
  9. 9 }
  10. 10 .layout{
  11. 11 width: 100%;
  12. 12 margin-top: 15px;
  13. 13 }
  14. 14 .layout .three_columns > div{
  15. 15 min-height: 150px;
  16. 16 text-align: center;
  17. 17 }
  18. 18 .layout .three_columns > div.center > p{
  19. 19 margin-top: 15px;
  20. 20 }
  21. 21 </style>
  22. 22 </head>
  23. 23 <body>
  24. 24 <!-- 第一种三栏布局:float方式 -->
  25. 25 <section class="layout float">
  26. 26 <style type="text/css">
  27. 27 .layout.float .left{
  28. 28 float: left;
  29. 29 width: 300px;
  30. 30 background: #90D9F7;
  31. 31 }
  32. 32 .layout.float .right{
  33. 33 float: right;
  34. 34 width: 300px;
  35. 35 background: #F5DE25;
  36. 36 }
  37. 37 .layout.float .center{
  38. 38 background: pink;
  39. 39 }
  40. 40 </style>
  41. 41 <article class="three_columns">
  42. 42 <div class="left"></div>
  43. 43 <div class="right"></div>
  44. 44 <div class="center">
  45. 45 <h1>三栏布局float布局</h1>
  46. 46 <p>优点:兼容性比较好
  47. 47 缺点:脱离文档流,清除浮动,处理与周边元素布局
  48. 48 </p>
  49. 49 <p>去掉高度后,内容会超出容器</p>
  50. 50 </div>
  51. 51 </article>
  52. 52 </section>
  53. 53
  54. 54 <!-- 第二种三栏布局:position方式 -->
  55. 55 <section class="layout position">
  56. 56 <style type="text/css">
  57. 57 .layout.position .left{
  58. 58 position: absolute;
  59. 59 left: 0;
  60. 60 width: 300px;
  61. 61 background: #90D9F7;
  62. 62 }
  63. 63 .layout.position .center{
  64. 64 position: absolute;
  65. 65 left: 300px;
  66. 66 right: 300px;
  67. 67 background: pink;
  68. 68 }
  69. 69 .layout.position .right{
  70. 70 position: absolute;
  71. 71 right: 0;
  72. 72 width: 300px;
  73. 73 background: #F5DE25;
  74. 74 }
  75. 75 </style>
  76. 76 <srticle class="three_columns">
  77. 77 <div class="left"></div>
  78. 78 <div class="center">
  79. 79 <h1>三栏布局position布局</h1>
  80. 80 <p>优点:快速布局
  81. 81 缺点:脱离文档流,可使用性差
  82. 82 </p>
  83. 83 <p>去掉高度后,内容会超出容器</p>
  84. 84 </div>
  85. 85 <div class="right"></div>
  86. 86 </srticle>
  87. 87 </section>
  88. 88
  89. 89 <!-- 第三种三栏布局:flex方式 -->
  90. 90 <section class="layout flexbox">
  91. 91 <style type="text/css">
  92. 92 .layout.flexbox{
  93. 93 margin-top: 180px;
  94. 94 }
  95. 95 .layout.flexbox .three_columns{
  96. 96 display: flex;
  97. 97 }
  98. 98 .layout.flexbox .left{
  99. 99 width:300px;
  100. 100 background: #90D9F7;
  101. 101 }
  102. 102 .layout.flexbox .center{
  103. 103 flex: 1;
  104. 104 background: pink;
  105. 105 }
  106. 106 .layout.flexbox .right{
  107. 107 width: 300px;
  108. 108 background: #F5DE25;
  109. 109 }
  110. 110 </style>
  111. 111 <article class="three_columns">
  112. 112 <div class="left"></div>
  113. 113 <div class="center">
  114. 114 <h1>三栏布局flex布局</h1>
  115. 115 <p>解决上两种方案的缺陷,最好用的布局
  116. 116 </p>
  117. 117 <p>去掉高度后,容器会被内容撑开,还可以使用</p>
  118. 118 </div>
  119. 119 <div class="right"></div>
  120. 120 </article>
  121. 121 </section>
  122. 122
  123. 123 <!-- 第四种三栏布局:table -->
  124. 124 <section class="layout table">
  125. 125 <style type="text/css">
  126. 126 .layout.table .three_columns{
  127. 127 display: table;
  128. 128 width: 100%;
  129. 129 height: 150px;
  130. 130 }
  131. 131 .layout.table .three_columns>div{
  132. 132 display: table-cell;
  133. 133 }
  134. 134 .layout.table .left{
  135. 135 width: 300px;
  136. 136 background: #90D9F7;
  137. 137 }
  138. 138 .layout.table .center{
  139. 139 background: pink;
  140. 140 }
  141. 141 .layout.table .right{
  142. 142 width: 300px;
  143. 143 background: #F5DE25;
  144. 144 }
  145. 145 </style>
  146. 146 <article class="three_columns">
  147. 147 <div class="left"></div>
  148. 148 <div class="center">
  149. 149 <h1>三栏布局table布局</h1>
  150. 150 <p>
  151. 151 优点:兼容性是最好的
  152. 152 缺点:不好控制、当其中一个高度变,其他的高度也会变
  153. 153 </p>
  154. 154 <p>去掉高度后,容器会被内容撑开,还可以使用</p>
  155. 155 </div>
  156. 156 <div class="right"></div>
  157. 157 </article>
  158. 158 </section>
  159. 159
  160. 160 <!-- 第五种三栏布局:grid -->
  161. 161 <section class="layout grid">
  162. 162 <style type="text/css">
  163. 163 .layout.grid .three_columns{
  164. 164 width: 100%;
  165. 165 display: grid;
  166. 166 grid-template-rows: 150px;
  167. 167 grid-template-columns: 300px auto 300px;
  168. 168 }
  169. 169 .layout.grid .left{
  170. 170 background: #90D9F7;
  171. 171 }
  172. 172 .layout.grid .center{
  173. 173 background: pink;
  174. 174 }
  175. 175 .layout.grid .right{
  176. 176 background: #F5DE25;
  177. 177 }
  178. 178 </style>
  179. 179 <article class="three_columns">
  180. 180 <div class="left"></div>
  181. 181 <div class="center">
  182. 182 <h1>三栏布局grid布局</h1>
  183. 183 <p>
  184. 184 优点:兼容性是最好的
  185. 185 缺点:不好控制、当其中一个高度变,其他的高度也会变
  186. 186 </p>
  187. 187 <p>去掉高度后,内容会超出容器</p>
  188. 188 </div>
  189. 189 <div class="right"></div>
  190. 190 </article>
  191. 191 </section>
  192. 192 </body>
  193. 193 </html>

下图是我的代码的运行结果:

我在上边总结了一下每种布局的优缺点,以及去掉高度后哪种布局还可以使用,如果各位有觉得不对的地方,欢迎大家帮我纠正!

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号