如何让div盒子里的内容水平居中,垂直居中。
100人浏览 2024-11-13 09:18:29
一、盒子里的字,默认是位于盒子内的左上角,如何让它水平居中,垂直居中呢。
- 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>
相关推荐
-
PHP8种变量类型的详细讲解2025-02-22 00:32:24
-
php+apache 和 php+nginx的区别2025-02-22 00:21:27
-
PHP:与workerman结合实现定时任务2025-02-22 00:15:57
-
Nginx的Rewrite规则与实例2025-02-22 00:15:39
-
MySql中身份证字段的简单脱敏介绍2025-02-22 00:15:36