html中div自适应代码,CSS+DIV自适应布局

本文详细介绍了如何使用CSS和DIV进行布局,包括两列布局(左侧固定,右侧自适应)、三列布局(左右固定,中间自适应)、上中下布局(头部、中部自适应、底部固定)以及上下布局(内容决定底部位置)。通过实例代码和结果展示,帮助新手理解并掌握响应式设计的基础技巧。
摘要由CSDN通过智能技术生成

CSS+DIV自适应布局

1.两列布局(左右两侧,左侧固定宽度200px;右侧自适应占满)

代码如下:

左右两侧,左侧固定宽度200px;右侧自适应占满

.box{

width:800px;

height:300px;

margin:0 auto;

}

.left{

width:200px;

height:300px;

background:#f00;

float:left;

}

.right{

height:300px;

margin-left:200px;

background:#0f0;

}

运行结果如下图:

bVbdKk8?w=1061&h=420

两栏布局(左定宽,右自动)

float + margin

2.三列布局(左中右三列,左右200px固定,中间自适应占满)

方法一(左右浮动,中间 margin-left,margin-right,中间div在最后)

代码如下

左中右三列,左右200px固定,中间自适应占满 方法一

.box{

width:800px;

height:300px;

margin:0 auto;

}

.left{

width:200px;

height:300px;

background:#f00;

float:left;

}

.right{

width:200px;

height:300px;

float:right;

background:#0f0;

}

.center{

height:300px;

background-color:#00f;

margin:0 200px;

}

运行结果如下图bVbdKmi?w=1354&h=460

方法二(左中右定位):

代码如下

左中右三列,左右200px固定,中间自适应占满 方法二

.box{

width:1000px;

height:300px;

margin:0 auto;

position:relative;

}

.left{

width:200px;

height:300px;

background:#f00;

position:absolute;

top:0px;

left:0px;

}

.right{

width:200px;

height:300px;

background:#0f0;

position:absolute;

top:0px;

right:0px;

}

.center{

height:300px;

background-color:#00f;

position:absolute;

left:200px;

right:200px;

}

运行结果如下图:

bVbdKmi?w=1354&h=460

3.上中下三行,头部,底部固定高200px,中间自适应占满

中间定位,底部定位

代码如下:

上中下三行,头部,底部固定高200px,中间自适应占满

*{

margin:0;

}

html{

height:100%;

}

body{

min-height:100%;

}

.header{

width:100%;

height:100px;

background-color:#ccc;

}

.main{

width:100%;

background-color:#f00;

position:absolute;

top:100px;

bottom:100px;

}

.footer{

height:100px;

width:100%;

position:absolute;

bottom:0px;

background-color:#ccc;

}

运行代码如下图:

bVbdKoh?w=1339&h=616

4.上下两部分,地下这个股东高度200px,如果上面的内容少,那么这个footer就固定在底部,如果内容多,就把footer往下挤

代码如下:

上下两部分,地下这个股东高度200px,如果上面的内容少,

那么这个footer就固定在底部,如果内容多,就把footer往下挤

*{

margin:0;

}

html{

height:100%;

}

body{

min-height:100%;

background-color:#00f;

position:relative;

}

.header{

height:100%;

background-color:#00f;

padding-bottom:200px;

}

.footer{

width:100%;

height:200px;

position:absolute;

bottom:0px;

background-color:#0ff;

}

sdsadas

sdsadas

sdsadas

sdsadas

sdsadas

sdsadas

sdsadas

sdsadas

sdsadas

sdsadas

sdsadas

sdsadas

sdsadas

运行结果如下图:

bVbdKph?w=1366&h=637

新手入门,请多多关照

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值