经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
css实现两栏固定中间自适应的方法_CSS布局实例_CSS
来源:jb51  时间:2018/11/30 9:16:57  对本文有异议

1、利用绝对定位和margin

此方法的原理说将左右两侧进行定位,让其脱离文档流。 中心区域自然流动到它们下面,再为其设置margin值

此方法页面元素结构可以顺序可以随意变动,注意top值需要进行处理,不然可能会出现对不齐现象

HTML

  1. <div id='container'>
  2. <div class='left'>左侧</div>
  3. <div class='center'>中间</div>
  4. <div class='right'>右侧</div>
  5. </div>

CSS

  1. #container {
  2. position: relative;
  3. }
  4. .left, .right{
  5. position: absolute;
  6. top: 0;
  7. width: 200px;
  8. min-height: 500px;
  9. background-color: red;
  10. }
  11. .left {
  12. left: 0;
  13. }
  14. .right {
  15. right: 0;
  16. }
  17. .center {
  18. margin: 0px 210px;
  19. min-height: 500px;
  20. background-color: yellow;
  21. }

2、利用浮动和margin

此方法的原理说将左右两侧进行float 浮动让其脱离文档流,中心部分处于正常文档流,再为其设置margin值

此方法一定要将center中间部分放到最后,当窗口特别小时右侧会被挤下来

HTML

  1. <div id='container'>
  2. <div class='left'>左侧</div>
  3. <div class='right'>右侧</div>
  4. <div class='center'>中间</div>
  5. </div>

CSS

  1. #container {
  2. position: relative;
  3. }
  4. .left, .right {
  5. width: 200px;
  6. min-height: 500px;
  7. background-color: red;
  8. }
  9. .left {
  10. float: left;
  11. }
  12. .right {
  13. float: right;
  14. }
  15. .center {
  16. min-height: 500px;
  17. margin: 0px 210px;
  18. background-color: yellow;
  19. }

3、圣杯布局

此方法最常见,三者相互关联,最稳健。

首先需要将中间部分放再最前面,外面用一层容器包裹。外层容器让其占满整个屏幕100%, 左中右三者都float: left。 将center左右margin设置为两边容器的宽度加上边距,将left左侧margin-left设置为-100%,让其出现在最左侧,将right右侧margin-right设置为-200px,让其出现在最右侧。

HTML

  1. <div id='container'>
  2. <div class='center_wrap'>
  3. <div class='center'>中间</div>
  4. </div>
  5. <div class='left'>左侧</div>
  6. <div class='right'>右侧</div>
  7. </div>

CSS

  1. #container {
  2. position: relative;
  3. }
  4. .center_wrap, .left, .right{
  5. float: left;
  6. min-height: 500px;
  7. }
  8. .center_wrap {
  9. width: 100%;
  10. }
  11. .center_wrap .center{
  12. min-height: 500px;
  13. margin: 0px 210px;
  14. background-color: yellow;
  15. }
  16. .left, .right {
  17. width: 200px;
  18. background-color: red;
  19. }
  20. .left {
  21. margin-left: -100%;
  22. }
  23. .right {
  24. margin-left: -200px;
  25. }

4、CSS3 flex

HTML

  1. <div id='container'>
  2. <div class='left'>左侧</div>
  3. <div class='center'>中间</div>
  4. <div class='right'>右侧</div>
  5. </div>

CSS

  1. #container {
  2. width: 100%;
  3. display: flex;
  4. }
  5. .left, .right {
  6. width: 200px;
  7. background-color: red;
  8. min-height: 500px;
  9. }
  10. .center {
  11. flex: 1;
  12. min-height: 500px;
  13. margin: 0 10px;
  14. background-color: yellow;
  15. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

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

本站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号