html - 如何在标题元素下对齐内容元素?
问题描述
是否可以通过具有固定宽度的 CSS 实现以下对齐,所以当它们的标题和内容不在同一个 div 中时,这两行会在彼此下方?
<section>
<div>
<div>Header Text 1</div>
<div>Header Text 2</div>
<div>Header Text 3</div>
<div>Header Text 4</div>
<div>Header Text 5</div>
</div>
<div>
<div>Content 1</div>
<div>Content 2</div>
<div>Content 3</div>
<div>Content 4</div>
<div>Content 5</div>
</div>
</section>
解决方案
事实上,你可以,为了更简化这个过程,你可以打破你的 html 代码的结构,然后使用Flexbox(不是必需的,但它很方便)相应地设置它们的样式。
html
<section>
<div id="first_row">
<div>
Element
</div>
<div>
Element
</div>
<div>
Element
</div>
</div>
<div id="second_row">
<div>
Element
</div>
<div>
Element
</div>
<div>
Element
</div>
</div>
<div id="third_column">
<div>
Element
</div>
<div>
Element
</div>
<div>
Element
</div>
</div>
</section>
CSS:
#first_row {
width: 500px;
background: orange;
height: 90px;
display: flex;
justify-content: space-evenly;
align-items: center;
}
#first_row > div {
background: yellow;
width: 100px;
height: 50px;
text-align: center;
}
#second_row {
width: 500px;
background: red;
height: 90px;
display: flex;
justify-content: space-evenly;
align-items: center;
}
#second_row > div {
background: yellow;
width: 100px;
height: 50px;
text-align: center;
}
#third_column {
width: 200px;
background: lightblue;
height: 200px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-evenly;
}
#third_column > div {
background: yellow;
width: 100px;
height: 50px;
text-align: center;
}
推荐阅读
- android - Gradle 同步在 Windows 10 上的 Android Studio 4、0、1 中失败
- javascript - 在反应中使用功能显示通知/模式
- javascript - 我们如何使用 for 循环初始化多个变量?
- excel - 从动态表中提取信息到静态稳定
- php - 如何正确地将外语名称编码为 php 中 .txt 文件的名称
- python - 解码csv文件中的base64字符串
- react-native - React: TypeError: undefined is not a function ('...events.map...'附近)。我是否正确传递了这组对象?
- javascript - forEach loop skips modal opening
- javascript - 我可以通过单击一个按钮来调用一个组件,我可以直接在 onclick 事件值中传递一个组件吗?
- javascript - 我可以用 JSDOM 欺骗 instanceof 吗?