<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>垂直居中</title> <style> .parent{width: 400px;height: 400px;border:1px solid grey;display: } .children{width: 200px;height: 200px;background-color: #ccc;} /*table-cell*/ .parent{display: table-cell;text-align: center;vertical-align: middle;} .children{display: inline-block;} /*flex*/ .parent{display: flex;justify-content: center;align-items: center;} /*position transform*/ .parent{position: relative;} .children{position: absolute;top:50%;left: 50%;transform:translate(-50%,-50%);} /*position margin 不推荐使用,使用必须设置宽高*/ .parent{position: relative;} .children{position: absolute;top:0;left: 0;bottom: 0;right: 0;margin: auto;} </style> </head> <body> <p class="parent" > <p class="children"> </p> </p> </body> </html>
以上就是四种css垂直居中方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号