1. 程式人生 > >hmtl div水平、垂直居中

hmtl div水平、垂直居中

最近寫網頁經常需要將div在螢幕中居中顯示,遂記錄下幾個常用的方法,都比較簡單。
水平居中直接加上<center>標籤即可,或者設定margin:auto;當然也可以用下面的方法

下面說兩種在螢幕正中(水平居中+垂直居中)的方法
放上示範的html程式碼:

<body>
    <div class="main">
        <h1>MAIN</h1>
    </div>
</body>

    方法一:

div使用絕對佈局,設定margin:auto;並設定top、left、right、bottom的值相等即可,不一定要都是0。

.main{
    text-align: center; /*讓div內部文字居中*/
    background-color: #fff;
    border-radius: 20px;
    width: 300px;
    height: 350px;
    margin: auto;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
---------------------
作者:qq_32623363
來源:CSDN
原文:https://blog.csdn.net/qq_32623363/article/details/77101971
版權宣告:本文為博主原創文章,轉載請附上博文連結!