flex如何实现这种布局?

flex如何实现这种布局,前提是不拆分成两列,不使用flex范围以外的css属性,比如margin-top: -xxpx
图片描述

阅读 3.2k
3 个回答

大 flex 里 单独套 小的 flex
先拆左右,1 3 5 在大 flex 的左边,2 4 在大flex的右边
1 3 5 自己弄个 flex
2 4 自己弄个 FLEX

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flex</title>
    <style>
    * {font-size:36px;}
    .flex-row {display:-webkit-flex;display:flex;-webkit-flex-direction:row;flex-direction:row;}
    .flex-row > .flex-main {-webkit-flex:1 1 auto;flex:1 1 auto;}
    .flex-row > .flex-side {-webkit-flex:0 0 auto;flex:0 0 auto;}

    .flex-column {display:-webkit-flex;display:flex;-webkit-flex-direction:column;flex-direction:column;}
    .flex-column > .flex-main {-webkit-flex:1 1 auto;flex:1 1 auto;}
    .flex-column > .flex-side {-webkit-flex:0 0 auto;flex:0 0 auto;}
    </style>
</head>
<body>
<div class="flex-row">
    <div class="flex-main flex-column">
        <div class="flex-main" style="background-color:rgba(255,0,0,0.5)">
            1<br />1<br />1<br />1<br />1
        </div>
        <div class="flex-side flex-row">
            <div class="flex-main" style="background-color:rgba(0,255,0,0.5)">
                3
            </div>
            <div class="flex-main" style="background-color:rgba(0,0,255,0.5)">
                5
            </div>
        </div>
    </div>
    <div class="flex-main flex-column">
        <div class="flex-side" style="background-color:rgba(255,255,0,0.5)">
            2
        </div>
        <div class="flex-main" style="background-color:rgba(255,0,255,0.5)">
            4<br />4<br />4<br />4
        </div>
    </div>
</div>
</body>
</html>

我还以为自己很熟悉flex布局了,直到看到了你的这个问题。

flex 要么分行,要么分列,像这种没行没列的,不拆分成几个小容器应该是没办法的。。。

<div class="wrap flex">
    <div>1</div>
    <div class="flex">
        <div>3</div>
        <div>5<div>
    </div>
    <div>2</div>
    <div>4</div>
</div>


<style>
.flex:{
    display:flex;
    justify-content:space-between;
}
.wrap:{
    flex-direction:column;
    flex-wrap:wrap;
    height:xxxx(最外框限制高度);
}
.wrap div{
    flex:0;
}

框的大小就懒得写了
flex是流式布局,所以可以通过限制最外框高度,达到不人为写成两列而是自动分为两列,不知道符不符合楼主的要求。

仔细一想不加margin的话分了列后中间的间隔还是不行。
图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题