两边固定,中间自适应

运用float,左右浮动,中间采用margin-left 和 margin-right
<div class="main"> 
                <div class="right">这是右侧的内容 固定宽度</div>
                <div class="left">这是左侧的内容 固定宽度</div>
                <div class="center">中间内容,自适应宽度</div>
 
 </div>
-------------------------css--------------------------
<style>
        body{
            margin:0;
            padding:0;
        }
        .main{
            width:100%; 
            margin:0 auto;
         
        }
        .left{
            width:150px; 
            float:left; 
            background:red;
            height: 60px;
        }
        .center{
            margin-left:150px;
            margin-right:200px;
          background-color:pink;
         height: 60px;
        }
        .right{
            width:200px;
             float:right; 
             background-color:blue;
             height: 60px;
        }
    </style>
   三个div的顺序:center必须是最后一个,即HTML的文档结构可以是left——right——center也可以是right——left——center,但绝对不能是left——center——right
二、左右两侧采用绝对定位 中间同样采用margin-left margin-right方法

<div class="main"> 
                <div class="left">这是左侧的内容 固定宽度</div>
                <div class="center">中间内容,自适应宽度</div>
                <div class="right">这是右侧的内容 固定宽度</div>
</div>
 
------------------------------css-------------------------------------
 <style>
        body{
            margin:0;
            padding:0;
        }
        .main{
            width:100%; 
            margin:0 auto;
         
        }
        .left ,.right{
            position: absolute;
            top:0;
            width: 180px;
            height: 100%;
        }
        .left{
           left: 0;
            background:red;
           
        }
        .center{
            margin-left:180px;
            margin-right:180px;
           
        }
        .right{
             right:0;
             background-color:blue;
            
        }
    </style>
     left、right采用绝对定位(left固定在左边,right固定在右边),所以left、right乖乖待在左右两边;center设置了左右margin(如果不设置会覆盖left,right) ,margin的位置刚好放left和right。
三、flex布局

<div class="main"> 
                <div class="right">这是右侧的内容 固定宽度</div>
                <div class="center">中间内容,自适应宽度</div>
                <div class="left">这是左侧的内容 固定宽度</div>      
             </div>
--------------------------------CSS----------------------------
 .main{
            display: flex;
            width:100%; 
            margin:0 auto;
         
        }
        .left{
            width:150px; 
            height: 60px;
            background:red;
        }
        .center{
            flex: 1;
            background-color:pink;
             height: 60px;
        }
        .right{
            width:200px;
            height: 60px;
            background-color:blue;
            
        }
左右宽度设置固定,中间div通过设置flex:1让其占满剩余空间,达到左右固定,中间自适应的效
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值