塊級元素水平,垂直居中的兩種方式


方式一: 利用margin

<!DOCTYPE html>
<html>
    <head>
        <title>塊級元素水平,垂直居中</title>
        <meta charset="utf-8">
        <style>
        	.wrapper {
        		height: 600px;
        		border: 1px solid gray;
        	}
        	.box {
        		width: 100px;
        		height: 100px;
        		background: gold;
        		margin: 250px auto 0;
        	}
        </style>
    </head>
    <body>
		<div class="wrapper">
			<div class="box"></div>
		</div>
    </body>
</html>

 

方式二,利用定位及負邊距

<!DOCTYPE html>
<html>
    <head>
        <title>塊級元素水平,垂直居中</title>
        <meta charset="utf-8">
        <style>
        	.wrapper {
        		height: 600px;
        		border: 1px solid gray;
        		position: relative;
        	}
        	.box {
        		width: 100px;
        		height: 100px;
        		background: gold;
        		position: absolute;
        		left: 50%;
        		top: 50%;
        		margin-left: -50px;
        		margin-top: -50px;
        	}
        </style>
    </head>
    <body>
		<div class="wrapper">
			<div class="box"></div>
		</div>
    </body>
</html>

  


免責聲明!

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



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