javascript - 使用 flexbox 垂直对齐内容
问题描述
我正在创建一个图块,其中图像可以是图块的 50% 或图块的 75%,并且内容将填充剩余的区域/剩余空间。我的问题是我不想为图像设置特定高度,因为它会失去图像/纵横比。我有如下内容:
const tile= () => (
<StyledWrapper>
<StyledImageBlock>
//get Image here
</StyledImageBlock>
<StyledTextContent>
{getTitleOne()} //this is a span element
{getTitleTwo()} //this is a span element
</StyledTextContent>}
</StyledWrapper>
);
在我的StyledWrapper
我有以下内容:
text-align: center;
a {
text-decoration: none;
}
在我的StyledImageBlock
我有以下内容:
width: 100%;
在我的StyledTextContent
我有以下内容:
align-content: center;
align-items: center;
display: flex;
flex-direction: column;
justify-content: center;
但是项目拒绝垂直对齐,我为演示设置了高度,但StyledWrapper
仍然height: 700px
拒绝。我在哪里错了?关于如何实现我所描述的任何建议。
更多信息。在图像和内容上设置百分比高度时,我可以达到预期的结果,但是这样图像纵横比会变得更糟/拉伸我不想要的图像。最终,这个 tile 将进入一个 flexbox 网格。
解决方案
You probably need to put
justify-content: center;
align-items: center;
on StyledWrapper. This will center the StyledImageBlock vertically and horizontally.
推荐阅读
- javascript - 即使使用 await,Firebase 函数也不会等待异步代码
- java - 使数组相等的最小计数
- sql - 需要在 SQL 中计算最终日期
- python - 为什么在这个 8 皇后算法中需要“list()”?
- python - 基础镜像满足 tensorflow 版本但 docker 仍然从 requirements.txt 下载 tensorflow
- php - 如何基于函数内的foreach循环在php中编写if条件?
- php - API 平台:规范化和非规范化继承映射的 Doctrine 实体
- flutter - Flutter:绘制弧线。如何修复我的圆圈方向并根据需要拆分它?
- sql - SQL查询查找仅包含另一列部分的列的总数?
- python - 如何将 DOCTYPE 添加到已经存在的元素树的第二行?