移动开发-两栏自适应

25 篇文章 0 订阅
11 篇文章 0 订阅

左边固定,右边自适应的3种总结:

  1. 左边左浮动,右边加个overflow:hidden;
    #lt{ float: left;width:200px; background: #ff0;}
    #rt{ overflow: hidden; background: #f0f;}
  2. 左边左浮动,右边加个margin-left;
    #lt{ float: left; width:200px; background: #ff0;}
    #rt{ margin-left: 200px; background: #f0f;}
  3. 左边绝对定位,右边加个margin-left;
    #lt{ position: absolute; top:0; left:0; width:200px; background: #ff0;}
    #rt{ margin-left: 200px; background: #f0f;}

方法1: padding实现

一栏定位+固定宽度,另一栏用padding实现

(如果是用定位+百分比来做的话,在移动端,所有的border都属于content,不好处理)

<style>
    .box {
       min-width: 320px;
        max-width: 640px;
        height: 500px;
        border: 1px solid #000;
        margin: 0 auto;
        position: relative;
    }
    .boxFix {
        height: 100%;
        width: 100px;
        background-color: red;
        position: absolute;
        top: 0;
        left: 0;
    }
    .boxMove {
        height: 100%;
        background-color: sandybrown;
        padding-left: 100px;
    }
</style>

<div class="box">
<div class="boxFix">固定栏</div>
<div class="boxMove">padding实现</div>
</div>

方法二:利用overflow:hidden

overflow:hidden
有个特性:让当前盒子绝对独立,绝缘。不会受到浮动元素的影响。

/*文字围绕现象:/*
<style>
    .boxFix {
        width: 50px;
        height: 50px;
        float: left;
        background-color: sandybrown;
    }

</style>

<body>
<div class="boxFix"></div>
<div class="boxMove">内容此处省略无数字</div>
</body>

文字围绕现象:

但是当给boxMove添加overflow:hidden之后,就能实现左边盒子固定,右边boxMove盒子宽度自适应:

/*只需要CSS样式加这一条:*/
.boxMove {
        overflow: hidden;
    }

同时,元素原本的流体特性依然保留了。也就是,虽然不与浮动交集,自动退避浮动元素宽度的距离,但,本身作为普通元素的流动性依然存在,反映在布局上就是自动填满除去浮动内容以外的剩余空间。哟,这不就是自适应布局嘛!!

BFC

  • BFC:Block Formatting Context中文为块级格式化上下文
  • BFC表现规则:内部子元素再怎么翻江倒海都不会影响外部的元素
  • 什么时候会触发BFC呢?

常见的如下:

  • float的值不为none。
  • overflow的值为auto,scroll或hidden。
  • display的值为table-cell, table-caption, inline-block中的任何一个。
  • position的值不为relative和static。

而刚刚的例子则是:跟随div添加overflow:hidden触发BFC

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值