css外邊距屬性有哪些?都有哪些用處?(詳解)

來源:互聯網
上載者:User
我們在進行網頁頁面配置時,相信做過頁面設計的朋友都知道,margin屬性是css中非常重要的一個樣式屬性,那麼有的新手可能就會問margin屬性是什嗎?

css樣式中的margin也就是css中外邊距外間距屬性,顧名思義,就是控制div塊或者各元素之間上下左右的距離,顯然margin在css中的作用不容小覷。本篇文章就給新手小白介紹關於css margin屬性的具體用法。希望對大家有所協助。

下面我們通過具體的程式碼範例詳細解說

css中margin屬性css外邊距屬性的具體用法程式碼範例:

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>css中的margin屬性使用樣本</title>    <style>        .demo{            width: 300px;height: 100px;            background-color: #ccccff;        }        .demo1{               margin-left:50px;               margin-top: 50px;               margin-bottom: 50px;               width: 300px;height: 100px;               background-color: #b2ecef;           }        .demo2{            width: 300px;height: 100px;            background-color: #94ef9a;        }        .demo1 p{margin-left: 50px;     }     </style></head><body><div class="demo">    <p>沒有給這個div塊設定margin值</p></div><div class="demo1">    <p>給這個div塊設定了margin值,並且給此段文字也設定margin值</p></div><div class="demo2">    <p>沒有給這個div塊設定margin值</p></div></body></html>

以上代碼在瀏覽器中呈現效果如:


我們可以發現,給元素設定了margin值後,div塊之間的間距發生了變化。div塊demo1因為設定了margin-left的樣式屬性後,元素距離瀏覽器左邊移動了50個像素,設定了margin-top後,元素距離上面的div塊也移動了50個像素,然後又給添加了margin-bottom樣式屬性,距離下面的div塊上邊框移動了50個像素,div塊demo1以此產生了上左下的外邊距。同時我們也給第二段文字添加了margin-left的樣式屬性後,文字在demo1中距離div左邊框移動了50個像素也產生了外邊距。

margin的值有上下左右可以選擇,當然如果我們直接給div添加margin:50px;這個屬性的話,同樣可以出來上面的效果。因為當你預設給margin只添加一個值時,他就相當於四邊距離是同一個值。或者我們添加margin:50px 50px 50px 50px;的話,這個就相當於css中細分的margin-top、margin-right、margin-bottom、margin-left;預設的是,上右下左的外邊距也就是順時針方向。

那麼以上就是關於css樣式中margin屬性也就是css中外邊距屬性的具體用法介紹,具有一定的參考價值,希望對有需要的朋友有所協助!

相關文章

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.