如何让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>
相关推荐
-
Nginx 原理和架构 | 原力计划2025-04-12 01:23:13
-
PHP极速匹配子字符串,你是怎么做的?2025-04-12 01:10:40
-
nginx的安装 教程2025-04-12 00:59:04
-
MySQL中是如何实现事务提交和回滚的?2025-04-12 00:54:21
-
nginx 配置白黑名单与配置访问频率和连接数2025-04-12 00:41:59