css讓一個正方形方塊垂直居中


        這里有top和margin-top的區別,top(left,right,bottom)是絕對定位,要用position,margin-top是相對定位,相對於相鄰的元素或者父元素。

        代碼如下:

        

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>square at center</title>
<style type="text/css">
 #square{
 	 position:absolute;
     top:50%;
     left:50%;
     margin-top:-50px;
     margin-left:-50px;
     width:100px;
     height:100px;
     background-color:red;
  }
</style>
</head>
<body>
    
      <div id="square">
      </div>

</body>
</html>

  

 


免責聲明!

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



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