删除不需要的水平滚动(水平滚动来自哪里?)

vis*_*hli 5 html css user-interface frontend reactjs

我注意到我的反应应用程序有一个不需要的水平滚动。我发现如果我注释掉该<About/>组件,滚动就会消失。不确定为什么要添加它。overflow-x: hidden如果我添加它就会消失App.css,但这似乎不是正确的解决方案。

另外,我认为<About/>=蓝色组件出现在红色部分上。我无法理解为什么会发生这种情况。

感谢任何帮助。

这些是文件和屏幕截图

应用程序.js

import React from "react";
import "./App.css";
import "bootstrap/dist/css/bootstrap.min.css";
import Landing from "./Components/Landing";
import About from "./Components/About";
function App() {
  return (
    <div className="App">
      <Landing />
      <About />
    </div>
  );
}
export default App;
Run Code Online (Sandbox Code Playgroud)

应用程序.css

.App {
  height: 100vh;
  width: 100vw;
  max-width: 900px;
  padding: 1rem;
  background-color: lightpink;
}
Run Code Online (Sandbox Code Playgroud)

登陆.js

import React from "react";
import "./Landing.css";

function Landing() {
  return (
    <div className="LandingContainer"></div>
}
export default Landing;
Run Code Online (Sandbox Code Playgroud)

登陆.css

.LandingContainer {
  height: 100%;
  width: 100%;
  background-color: lightgreen;
}
Run Code Online (Sandbox Code Playgroud)

关于.js

import React from "react";
import "./About.css";

function About() {
  return (
    <div className="MainAboutContainer"></div>
}
export default About;
Run Code Online (Sandbox Code Playgroud)

关于.css

.MainAboutContainer {
  background-color: lightblue;
  width: 100%;
  height: 100%;
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)

Hao*_* Wu 7

你还启用了其他CSS吗?

如果没有,请尝试在您的中添加以下内容App.css

*, *::before, *::after {
  box-sizing: inherit;
}

html {
  /* this makes sure the padding and the border is included in the box sizing */
  box-sizing: border-box;
  overflow-y: hidden;
}

html body {
  padding: 0;
  margin: 0;
  overflow-y: inherit;
}
Run Code Online (Sandbox Code Playgroud)

此外,您不应该height: 100%同时添加LandingAbout元素,它会使应用程序的高度加倍。

编辑

试试这个,它将消除水平滚动和奇怪的填充问题:

应用程序.css

*, *::before, *::after {
  box-sizing: inherit;
}

html {
  /* this makes sure the padding and the border is included in the box sizing */
  box-sizing: border-box;
  overflow-y: hidden;
}

html body {
  padding: 0;
  margin: 0;
  overflow-y: inherit;
}

.App {
  height: 100vh;
  width: 100vw;
  max-width: 900px;
  padding: 1rem;
  background-color: lightpink;
}
Run Code Online (Sandbox Code Playgroud)

登陆.css

.LandingContainer {
  width: 100%;
  height: 100%;
  background-color: lightgreen;
}
Run Code Online (Sandbox Code Playgroud)

关于.css

.MainAboutContainer {
  width: 100%;
  height: 100%;
  background-color: lightblue;
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)


vis*_*hli -3

最简单的答案是将overflow-x:none您的App.css.