一定一自适应:左定宽,右自适应;左自适应,右定宽

两个div,一侧定宽一侧自适应,涉及的原理:①div有个默认的属性,即如果不设置宽度,那他会自动填满父标签的宽度(也可以说是块级元素的属性);②自适应区不能设置浮动,因为一旦浮动就不是块级元素了,而是行内块,失去了块级元素的默认属性;③position:absolute;绝对定位不受流式布局影响;④calc()用在CSS中动态计算长度值;


一、两个div,左侧定宽200px,右侧自适应:

<div class="left"></div>
<div class="right"></div>

1.第一种解决方案:

    定宽区浮动,自适应区不设宽 设margin

       .left {
            width:200px;
            height: 80px;
            background-color: red;
            float:left;
        }

        .right {
            height: 100px;
            background-color: blue;
            margin-left: 200px;
        }


2.第二种解决方案:

    定宽区绝对定位,自适应区不设宽 设margin  

       .left {
            position: absolute;
            width: 200px;
            height: 80px;
            background-color: red;
        }

        .right {
            height: 100px;
            background-color: blue;
            margin-left: 200px;
        }


3.第三种解决方案:

    外层加个父div display:table,子div display:table-cell;  

    <div class="outer">
        <div class="inleft"></div>
        <div class="inright"></div>
    </div>
       .outer {
            display:table;
            width:100%;
            min-height:100px;
        }
        .inleft {
            width:200px;
            background-color:red;
            display:table-cell;
        }
        .inright {
            background-color:blue;
            display:table-cell;
        }
   此方法只适用于两个div等高。

效果如下:



二、两个div,左侧自适应,右侧定宽200px:

对于右侧定宽,左侧自适应的两种方法照搬到这里是不起作用的,因为在流式布局中是从上到下,从左到右,所以对于左定右自适应还是比较占优势的。

下面看下怎么解决左自适应右定。


1.第一种解决方案:

    左侧宽度width用calc计算,右侧绝对定位 

       .left {
            height: 80px;
            margin-right: 200px;
            background-color: red;
            width: calc(100% - 200px);
            float: left;
        }

        .right {
            width: 200px;
            height: 100px;
            background-color: blue;
            position: absolute;
            right: 0;
        }

2.第二种解决方案:

   左右div换位置,左侧不设宽 设margin,右侧浮动

     <div class="right"></div>
     <div class="left"></div>
       .left {
            height: 80px;
            margin-right: 200px;
            background-color: red;
        }

        .right {
            width: 200px;
            height: 100px;
            background-color: blue;
            float: right;
        }

3.第三种解决方案:

    外层加个父div display:table,子div display:table-cell; 

    <div class="outer">
        <div class="inleft"></div>
        <div class="inright"></div>
    </div>
	.outer {
            display:table;
            width:100%;
            min-height:200px;
        }
        .inleft {
            background-color:red;
            display:table-cell;
        }
        .inright {
            width:200px;
            background-color:blue;
            display:table-cell;
        }
     此方法只适用于两个div等高的情况。


效果如下:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值