帝王谷资源网 Design By www.wdxyy.com

对于平均分布的布局,我们一般使用负margin的方法。如下图,就是平均布局。

css利用负margin实现平均布局的示例

一般这种情况,我们都是在父元素和子元素之间加上一层div,然后,给这个div设置负margin-right,值为两个子元素之间的距离。
例如,我们令每个子元素宽度为100px,一共3个子元素,给每个子元素一个margin-right,设为50px,那父元素宽度应为100x3+50x2=400px。上代码:

//HTML
<div class="father">
    <div  class="middle">
        <div  class="son1"></div>
        <div  class="son2"></div>
        <div  class="son3"></div>
    </div>
</div>
//CSS
.son1,.son2,son3{
    margin-right:50px;
    width:100px;
}
.father{
    width:400px;
}
.middle{
    margin-right:-50px;
}

中间层margin-right:-50px相当于向右“延伸”了50px,使得son3元素有空间和其他元素在一行。

标签:
css,负margin,平均布局

帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com