源代碼
點擊運行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>教程</title> <style> .center { line-height: 200px; height: 200px; border: 3px solid green; text-align: center; } .center p { line-height: 1.5; display: inline-block; vertical-align: middle; } </style> </head> <body> <h2>居中</h2> <p>以下實例中,我們讓 line-height 屬性值和 height 屬性值相等來設置 div 元素居中:</p> <div class="center"> <p>我是垂直居中的。</p> </div> </body> </html>
運行結果