在div里实现它的一个子div定宽,而另一个子div获取剩下的宽度

来源:互联网 发布:汽车销售软件 编辑:程序博客网 时间:2024/04/28 01:42

html:

<div class="include_AandB">
    <div class="A">
        
        </div>
        <div class="b">
       
        </div>
</div>


CSS:

.include_AandB{width:80%;background-color:red;height:100px;margin:0 auto;position:relative;}
.A{width:300px;background-color:green;height:100px;float:left;}
.B{background-color:#039;height:100px;position:absolute;right:0px;top:0;left:300px;}


说明:

include_AandB: 设置宽度和高度,然后利用position:relative让它成为被子div定位的参照元素。

A:设置固定宽度300px,左飘

B:利用position:absolute设置以include_AandB为参照物的绝对定位,然后再设置right:0使它右边距离include_AandB的宽度为0,设置left:300px使它左边距离A的长度为A的宽度。

综上:这样的话无论include_AandB的宽度如何变化,B始终能获取include_AandB除去A后剩下的宽度。


另:本人是菜鸟,这是今天刚想出来的方法,不喜勿喷,有哪里说得不对或者有更好的方法的话希望指出提出,谢谢。

1 0
原创粉丝点击