【发布时间】:2017-06-07 10:33:57
【问题描述】:
下面的代码说明了这两种不同的布局。我的问题是我无法在不更改标记的情况下复制这些布局。我想知道是否有一些奇特的 flexbox 方式我可以在只使用一个 html 方案的情况下完成这一点。注意:容器需要具有动态高度。只要实现了所需的布局,解决方案就不必使用 flexbox。
main {
width: 750px;
max-width: 100%;
margin: auto;
border: solid 1px black;
display: flex;
flex-wrap: wrap;
}
.a {
background: red;
width: 40%;
}
.b {
background: blue;
width: 60%;
}
.c {
background: green;
}
.a-mobile {
background: red;
width: 40%;
}
.b-mobile {
background: blue;
width: 60%;
}
.c-mobile {
background: green;
width: 100%;
}
<h2>Desktop</h2>
<main>
<div class="a">a</div>
<div class="b">b
<div class="c">c</div>
</div>
</main>
<h2>Mobile</h2>
<main>
<div class="a-mobile">a-mobile</div>
<div class="b-mobile">b-mobile</div>
<div class="c-mobile">c-mobile</div>
</main>
【问题讨论】:
-
答案是肯定的,如果你可以在容器上设置一个高度。这是一个选项吗?
-
有了浮动和弹性盒,这在不需要高度的情况下是可能的。
-
@Michael_B 不幸的是,容器上需要一个动态高度。
-
那么,如果不更改标记,目前无法实现纯 flex 解决方案。
-
@AndreiGheorghiu 这是个好消息,你能给我举个例子吗?