【发布时间】:2019-04-25 07:33:34
【问题描述】:
我在使用 flexbox 时遇到了问题。我试图对齐两个元素;一个在容器的顶部,另一个在中心。大多数 flexbox 示例都使用三个元素,而不是两个元素。所以我尝试了自己的解决方案。
#main {
display: flex;
flex: 1;
flex-direction: column;
justify-content: space-between;
height: 100px;
background-color: #dfdfdf;
}
#box1 {
display: flex;
justify-content: flex-end;
background-color: #ff0000;
}
#box2 {
display: flex;
background-color: #00ff00;
}
#dummy {
display: flex;
opacity: 0;
}
<div id="main">
<div id="box1">box1</div>
<div id="box2">box2</div>
<div id="dummy">dummy</div>
</div>
...我也将它应用到水平大小写。
#main {
display: flex;
flex: 1;
flex-direction: row;
justify-content: space-between;
height: 100px;
background-color: #dfdfdf;
}
#box1 {
display: flex;
justify-content: flex-end;
background-color: #ff0000;
width: 200px;
}
#box2 {
display: flex;
background-color: #00ff00;
width: 100px;
}
#dummy {
display: flex;
opacity: 0;
width: 200px;
}
<div id="main">
<div id="box1">box1</div>
<div id="box2">box2</div>
<div id="dummy">dummy</div>
</div>
但是,它需要一个无用的虚拟元素。我认为这不是一个好主意:(
有没有更好的办法解决这个问题?
【问题讨论】: