左侧固定右侧自适应,右侧固定左侧自适应,左右两边固定中间自适应的几种方法

1. 左侧固定右侧自适应的方法

  • 定位法
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <style>
   .father{
      width: 100%;
      height: 300px;
      background-color: yellow;    
      position: relative;
      padding-left: 200px;
      box-sizing: border-box;
   }
   .left{ 
     position: absolute;
     left: 0;
     top: 0;
     width: 200px;
     height: 80%;   
     background-color: snow;
   }
   .right{  
     width: 100%;
     height: 100%;   
     background-color: lightblue;
   } 
  </style>
</head>
<body>
  <div class="father">
    <div class="left"></div>
    <div class="right"></div>
  </div>
</body>
</html>

  • flex法(伸缩盒子)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"> 
  <style>
   .father{
      width: 100%;
      height: 500px;
      background-color: lightblue;
      display: flex;
   }
   .left{
      width: 300px;
      height: 90%;
      background-color: rgb(175, 175, 49);
     }
     .right{
      flex: 1;
      background-color: lightgreen;
     } 
  
  </style>
</head>
<body>
  <div class="father">
     <div class="left"></div>
     <div class="right"></div>
  </div>
</body>
</html>
  • bfc 法
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    .father{
      width: 100%;
      height: 500px;
      background-color: rgb(125, 138, 196);     
    }
    .left{
      float: left;
      width: 200px;
      height: 80%;      
      background-color: lightblue;
    }
    .right{        
       overflow: hidden;
      /*  注意点右侧不可设置宽度  */
       height:450px;
       background-color: yellow;
    }
  

    /* 利用 bfc 实现的左侧固定,右侧自适应
      1--让左侧的盒子左浮动个,
      2--让右侧的盒子 overflow:hidden;
         注意点右边的盒子不可设置宽度。
         
      bfc就素块级格式化上下文
      特点:
      1--成为了bfc的盒子,就是一个独立的渲染区域,子元素的布局不会影响的外面的其他元素。
         应用: 清除浮动
                解决塌陷的问题
      2--  成为了bfc 的盒子额,不能与浮动的元素重叠
           用在:实现左侧固定右侧自适应。或者圣杯  


      触发bfc
       1-- 浮动
       2-- display:table /table-cell
       3-- o=position:fixed/absolute
       4-- overflow  非 visible (hidden auto scroll)    
     */
  </style>
</head>
<body>
  <div class="father">
    <div class="left"></div>
    <div class="right"></div>
  </div>
</body>
</html>

2. 右侧固定左侧自适应的方法

  • 定位法
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
   .father{
      width:100%;
      height: 500px;
      background-color: lightblue;
      position: relative;
      padding-right: 300px;
      box-sizing: border-box;
   }
   .rigth{
      position: absolute;
      right: 0px;
      top: 0px;      
      width: 300px;
      height: 90%;
      background-color: rgb(21, 128, 21);
   }
   .left{
      height: 95%;
      background-color:#ccc;
   }   
    </style>
</head>
<body>
  <div class="father">
     <div class="left"></div>
     <div class="rigth"></div>
  </div>
  
</body>
</html>
  • flex法
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
   .father{
      width:100%;
      height: 500px;
      background-color: lightblue;
   }
   .rigth{
      float: right;
      width: 300px;
      height: 90%;
      background-color: rgb(21, 128, 21);
   }
   .left{
     height: 95%;
     overflow: hidden;
     background-color: lightyellow;
   }
  </style>
</head>
<body>
  <div class="father">
    <div class="rigth"></div>
    <div class="left"></div>
  </div>
</body>
</html>
  • bfc法
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <style>
   .father{
      width: 100%;
      height: 500px;
      background-color: skyblue;
   }
   .left{
      height: 100%;
      overflow: hidden;
      background-color: yellow;
   }
   .right{
      float: right;
      width: 200px;
      height: 70%;
      background-color: #ccc;
   } 
  </style>
</head>
<body>
  <div class="father">
   <!--注意先放了右边吖-->
    <div class="right"></div>
    <div class="left"></div>
  </div>
</body>
</html>

3. 左右两边固定中间自适应的方法

  • 定位法
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <style>
   .outer{
     width: 100%;
     height: 300px;
     background-color: skyblue;
     position: relative;
     padding: 0px 200px;
     box-sizing: border-box;
   }
   .left{
    width: 200px;
     height: 80%;
     background-color: #fe2;
     position: absolute;
     left: 0;
    top:0;

   }
   .right{
     width: 200px;
     height: 80%;
     position: absolute;
     background-color: #dae;
     right: 0px;
     top:0px;
   }  
  .mid{
     overflow: hidden;
     height: 100%;
     background-color: lightgreen;
  } 
  </style>
</head>
<body>
  <div class="outer">
    <div class="left"></div>
     <div class="mid"></div>
    <div class="right"></div>
  </div>
</body>
</html>
  • flex法
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <style>
   .box{
     display: flex;
     width: 100%;
     height: 400px;
     background-color: lightblue;
   }
   .left{     
      height: 80%;
      width: 200px;
      background-color: yellow;     
   }
   .right{
      float: left;
      height: 80%;
      width: 200px;
      background-color: #ccc;     
   }
   .center{      
     height: 200px;
     background-color: lightpink;    
     flex: 1;
   }
  </style>
</head>
<body>
 <div class="box">
    <div class="left"></div>
    <div class="center"></div>
    <div class="right"></div>
 </div>
</body>
</html>
  • bfc 法 (注意是左右先写浮动的元素,它们脱标了,后面的标准流的元素才会上去)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <style>
   .outer{
     width: 100%;
     height: 300px;
     background-color: #ad0;
   }
   .left{
    width: 200px;
     height: 90%;
     background-color: rgb(230, 230, 115);
    float: left;
   }
   .right{
     width: 200px;
     height: 90%;
     background-color: lightgrey;
     float: right;
   }
   .mid{  
     overflow: hidden;
     height: 100%;
     background-color: #348;
   }
  
  </style>
</head>
<body>
  <div class="outer">  
    <div class="left"></div>
    <div class="right"></div>
    <div class="mid"></div>
  </div>
</body>
</html>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值