javascript - justify-content 没有预期的行为,我做错了什么?
问题描述
我想把一个放在页面中心h1
的div
另一个下面。为此,我使用了以下 CSS 和 HTML:
.main {
display: flex;
flex-direction: column;
justify-content: center;
width: 100%;
height: 100%;
}
h1 {
display: flex;
margin-block-start: 0;
width: 40%;
}
.products {
margin-top: 5rem;
width: 40%;
display: flex;
flex-direction: row;
justify-content: center;
}
<!--
Commented because reactjs className does not work for snippet so replace by normal class
<div className="main">
<h1>Products</h1>
<div className="products">
</div>
-->
<div class="main">
<h1>Products</h1>
<div class="products">
</div>
它们与列对齐,但未居中。
解决方案
在列方向弹性容器中时,需要align-items: center
用于水平对齐。
.main {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
height: 100%;
}
推荐阅读
- docker - .NET Core - Path.Combine 中的文件路径在 Docker 中不起作用
- c++ - “释放你的功能”方法:IDE 可以提供提示吗?
- python - 从 requirements.txt 安装时出现“没有名为 numpy 的模块”错误
- amazon-s3 - 上传失败到 s3 失败,没有描述
- angular - angulario rxjs 转换 http 结果
- c# - 如何将字符串传递给基本层次结构头类
- angular - 等待用户按下确认/拒绝然后做一些事情
- tensorflow - 使用 tf.slim 从 vgg 部分恢复图层
- java - MapStruct:将嵌套对象属性映射到属性
- swift - CVPixelBufferGetAddress 返回 Nil