【问题标题】:How to make Div height: 50%? it does not work on React如何使 Div 高度:50%?它不适用于 React
【发布时间】:2021-11-23 14:57:48
【问题描述】:

好的,所以我正在尝试使用 React javascript 库制作一个 todolist。

问题

我正在尝试将网站屏幕除以 2 个 div。其中,一个在顶部 50%,另一个在底部 50%。然而,

.top{
  width: 50%;
  float: top;
}

.bottom{
  width: 50%;
  float: bottom;
}

好像不行..

我试过了

添加html, body { height: 100%; }

添加.App {text-align: center;margin:0;height:100%;}

它没有任何改变。

示例

import React from 'react'
import './App.css';
import Navigation from './components/ToDoNav';

function App() {
  return (
      <div className="App">
        <Navigation/>
        <div className="top">
            <h1>Copy</h1>
        </div>
        <div className="bottom">
            <h1>Design</h1>
        </div>
      </div>
  );
}

export default App;

App.js

.App {
  text-align: center;
  margin:0;
  height:100%;
}

html, body {
  height: 100%;
}

.top{
  height: 50%;
  float: top;
}

.bottom{
  height: 50%;
  float: bottom;
}

.App-logo {
  height: 40vmin;
  pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
  .App-logo {
    animation: App-logo-spin infinite 20s linear;
  }
}

.App-header {
  background-color: #282c34;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: calc(10px + 2vmin);
  color: white;
}

.App-link {
  color: #61dafb;
}

@keyframes App-logo-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

App.css

结果

【问题讨论】:

  • 试试这个body { margin: 0 auto; }

标签: html css reactjs


【解决方案1】:

您必须将高度 100vh 添加到包装标签 (.App)

.App {
  text-align: center;
  margin:0;
  height:100vh;
}

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 2022-01-23
    • 2010-09-15
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2020-01-02
    相关资源
    最近更新 更多