【问题标题】:Three columns responsive三列响应式
【发布时间】:2018-12-17 16:51:34
【问题描述】:

我不能使用引导程序来完成这项任务。我使用带有镭的 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>

【问题讨论】:

  • 请给我们看一些示例代码......你试过什么?
  • 你在使用任何框架吗?您使用的是什么尺寸单位?
  • @MihailMinkov 我使用带有镭的 reactjs(样式如 react-native),我尝试使用 flexbox,但我可以使用 CSS 和 MediaQueries
  • @kay 我尝试使用 flexbox 但效果不佳,我想我需要一个媒体查询

标签: html css reactjs flexbox


【解决方案1】:

由于您可以使用 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 列和第 3 列放入列容器中,并且当屏幕尺寸发生变化时,将第 2 列放入第 1 列和第 3 列中。
  • 按列容器,你的意思是&lt;div class="column"&gt;?并改变你的 html?
【解决方案2】:

嗯,我在 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>

非常感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多