首页 技术 正文
技术 2022年11月17日
0 收藏 888 点赞 2,451 浏览 2320 个字

自适应布局分两类:高度和宽度,方法有很多,我用三列布局举例,我就列几个通俗易懂的例子呗,懂了三列的,两列的原理一样,呵呵哒。

效果图如下:高度自适应——宽度自适应

DIV+CSS自适应布局          DIV+CSS自适应布局

1,高度自适应布局

原理就是把每个模块设置为绝对定位,然后设置中间自适应的模块的top和bottom属性的值分别为头部模块和底部模块的高,然后中间模块的高度就自适应了。代码如下:

html代码:

<body>
<div class="top">
120px
</div>
<div class="main">
自适应
</div>
<div class="bottom">
120px
</div>
</body>

css代码:

.top{
width: 100%;
height: 120px;
position: absolute;
background-color: greenyellow;}
.main{
position: absolute;
width: 100%;
top: 120px;
bottom: 120px;
background-color: azure;
height: auto;
}
.bottom{
position: absolute;
bottom:;//别漏了
width: 100%;
height: 120px;
background-color:greenyellow ;
}

2,宽度自适应,有三种方法,分别是用绝对定位;利用margin,中间模块先渲染;自身浮动。

a,用绝对定位来设置宽度自适应布局,原理:针对自适应模块使用绝对定位,在把left和right设置为左右两列的宽,其实原理和高度自适应一样,另外左右两列分别左右浮动。

html代码:

<body>
<div class="left">
200px
</div>
<div class="main">
自适应
</div>
<div class="right">
200px
</div>
</body>

css代码:

html,
body {
margin:;
height: 100%;
padding:;
font-size: 30px;
font-weight:;
text-align: center;
}.left,
.right {
width: 200px;
display: inline;
height: 100%;
background-color: greenyellow;
}.left {
float: left;
}.right {
float: right;
}.main {
position: absolute;
left: 200px;
right: 200px;
height: 100%;
background-color: azure;
display: inline;
}

b,中间一列优先渲染的自适应三列布局,优先渲染(加载)的关键:内容在html里面必须放在前面。自适应的div必须放在left和right前面且包含在一个父div里。父div,left和right模块都向左浮动,然后对自适应的div(就是父div里的子div)设置margin:0 200px,然后对left的margin-left的属性值设置为100%的负数,就是margin-left:-100%;对right的margin-left的属性值设置为自身宽度的负数,就是margin-left:-200px。

注意:自适应的div必须放在left和right前面且包含在一个父div里。

html代码:

<body>
<div class="main"> <!--看清楚,这里用一个父div包住-->
<div class="content">
自适应
</div>
</div>
<div class="left">
200px
</div>
<div class="right">
200px
</div>
</body>

css代码:

html,
body {
margin:;
height: 100%;
padding:;
font-size: 30px;
font-weight:;
text-align: center;
}.main {
width: 100%;
height: 100%;
float: left;
}.main .content {
margin: 0 200px;
background-color: azure;
height: 100%;
}.left,
.right {
width: 200px;
height: 100%;
float: left;
background-color: greenyellow;
}.left {
margin-left: -100%; //important
}.right {
margin-left: -200px; //important
}

c,自身浮动,原理:中间列设置margin属性,就是把左右列分别左右浮动。注意:使用这个方法布局自适应的话,必须把自适应的那一列在html中放在left和right后面。

html代码:

<body>
<div class="left">
200px
</div>
<div class="right">
200px
</div>
<div class="main">
自适应
</div>
</body>

css代码:

html,
body {
margin:;
height: 100%;
padding:;
font-size: 30px;
font-weight:;
text-align: center;
}
.main {
margin: 0 200px;
height: 100%;
background-color: azure;
}
.left,
.right {
width: 200px;
height: 100%;
background-color: greenyellow;
}
.left {
float: left;
}
.right {
float: right;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291