三 div / 两列布局 - 可以使用 Flexbox 吗?
Three div / two column layout - possible with Flexbox?
我一直在尝试使用 flexbox 实现下面的布局。我最初有一个包含图像和导航的左侧边栏,以及一个主要内容区域。在移动设备上,侧边栏用于包裹在主要内容之下。
问题是我需要图像在移动设备上保持在顶部,所以我一直在尝试在一个包装器中使用三个同级 div
s div
.
这甚至可以用 flexbox 还是我需要使用 css 网格?
尽管 CSS 网格是实现您想要的布局的最佳方法,但使用 CSS Flexbox 也是可能的。
您只需创建一个包装器 div
,其中包含三个 div
(当执行 移动优先 方法时)和 .content
设置为 flex: 1
以拉伸视口的高度。
然后对于桌面(在本例中为 @media screen and (min-width: 1000px)
),更改 .navigation
和 .content
的顺序 (MDN reference of order
) 并给出所有三个 div
根据自己的需要选择合适的宽度。 div.wrapper
的唯一变化是它需要 flex-flow: column wrap
才能正确换行。
.wrapper {
display: flex;
flex-direction: column;
height: 100%;
min-height: 100%;
}
.box {
display: flex;
}
.content {
flex: 1;
}
@media screen and (min-width: 1000px) {
.wrapper {
flex-flow: column wrap;
}
.navigation {
order: 2;
}
.content {
order: 3;
}
.image,
.navigation {
width: 200px;
flex: 50%;
}
.content {
width: calc(100% - 200px);
flex: 0 0 100%;
}
}
/* Generic styling */
html,
body {
height: 100%;
}
body {
margin: 0;
padding: 0;
}
.image {
background: orange;
height: 60px;
}
.content {
background: lightblue;
}
.navigation {
background: lightgreen;
height: 60px;
}
<div class="wrapper">
<div class="box image">Image</div>
<div class="box content">Content</div>
<div class="box navigation">Navigation</div>
</div>
我一直在尝试使用 flexbox 实现下面的布局。我最初有一个包含图像和导航的左侧边栏,以及一个主要内容区域。在移动设备上,侧边栏用于包裹在主要内容之下。
问题是我需要图像在移动设备上保持在顶部,所以我一直在尝试在一个包装器中使用三个同级 div
s div
.
这甚至可以用 flexbox 还是我需要使用 css 网格?
尽管 CSS 网格是实现您想要的布局的最佳方法,但使用 CSS Flexbox 也是可能的。
您只需创建一个包装器 div
,其中包含三个 div
(当执行 移动优先 方法时)和 .content
设置为 flex: 1
以拉伸视口的高度。
然后对于桌面(在本例中为 @media screen and (min-width: 1000px)
),更改 .navigation
和 .content
的顺序 (MDN reference of order
) 并给出所有三个 div
根据自己的需要选择合适的宽度。 div.wrapper
的唯一变化是它需要 flex-flow: column wrap
才能正确换行。
.wrapper {
display: flex;
flex-direction: column;
height: 100%;
min-height: 100%;
}
.box {
display: flex;
}
.content {
flex: 1;
}
@media screen and (min-width: 1000px) {
.wrapper {
flex-flow: column wrap;
}
.navigation {
order: 2;
}
.content {
order: 3;
}
.image,
.navigation {
width: 200px;
flex: 50%;
}
.content {
width: calc(100% - 200px);
flex: 0 0 100%;
}
}
/* Generic styling */
html,
body {
height: 100%;
}
body {
margin: 0;
padding: 0;
}
.image {
background: orange;
height: 60px;
}
.content {
background: lightblue;
}
.navigation {
background: lightgreen;
height: 60px;
}
<div class="wrapper">
<div class="box image">Image</div>
<div class="box content">Content</div>
<div class="box navigation">Navigation</div>
</div>