垂直自适应问题

一、独立元素的自适应

1.设置高度为auto


设置高度为auto时,元素的高度随内容多少自动变化。

2、使用min-height和max-height

使用min-height时内容达不到min-height的高度时,显示min-height的高度;内容超过min-height时,随内容而自适应。
使用max-height自然是和min-height类似的道理。

二、两列自适应

1、右侧适应左侧
本质:父元素适应子元素的高度;同时清除浮动的子元素,让子元素的内容影响父元素的高度。
< div  class= "main" >
     < div  class= "sub" >This is a highly adaptive exercise
        This is a highly adaptive exercise
        This is a highly adaptive exercise
        This is a highly adaptive exercise
        This is a highly adaptive exercise
        This is a highly adaptive exercise
      
     </ div >this is father element < div  class= "clear" ></ div ></ div >

.main{
     width200px;
     backgroundyellow;
     color#fff;
}
.sub{
     heightauto;
     floatleft;
     width100px;
     backgroundyellow;
     color#444;
}
.clear{
     clearboth;
}
2、左右自适应
本质:使用负边距使子元素处于父元素之外,在这里使用了两次这个技术。当最里层的元素增加时,也即sub,content被撑大;当content元素增加时,main被撑大,而main和sub背景颜色相同,达到自适应的效果。
< div  class= "main" >
     < div  class= "content" >
     < div  class= "sub" >This is a highly adaptive exercise
        This is a highly adaptive exercise
        This is a highly adaptive exercise
        This is a highly adaptive exercise
        This is a highly adaptive exercise
        This is a highly adaptive exercise
      this is sub element
     </ div >
     < div >this is father element this is father element this is father element
     this is father elementthis is father element this is father element 
     this is father element this is father elementthis is father element
      this is father element this is father element 
      this is father elementthis is father element
     this is father element this is father element this is father element
     this is father element this is father elementthis is father element
      this is father element this is father element 
      this is father elementthis is father element
     this is father element this is father element this is father element </ div >
      </ div >

     < div  class= "clear" ></ div >
     </ div >

.main{
     width200px;
     margin-right-200px;
     backgroundyellow;
     color#fff;
}
.content{
     width200px;
     positionrelative;
     floatright;
     margin-right-200px;
     backgroundgreen;
     color#444;
}
.sub{
     positionrelative;
     floatleft;
     width200px;
     margin-left-200px;
     backgroundyellow;
     color#444;
}
.clear{
     clearboth;
}
三、三列自适应
1、两列固定,一列适应。
本质:适应的元素处于最内层嵌套元素。
< body >
     < div  class= "main" >       
     < div  class= "content" >
     < div  class= "left" >This is a highly adaptive exercise
      this is sub element
     </ div >
     < div  class= "change" >this is father element this is father element this is father element
     this is father element this is father element this is father elemen
     this is father element this is father element this is father element </ div >
      < div  class= "clear" ></ div >
      </ div >
      < div  class= "right" >This is a highly adaptive exercise
        This is a highly adaptive exercise
     </ div >
     < div  class= "clear" ></ div >
     </ div >   
</ body >

.main{
     width600px;
     backgroundyellow;
}
.content{
     width45%;
     color#000;
     background#777;
     floatleft;
}
.right{
width55%;
color#000;
backgroundyellow;
floatright;
}
.left{
     floatleft;
     width50%;
}
.change{
     floatright;
     width50%;
     backgroundyellowgreen;
     colorred;
     /**height: 250px;   
    当change高度小于left时,change必须设置一个合适于left的高度;
    当change高度大于left时,不必设置height**/
}
.clear{
     clearboth;
}

2、两列可变,右列固定
< body >
     < div  class= "main" >       
     < div  class= "content" >
     < div  class= "all-left" >
     < div  class= "left" >This is a highly adaptive exercise
      this is sub element </ div >
     < div  class= "middle" >thihis is fais this is father elementt </ div >
      < div  class= "clear" ></ div > 
     </ div >
     </ div >
      < div  class= "right" >This is a highly adaptive exercise
        This is a highly adaptive exercise  </ div >
     < div  class= "clear" ></ div >
     </ div >   
</ body >

.main{
     width600px;
     backgroundyellow;
}
.content{
     width150px;
     color#000;
     background#777;
     floatleft;
}
.all-left{
     positionrelative;
     floatright;
     width150px;
     margin-right-150px;
     backgroundyellowgreen;
     colorred;
}
.left{
     positionrelative;
     floatleft;
     width150px;
     height100px;
     margin-left-150px;
}
.middle{
width: 150px;
color#000;
backgroundyellowgreen;
floatright;
}
.right{
     floatright;
     width150px;
}
.clear{
     clearboth;
}

3、三列高度均不确定
本质:在有宽边距的元素中嵌套子元素,当任何子元素增加时,父元素也增加。
< body >
     < div  class= "main" >       
      < div  class= "left" >This is a highly adaptive exercise this is sub element </ div >
     < div  class= "middle" >thihis is fais this is father elementt </ div >
      < div  class= "right" >This is a highly adaptive exercise This is a highly adaptive exercise  </ div >
     < div  class= "clear" ></ div >
     </ div >   
</ body >
.main{
     width150px;
     border-left150px  solid  #333;
     border-right150px  solid  #999;
     backgroundyellow;
}
.left{
     positionrelative;
     floatleft;
     width150px;
     margin-left-150px; /**使用负边距将左边内容移到边框中**/
}
.middle{
width: 150px;
color#000;
backgroundyellow;
floatleft;
}
.right{
     positionrelative;
     floatright;
     width150px;
     margin-right-150px;
}
.clear{
     clearboth;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值