1. 程式人生 > >CSS---讓div居中的方法

CSS---讓div居中的方法

第一種方式:設定body 居中。在CSS中的程式碼是(body{text-align:center;})
第二種方式:用盒子模型,首先設定一個Div ,這個DIV的寬度為100%,然後在這個DIV居中,那麼在這個DIV中加的內容就居中顯示,程式碼如下:
<div class="div1">
<div class="div2"></div>
</div>

CSS 樣式程式碼:
<style type="text/css">
.div1{text-align:center;width:100%;}
.div2{width:980px;background:red;} //為了看清效果,加了背景顏色
</style>

第三種方式:margin:0 auto;