如何让div盒子里的内容水平居中,垂直居中。
一、盒子里的字,默认是位于盒子内的左上角,如何让它水平居中,垂直居中呢。
- text-align: center;/* 这是让内容水平居中 */
- line-height: 200px; /* 这是让内容垂直居中,只要把行高设置得和盒子的高度一样 */

图1

图2
以下是我在vscode下做的代码:

图3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .hz{ width: 300px; height: 200px; background: #f00; margin: 100px 0px 0px 200px; text-align: center; /* 这是让内容水平居中 */ line-height: 200px; /* 这是让内容垂直居中,只要把行高设置得和盒子的高度一样 */ } </style> </head> <body> <div class="hz">我是盒子里的字</div> </body> </html>