React 和 Flex 布局如何使用它们

IT技术 javascript css reactjs flexbox
2021-05-21 21:20:29

我正在尝试使用 React 和 flexbox。通常我可以在 react native 中使用 flexbox 但在 react.js 中无法实现

这是我的 CSS 文件

.wrapper, html, body {
    height:100%;
    margin:0;
    position:absolute;
}
.wrapper {
    display: flex;
    flex-direction: column;
}
.nav {
    background-color: red;
    flex:1
}
.main {
    background-color: blue;
    flex:1
}

这是我的js文件

import React, { Component } from 'react';
import './background.css';
import { Icon } from 'semantic-ui-react'
import Navbar from './navbar'

class Back extends Component {



  render() {
    return (
      <div className="wrapper">
          <div className="nav"></div>
          <div className="main"></div>
       </div>
    );
  }
}

export default Back;

我只有一个白屏,这可能是什么问题?

谢谢

2个回答

由于wrapper是绝对定位的,并且没有内容可以增长,因此它的宽度折叠为 0。

所以只需position: absolute.wrapper, html, body规则中删除

如果您仍想使用position: absolute,则还需要为包装器指定宽度。

.wrapper,
html,
body {
  height: 100%;
  margin: 0;
}

.wrapper {
  display: flex;
  flex-direction: column;
}

.nav {
  background-color: red;
  flex: 1
}

.main {
  background-color: blue;
  flex: 1
}
<div class="wrapper">
  <div class="nav"></div>
  <div class="main"></div>
</div>

您还可以在包装类中使用float:left