您好,欢迎访问一九零五行业门户网

CSS自适应布局(左右固定 中间自适应或者右侧固定 左侧自适应)_html/css_WEB-ITnose

经常在工作中或者在面试中会碰到这样的问题,比如我想要个布局 右侧固定宽度 左侧自适应 或者 三列布局 左右固定 中间自适应的问题。
     下面我们分别来学习下,当然我也是总结下而已,有如以下网站源码方法:
     一: 右侧固定宽度 左侧自适应
  第一种方法:左侧用margin-right,右侧float:right  就可以实现。
    html代码可以如下写:
我是龙恩
我是龙恩
css代码可以如下写:
    .box-left{height:300px;margin-right:300px;background:#ddd;}
    .box-right{width:300px;height:300px;float:right;background:#aaa;}
    如上代码就可以实现效果。
    第2种方法:左侧同样用margin-right  右侧采用绝对定位 如下代码所示:
    html代码如下:
我是龙恩
我是龙恩
css代码如下:
 .bd{position:relative;}
 .bd-left{height:300px;;margin-right:300px;background:#ddd;}
 .bd-right{width:300px;height:300px;position:absolute;top:0;right:0;background:#aaa;}
 第三种方法:右侧浮动 且 用负margin值
 html代码如下:
我是龙恩
我是龙恩
css代码如下:
  .wrap{overflow:hidden;background:#eee;}
  .wrap-right{width:300px;position:relative;float:right;margin-left:-300px;background:#aaa;}
  .wrap-left{width:100%;float:left;}
  .left-con{margin-right:300px;background:#ddd;}
  .left-con,.wrap-right{height:300px;}
以上是我总结的三种html css 两列布局方法(左侧自适应 右侧固定),如有不足的地方 请大家多多指教。下面我们来看看三列布局(左右固定 中间自适应的情况)。
 二:左右固定 中间自适应的情况
 我目前总结了2种方法 如下:
 第一种:左右侧采用浮动 中间采用margin-left 和 margin-right 方法。
 代码如下:
这是右侧的内容 固定宽度
这是左侧的内容 固定宽度
中间内容,自适应宽度
第二种:左右两侧采用绝对定位 中间同样采用margin-left margin-right方法:
 html代码如下:
css代码如下:
 .l-sidebar {
 width:200px;
 height:500px;
 position:absolute;
 top:0;
 left:0;
 background:blue;
}
.mainbar {
 margin-left:200px;
 height:500px;
 margin-right:300px;
 background:green;
}
.r-sidebar {
 width:300px;
height:500px;
 position:absolute;
top:0;
 right:0;
 background:blue;
}
以上是我们日常工作中的一些总结!如有不足的地方 请留言!!一起互相讨论学习!
来源:http://www.codesocang.com/jiaocheng/css3jiaocheng/2014/0507/7469.html
其它类似信息

推荐信息