具有四个 div 的 Flex 容器,需要三列,第二列有两行
Flex container with four divs, need three columns, second column with two rows
对于页面上的一个部分,我试图显示两个矩形 div 堆叠在两个正方形 div 之间,两侧的大小与两个堆叠 div 的高度一样大,这些 div 内部是 img 标签。
我正在使用此标记,因为在移动设备上我希望能够将它们放在带有 overflow: hidden
父级的未包装的弹性行上,这样我就可以使用滑动代码来平移 X 轴。我在使用 flexbox(无网格,需要支持 IE11)创建带有此标记的桌面布局时遇到问题。有人用这个标记做过这个布局吗?谢谢
<div class="flex_container">
<div class='flex_child square'>
<img...>
</div>
<div class='flex-child rect'>
<img...>
</div>
<div class='flex-child rect'>
<img...>
</div>
<div class='flex-child square'>
<img...>
</div>
</div>
对您的内容做出一些主要假设,但您可以使用绝对定位来对齐内容。本质上,您可以将中间的两个元素从文档流中拉出,并通过定位模拟堆叠。
在这种情况下,当高于 767px
时,整行都在文档流中。当低于 767px
时,我们重新定位内部两个元素并将容器设置为 justify-content: space-between
以提供视觉效果 space.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.flex_container {
position: relative;
display: flex;
}
.flex_item {
flex: 1;
height: 120px;
border: 1px solid orange;
padding: 40px;
background-color: red;
text-align: center;
}
@media (max-width: 767px) {
.flex_container {
justify-content: space-between;
}
.flex_item {
padding: 20px;
}
.square {
flex: 0 1 33%;
}
.rect {
position: absolute;
left: calc(100% / 3);
width: calc(100% / 3);
height: 50%;
}
.rect.three {
top: 50%;
}
}
<div class="flex_container">
<div class="one flex_item square">
One
</div>
<div class="two flex_item rect">
Two
</div>
<div class="three flex_item rect">
Three
</div>
<div class="four flex_item square">
Four
</div>
</div>
同样,请注意 - 这假设您可以控制容器的高度和其中内容的尺寸。否则,这种类型的布局很容易分崩离析。如其他评论所述,如果您想要更多的结构和稳定性,最好用另一个 flex-direction: column
的 flex 元素包裹内部元素。只知道风险!
尝试
.flex_container{
display:flex;
flex-direction: row;
border: 1px solid red;
}
.flex_container2{
display:flex;
flex-direction:column;
border: 1px solid green;
}
.flex-child{
border: 1px solid blue;
}
<div class="flex_container">
<div class='flex_child square'>A</div>
<div class="flex_container2">
<div class='flex-child rect'>B</div>
<div class='flex-child rect'>C</div>
</div>
<div class='flex-child square'>D</div>
</div>
我认为您需要第二个弹性容器。
如果添加第二个容器来控制这两个 rect
会变得更容易。
您可以控制 square
的 height and width
使它们成为正方形,而不是动态宽度。
.flex-container {
display: flex;
height: 150px;
}
.container>*, .square {
background-color: purple;
}
.flex-child {
flex:1;
display: inline-flex;
margin: 2px
}
.square { flex-basis: 25%; }
.container {
flex-direction: row;
flex-basis: 50%
}
.container > * {
flex-basis: 50%;
}
.container div:first-child {
margin-right: 4px
}
@media (max-width: 767px) {
.container {
flex-direction: column;
flex-basis: 33.333%
}
.container div:first-child{
margin: 0 0 4px 0
}
}
<div class="flex-container">
<div class='flex-child square'></div>
<div class='flex-child container'>
<div class='rect'></div>
<div class='rect'></div>
</div>
<div class='flex-child square'></div>
</div>
或者:(单击 full page
以查看更改)
.flex-container {
display: flex;
height: 150px;
}
.container>*, .square {
background-color: purple;
}
.flex-child {
flex:1;
display: inline-flex;
margin: 2px
}
.square { flex-basis: 25%; }
.container {
flex-direction: column;
flex-basis: 33.333%
}
.container > * {
flex-basis: 50%;
}
.container div:first-child {
margin: 0 0 4px 0
}
@media (max-width: 767px) {
.square, .container>* {
height: 100px;
}
.flex-container {
height: auto;
flex-direction: column;
}
.rect {
flex-direction: column;
flex-basis: 50%
}
}
<div class="flex-container">
<div class='flex-child square'></div>
<div class='flex-child container'>
<div class='rect'></div>
<div class='rect'></div>
</div>
<div class='flex-child square'></div>
</div>
假设这将是您页面的主要布局,您可以尝试设置固定高度并使用 flexbox 的列方向:
.flex_container {
height: 100vh; /*adjust this value like you want*/
display: flex;
flex-direction: column;
flex-wrap:wrap;
}
.flex_child {
background:purple;
border:2px solid #fff;
box-sizing:border-box;
width:calc(100% / 3);
}
.square {
flex:1 1 100%;
}
.rect {
flex:1 1 47%;
}
body {
margin: 0;
}
<div class="flex_container">
<div class='flex_child square'></div>
<div class='flex_child rect '></div>
<div class='flex_child rect '></div>
<div class='flex_child square'></div>
</div>
如果您想要更好的浏览器支持,您可以使用 float/inline-block,方法是稍微调整 类,使 2 个方块位于开头:
.flex_container {
height: 100vh; /*adjust this value like you want*/
}
.flex_child {
background:purple;
border:2px solid #fff;
box-sizing:border-box;
width:calc(100% / 3);
height:100%;
}
.square:nth-child(1) {
float:left;
}
.square:nth-child(2) {
float:right;
}
.rect {
display:inline-block;
height:50%;
vertical-align:top;
}
body {
margin: 0;
}
<div class="flex_container">
<div class='flex_child square'></div>
<div class='flex_child square'></div>
<div class='flex_child rect '></div>
<div class='flex_child rect '></div>
</div>
如果您不能同时更改 类,请使用一些负边距来纠正最后一个元素的位置:
.flex_container {
height: 100vh; /*adjust this value like you want*/
}
.flex_child {
background:purple;
border:2px solid #fff;
box-sizing:border-box;
width:calc(100% / 3);
height:100%;
}
.square:first-child {
float:left;
}
.square:last-child {
float:right;
margin-top:-50vh;
}
.rect {
display:inline-block;
height:50%;
vertical-align:top;
}
body {
margin: 0;
}
<div class="flex_container">
<div class='flex_child square'></div>
<div class='flex_child rect '></div>
<div class='flex_child rect '></div>
<div class='flex_child square'></div>
</div>
对于页面上的一个部分,我试图显示两个矩形 div 堆叠在两个正方形 div 之间,两侧的大小与两个堆叠 div 的高度一样大,这些 div 内部是 img 标签。
我正在使用此标记,因为在移动设备上我希望能够将它们放在带有 overflow: hidden
父级的未包装的弹性行上,这样我就可以使用滑动代码来平移 X 轴。我在使用 flexbox(无网格,需要支持 IE11)创建带有此标记的桌面布局时遇到问题。有人用这个标记做过这个布局吗?谢谢
<div class="flex_container">
<div class='flex_child square'>
<img...>
</div>
<div class='flex-child rect'>
<img...>
</div>
<div class='flex-child rect'>
<img...>
</div>
<div class='flex-child square'>
<img...>
</div>
</div>
对您的内容做出一些主要假设,但您可以使用绝对定位来对齐内容。本质上,您可以将中间的两个元素从文档流中拉出,并通过定位模拟堆叠。
在这种情况下,当高于 767px
时,整行都在文档流中。当低于 767px
时,我们重新定位内部两个元素并将容器设置为 justify-content: space-between
以提供视觉效果 space.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.flex_container {
position: relative;
display: flex;
}
.flex_item {
flex: 1;
height: 120px;
border: 1px solid orange;
padding: 40px;
background-color: red;
text-align: center;
}
@media (max-width: 767px) {
.flex_container {
justify-content: space-between;
}
.flex_item {
padding: 20px;
}
.square {
flex: 0 1 33%;
}
.rect {
position: absolute;
left: calc(100% / 3);
width: calc(100% / 3);
height: 50%;
}
.rect.three {
top: 50%;
}
}
<div class="flex_container">
<div class="one flex_item square">
One
</div>
<div class="two flex_item rect">
Two
</div>
<div class="three flex_item rect">
Three
</div>
<div class="four flex_item square">
Four
</div>
</div>
同样,请注意 - 这假设您可以控制容器的高度和其中内容的尺寸。否则,这种类型的布局很容易分崩离析。如其他评论所述,如果您想要更多的结构和稳定性,最好用另一个 flex-direction: column
的 flex 元素包裹内部元素。只知道风险!
尝试
.flex_container{
display:flex;
flex-direction: row;
border: 1px solid red;
}
.flex_container2{
display:flex;
flex-direction:column;
border: 1px solid green;
}
.flex-child{
border: 1px solid blue;
}
<div class="flex_container">
<div class='flex_child square'>A</div>
<div class="flex_container2">
<div class='flex-child rect'>B</div>
<div class='flex-child rect'>C</div>
</div>
<div class='flex-child square'>D</div>
</div>
我认为您需要第二个弹性容器。
如果添加第二个容器来控制这两个 rect
会变得更容易。
您可以控制 square
的 height and width
使它们成为正方形,而不是动态宽度。
.flex-container {
display: flex;
height: 150px;
}
.container>*, .square {
background-color: purple;
}
.flex-child {
flex:1;
display: inline-flex;
margin: 2px
}
.square { flex-basis: 25%; }
.container {
flex-direction: row;
flex-basis: 50%
}
.container > * {
flex-basis: 50%;
}
.container div:first-child {
margin-right: 4px
}
@media (max-width: 767px) {
.container {
flex-direction: column;
flex-basis: 33.333%
}
.container div:first-child{
margin: 0 0 4px 0
}
}
<div class="flex-container">
<div class='flex-child square'></div>
<div class='flex-child container'>
<div class='rect'></div>
<div class='rect'></div>
</div>
<div class='flex-child square'></div>
</div>
或者:(单击 full page
以查看更改)
.flex-container {
display: flex;
height: 150px;
}
.container>*, .square {
background-color: purple;
}
.flex-child {
flex:1;
display: inline-flex;
margin: 2px
}
.square { flex-basis: 25%; }
.container {
flex-direction: column;
flex-basis: 33.333%
}
.container > * {
flex-basis: 50%;
}
.container div:first-child {
margin: 0 0 4px 0
}
@media (max-width: 767px) {
.square, .container>* {
height: 100px;
}
.flex-container {
height: auto;
flex-direction: column;
}
.rect {
flex-direction: column;
flex-basis: 50%
}
}
<div class="flex-container">
<div class='flex-child square'></div>
<div class='flex-child container'>
<div class='rect'></div>
<div class='rect'></div>
</div>
<div class='flex-child square'></div>
</div>
假设这将是您页面的主要布局,您可以尝试设置固定高度并使用 flexbox 的列方向:
.flex_container {
height: 100vh; /*adjust this value like you want*/
display: flex;
flex-direction: column;
flex-wrap:wrap;
}
.flex_child {
background:purple;
border:2px solid #fff;
box-sizing:border-box;
width:calc(100% / 3);
}
.square {
flex:1 1 100%;
}
.rect {
flex:1 1 47%;
}
body {
margin: 0;
}
<div class="flex_container">
<div class='flex_child square'></div>
<div class='flex_child rect '></div>
<div class='flex_child rect '></div>
<div class='flex_child square'></div>
</div>
如果您想要更好的浏览器支持,您可以使用 float/inline-block,方法是稍微调整 类,使 2 个方块位于开头:
.flex_container {
height: 100vh; /*adjust this value like you want*/
}
.flex_child {
background:purple;
border:2px solid #fff;
box-sizing:border-box;
width:calc(100% / 3);
height:100%;
}
.square:nth-child(1) {
float:left;
}
.square:nth-child(2) {
float:right;
}
.rect {
display:inline-block;
height:50%;
vertical-align:top;
}
body {
margin: 0;
}
<div class="flex_container">
<div class='flex_child square'></div>
<div class='flex_child square'></div>
<div class='flex_child rect '></div>
<div class='flex_child rect '></div>
</div>
如果您不能同时更改 类,请使用一些负边距来纠正最后一个元素的位置:
.flex_container {
height: 100vh; /*adjust this value like you want*/
}
.flex_child {
background:purple;
border:2px solid #fff;
box-sizing:border-box;
width:calc(100% / 3);
height:100%;
}
.square:first-child {
float:left;
}
.square:last-child {
float:right;
margin-top:-50vh;
}
.rect {
display:inline-block;
height:50%;
vertical-align:top;
}
body {
margin: 0;
}
<div class="flex_container">
<div class='flex_child square'></div>
<div class='flex_child rect '></div>
<div class='flex_child rect '></div>
<div class='flex_child square'></div>
</div>