html-如何讓背景寬高100%元素上下左右居中


在一些登陸,注冊頁面中經常會出現這種布局模式,背景圖片或者背景顏色寬度,高度100%鋪開;

中間添加div上下左右居中,今天就帶着大家做這樣一個效果

 

 

<style>
        body,p,h1{
            padding: 0;
            margin: 0;
        }
        html,body{
            height: 100%;
            width: 100%;
        }
        html{
            height: 100%;
            width: 100%;
        }
        .div1{
            width: 100%;
            height: 100%;
            background-color: teal;
        }
        .div2{
            position: fixed;
            width: 300px;
            height: 400px;
            margin: auto;
            background-color: tomato;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }
    </style>
<body>
    <div class="div1">
        <p class="div2">居中元素</p>
    </div>
</body>

 

其中position: fixed;是必須要加的,但是用absolute和relative也是可以的,但是一般情況下,添加的窗口是固定在屏幕的正中間,所以一般使用fixed


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM