CSS负边距

2017-11-24  本文已影响0人  KiraStar

原文链接:
负边距详解


在static元素中使用负边距

static元素:没有使用过float的元素


image
 /* Moves the element 10px upwards */
<div style="width: 100px;height: 100px;margin-top: -10px;overflow: hidden;">
    <p>item11112222222</p>
    <p>item33333444444</p>
    <p>item55555666666</p>
</div>

前后对比:


移动前
移动后
/*
将第一个div后面的元素全部向上拉50px,覆盖掉前一个div
*/
<div style="width: 100px;height: 100px;overflow: hidden;margin-bottom: -50px;">
    <p>item11112222222</p>
    <p>item33333444444</p>
    <p>item555556666666</p>
</div>
<div style="width: 100px;height: 100px;overflow: hidden;">
    <p>item11112222222</p>
    <p>item33333444444</p>
    <p>item555556666666</p>
</div>

前后对比:


移动前
移动后

在浮动元素中使用负边距

index.html:

    <div class="left">
        <div class="left-element">item1</div>
    </div>
    <div class="right">item2</div>
css:
.left {
    width: 100%;
    float: left;
}

.left-element {
    height: 100px;
    background-color: red;
    margin-right: 100px;
}

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


<div id="mydiv1">First</div>
<div id="mydiv2">Second</div>

解决bug


上一篇 下一篇

猜你喜欢

热点阅读