html - 跨 div 垂直对齐文本
问题描述
我很确定这是一个愚蠢的问题,但我无法弄清楚。我有两个并排的 div,具有不同大小的标题,我希望每个 div 中的顶部标题与基线对齐。但是vertical-align: baseline
似乎不适用于跨div。这是我的代码(css 是 sass):
.about-page-column {
display: inline-block;
vertical-align: top;
width: 49%;
margin-top: 5px;
text-align: center;
padding: 5px;
h3,h4 {
padding: 5px;
vertical-align: baseline; // this line obviously isn't doing anything
font-family: 'Rubik', sans-serif;
text-decoration: underline;
color: $page-title-color;
}
h3 {
font-size: 2em;
}
h4 {
font-size: 1.5em;
}
}
<div>
<div class="about-page-column">
<h3>Experience</h3>
</div>
<div class="about-page-column">
<h4>Links</h4>
</div>
</div>
这是结果。标题由它们的顶部对齐,因为它们浮动到vertical-align: top
div 的顶部。
如何在各自的 div 中对齐标题,以便它们与基线垂直对齐?如果这是不可能的,有什么解决方法?
谢谢。
解决方案
我会使用弹性容器。在这种情况下,您的第一个 div
<div class="flex-container">
<div class="about-page-column">
<h3>Experience</h3>
</div>
<div class="about-page-column">
<h4>Links</h4>
</div>
</div>
.flex-container {
display: flex;
flex-direction: row; /*this is the default behavior of flex, you don't need to write it */
justify-content: center; /*This align the items horizontally*/
align-items: center; /*This align the items vertically or you can use baseline*/
}
如果 flex-direction 设置为 column,justify-content 和 align-items 会交换它们的功能。
您可以在此处了解更多信息:
https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Flexbox
推荐阅读
- javascript - 使用缩放在 p5.js 中翻转图像
- github - 为什么我的 gh-pages 的 unfurl 'og:image' 没有出现在任何平台上?
- python - 动态网格创建
- audio - 如何使用 ffmpeg 连接 3 个 mp4 文件(在结果视频中面临错误)?
- java - 从左到右而不是从右到左扫描字符串计算总数
- python-3.x - 从 Txt 文件中读取字典时出现 ValueError
- azure-devops - Azure Devops - 创建在所有其他触发管道之后运行的集成测试管道
- python - 如何根据从 jupyter lab/ipywidgets 的下拉列表中选择选项来创建嵌套小部件?
- ruby - 程序无响应但仍在运行
- python - 使用 Pandas 快速循环 - uszipcode