CSS中margin的認識

來源:互聯網
上載者:User

標籤:margin   負數的margin值   div置中   

  

   margin中設定邊框的順序為上右下左

{margin:top right buttom right}

     等同於單獨設定中的

{  margin-top: 20px;  margin-right: 30px;  margin-bottom: 30px;  margin-left: 20px;  }
    省略的寫法規則為:

        如果為外邊距指定了 3 個值,則第 4 個值(即左外邊距)會從第 2 個值(右外邊距)複製得到。如果給定了兩個值,第 4 個值會從第 2 個值複製得到,第 3 個值(下外邊距)會從第 1 個值(上外邊距)複製得到。最後一個情況,如果只給定一個值,那麼其他 3 個外邊距都由這個值(上外邊距)複製得到。


    設定margin為負值

    (以下是網上拷貝的一幅圖)


    margin設定成負值的移動效果。

    下面是一個DIV置中的例子

    介紹設定Div的置中效果:首先將Div以絕對布局或者fixed的方式設定為“置中”,這裡的置中只是代表height跟width設定了50%; 其次計算DIV的寬和高的1/2; 最後設定margin-top和margin-left為第二步值的負數;

    第一步:

.loginModal {    display: none;    background: #FDF3C1;    color: #000000;    padding: 20px;    border: 20px solid #8F6031;    font-size: 1.2em;    float: left;    position: fixed;    height: 175px;    width: 300px;    top: 50%;    left: 50%;    z-index: 200;}
    第二步:

 var modalMarginTop = ($('#' + modalID).height() + 80) / 2;        var modalMarginLeft = ($('#' + modalID).width() + 80) / 2;        /* apply the margins to the modal window */        $('#' + modalID).css({            'margin-top' : -modalMarginTop,            'margin-left' : -modalMarginLeft        });

    總結:1.設定成負數margin的元素,系統如何表現出負數的特徵外邊距。因為元素本身並不會發生變化,所以相當於元素的parent會向指定的相反的方向移動,達到效果(個人觀點)。


CSS中margin的認識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.