使用 CSS 实现整体居中:设置 align-items: center 进行垂直居中。设置 justify-content: center 进行水平居中。同时设置 align-items 和 justify-content 属性可将整体居中。

如何使用 CSS 设置整体居中
在网页设计中,有时需要将所有内容居中对齐,这可以通过使用 CSS 的 align-items 和 justify-content 属性来实现。
align-items 属性
align-items 属性用于设置容器中项目(flex item)的垂直对齐方式。如果将 align-items 设置为 center,则项目将垂直居中:
立即学习“前端免费学习笔记(深入)”;
<code class="css">.container {
display: flex;
align-items: center;
}</code>justify-content 属性
justify-content 属性用于设置容器中项目(flex item)的水平对齐方式。如果将 justify-content 设置为 center,则项目将水平居中:
<code class="css">.container {
display: flex;
justify-content: center;
}</code>将整体居中
要将整体居中,需要同时设置 align-items 和 justify-content 属性:
<code class="css">.container {
display: flex;
align-items: center;
justify-content: center;
}</code>示例
以下示例演示了如何使用 CSS 将网页中的所有内容居中:
<code class="html"><!DOCTYPE html>
<html>
<head>
<style>
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
</style>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html></code>以上就是css怎么设置整体居中的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号