三列响应式

时间:2018-07-09 23:03:19

标签: html css reactjs flexbox

我不能使用引导程序来完成此任务。我使用了带有镭的reactjs(样式像react-native),我尝试使用flexbox,但是我可以使用CSS和媒体查询。

我有3列的布局。从桌面访问它时,它显示如下:

--------------------------
|  column 1 |            |
------------|  column 2  |
|  column 3 |            |
--------------------------

第1列和第3列的高度与第2列的高度相同。

我希望从移动/平板电脑/调整大小的浏览器中查看时是这样的:

-------------
|  column 1 |
-------------
|  column 2 |
-------------
|  column 3 |
-------------

第2列需要在第1列和第3列之间移动,在此配置中每列的高度无关紧要。

下面的示例中,我使用镭,但是可以使用CSS和媒体查询。

columnsContainer: {
    display: "flex",
    flexDirection: "row",
    @media (min-width:"xxx"px): {
        flexDirection: "column"
    }
},
rightColumn: {
    width: "392px",
    @media (min-width:"xxx"px): {
        marginTop: "32px",
        width: "100%"
    }
},
leftColumn: {
    flex: "1",
    @media (min-width:"xxx"px): {
        marginRight: "0px",
        marginLeft: "0px"
    }
},
bottomColumn: {
    flex: "1",
    @media (min-width:"xxx"px): {
        marginRight: "0px",
        marginLeft: "0px"
    },
}

在我的html下面:

<div style={styles.columnsContainer}>
    <div style={styles.leftColumn}>
        Column 1
    </div>
    <div style={styles.rightColumn}>
        Column 2
    </div>
    <div style={styles.bottomColumn}>
        Column 3
    </div>
</div>

2 个答案:

答案 0 :(得分:4)

由于您可以使用Flexbox,因此可以尝试以下示例。

使用order根据要求放置列。

.outer {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.one {
  background: red;
  height: 200px;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
}

.two {
  background: blue;
  height: 200px;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
}

.three {
  background: green;
  height: 200px;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
}

@media only screen and (max-width: 600px) {
  .outer {
    max-height: 400px;
  }
  .one {
    width: 50%;
    order: 1;
  }
  .two {
    width: 50%;
    order: 3;
    height: 400px;
  }
  .three {
    width: 50%;
    order: 2;
  }
}
<div class="outer">
  <div class="one">1</div>
  <div class="two">2</div>
  <div class="three">3</div>
</div>

答案 1 :(得分:0)

好吧,我在CSS Grid中找到了答案:

columnsContainer {
    display: "grid",
    gridTemplateColumns: "1fr auto",
    @media (min-width:"xxx"px): {
        gridTemplateColumns: "auto"
    }
},
rightColumn: {
    gridColumn: "2",
    gridRow: "1 / 3",
    @media (min-width:"xxx"px): {
        gridColumn: "1 / 2",
        gridRow: "2",
        width: "auto"
    }
},
leftColumn: {
    @media (min-width:"xxx"px): {
        gridRow: "1",
    }
},
bottomColumn: {
    @media (min-width:"xxx"px):: {
        gridRow: "3",
    }
}

和HTML:

<div style={styles.columnsContainer}>
    <div style={styles.leftColumn}>
        Column 1
    </div>
    <div style={styles.rightColumn}>
        Column 2
    </div>
    <div style={styles.bottomColumn}>
        Column 3
    </div>
</div>

非常感谢您的帮助!