居中的程式碼怎麼寫
- 綜合知識
- 關注:2.86W次
1. div居中程式碼怎麼寫html文字居中測試 <meta charset="UTF-8"> <style type="text/css"> body{background: #ddd;} div{width:300px;height:180px;margin:10px auto;color:#fff;font-size:24px;} .box1{background: #71a879;text-align: center;} .box2{background: #6a8bbc;line-height: 200px;} .box3{background: #dea46b;text-align: center;line-height: 200px;} </style> <body> html文字水平居中 html文字垂直居中 html文字水平上下居中
如果只是水平居中的話,只需要先定義DIV的寬度,再設定外邊距margin屬性的左右邊距為auto即可。此時DIV便會相對於它的父容器水平居中。
比如 <div class=div_a><div class="div_b"><div></div>
設定div_b的樣式
.div_b{width:100px; margin:0 auto;}
這樣div_b便在div_a中水平居中了。如果外面沒有父DIV,則div_b便相對於整個頁面居中。注意如果div_b定義了浮動屬性float則無效。
2. 網頁文字居中的程式碼是什麼