使用 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,则项目将垂直居中:

立即学习“前端免费学习笔记(深入)”;

.container {
  display: flex;
  align-items: center;
}
登录后复制

justify-content 属性

justify-content 属性用于设置容器中项目(flex item)的水平对齐方式。如果将 justify-content 设置为 center,则项目将水平居中:

.container {
  display: flex;
  justify-content: center;
}
登录后复制

将整体居中

要将整体居中,需要同时设置 align-items 和 justify-content 属性:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
}
登录后复制

示例

以下示例演示了如何使用 CSS 将网页中的所有内容居中:

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
    }
  </style>
</head>
<body>
  

Hello, world!

</body> </html>
登录后复制

以上就是css怎么设置整体居中的详细内容,更多请关注慧达安全导航其它相关文章!

点赞(0)

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部