布局方案分析

完成两个div的左右布局,其中第一个div的宽度为200px,第二个div的宽度设置为剩余的宽度。

1、在不考虑ie低版本的兼容性的前提下,是可以使用弹性盒子来完成的。

<div style="width: 100%;display:-webkit-box;display: -moz-box; margin-top:20px ;">
            <div class="boxone" style="width:200px;height: 100px;background: red; ">1</div>
            <div class="boxtwo" style="-webkit-box-flex:1;-moz-box-flex: 1; height: 100px;background: green; ">2</div>
             
 </div>

2、在考虑到兼容性的问题的时候可以使用加边界的方式来处理该问题。

 <style> 
            *{
                box-sizing: border-box; 
            }
        </style>
    </head>
    <body style="margin: 0px; ">
        <div style="width: 100%;">
            <div style="width: 200px;height:100px;position:absolute; top 0; background: red; ">red</div>
            <div style="width: 100%;padding-left: 200px; height:100px; background-color: green;">
            </div>

</body>

首先,第一个div采用absolute定位来跳出文档流,这样第二个div就会出现浮动。这时候把第二个div的宽度设置为100%。

为了保证宽度包括了内边距和边框的宽度。添加盒子模型 box-sizing: border-box; 

再次强调:!!!盒子模型不包括外边距。

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            
            *{
                box-sizing: border-box; 
            }
        </style>
    </head>
    <body style="margin: 0px; ">
        <div style="width: 100%;">
            <div style="width: 200px;height:100px;position:absolute; top 0; background: red; ">red</div>
            <div style="width: 100%;padding-left: 200px; height:100px; background-color: green;">
                
            </div>
            
        </div>
        <div style="width: 100%;display:-webkit-box;display: -moz-box; margin-top:20px ;">
            <div class="boxone" style="width:200px;height: 100px;background: red; ">1</div>
            <div class="boxtwo" style="-webkit-box-flex:1;-moz-box-flex: 1; height: 100px;background: green; ">2</div>
                
            </div>
            
        </div>
        </body>
    <script>
    </script>
</html>

3、采用js来解决该问题,但是考虑到左边固定宽度的情况,我们能用css实现就尽量不采用js实现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值