html - 调整页面大小时如何在 CSS 中将这些框居中?
问题描述
我有以下箱子(文章)
当我尝试调整页面大小时,它变成了这样:
我希望它在调整窗口大小时自动居中以避免右侧的巨大间隙,但它需要与它已经采用相同的格式。基本上只需将整个组移动到中心而不使单个框居中,因为我希望它们像图片中所示那样浮动到左侧。
编辑:这就是我所追求的:
这是我当前的CSS:
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
.container {
width: 90%;
height: 100%;
overflow: hidden;
background: skyblue;
margin: auto;
padding: 20px;
}
.container article {
float: left;
width: 250px;
height: 250px;
background: blue;
margin: 25px
}
和我的html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Shop</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<article>
<h2>Hello</h2>
</article>
<article>
<h2>Hello</h2>
</article>
<article>
<h2>Hello</h2>
</article>
<article>
<h2>Hello</h2>
</article>
<article>
<h2>Hello</h2>
</article>
</div>
</body>
</html>
解决方案
使用 flex,卢克
display: flex;
flex-wrap: wrap;
justify-content: center;
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
.container {
width: 90%;
height: 100%;
overflow: hidden;
background: skyblue;
margin: auto;
padding: 20px;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.container article {
width: 250px;
height: 250px;
background: blue;
margin: 25px
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Shop</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<article>
<h2>Hello</h2>
</article>
<article>
<h2>Hello</h2>
</article>
<article>
<h2>Hello</h2>
</article>
<article>
<h2>Hello</h2>
</article>
<article>
<h2>Hello</h2>
</article>
</div>
</body>
</html>
推荐阅读
- sql - PostgreSQL 中的更新规则和“列引用不明确”错误
- c# - 使用 Unity C# 动态获取对象
- javascript - 在 AngularJS 中使用 ng-app 指令的问题
- alluxio - 每次我重新启动我的alluxio机器时,大师都无法启动
- regex - 根据空格和括号从字符串中提取单词
- javascript - 如何使用 React Hooks API 管理异步函数
- typescript - 如何使用 ionic 4 在网络浏览器中打开 pdf?
- python - 如何将 tf.Tensor 从最大池化层转换为 numpy 数组?
- python - “numpy.ndarray”对象没有属性“drop”
- python - 如何重定向到 POST url 并在 django 中传递数据