【问题标题】:Remove unwanted horizontal scroll (Where is the horizontal scroll coming from?)删除不需要的水平滚动(水平滚动来自哪里?)
【发布时间】:2020-07-10 09:19:03
【问题描述】:

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

另外,我发现 <About/>= BLUE 组件将出现在 RED 部分。我无法理解为什么会这样。

感谢任何帮助。

这些是文件和Screenshot

App.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;

App.css

.App {
  height: 100vh;
  width: 100vw;
  max-width: 900px;
  padding: 1rem;
  background-color: lightpink;
}

Landing.js

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

function Landing() {
  return (
    <div className="LandingContainer"></div>
}
export default Landing;

Landing.css

.LandingContainer {
  height: 100%;
  width: 100%;
  background-color: lightgreen;
}

关于.js

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

function About() {
  return (
    <div className="MainAboutContainer"></div>
}
export default About;

About.css

.MainAboutContainer {
  background-color: lightblue;
  width: 100%;
  height: 100%;
  text-align: center;
}

【问题讨论】:

    标签: html css reactjs user-interface frontend


    【解决方案1】:

    您是否启用了任何其他 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;
    }
    

    此外,您不应将height: 100% 添加到您的LandingAbout 元素中,这会使您的应用的高度加倍。

    编辑

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

    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;
    }
    
    .App {
      height: 100vh;
      width: 100vw;
      max-width: 900px;
      padding: 1rem;
      background-color: lightpink;
    }
    

    登陆.css

    .LandingContainer {
      width: 100%;
      height: 100%;
      background-color: lightgreen;
    }
    

    关于.css

    .MainAboutContainer {
      width: 100%;
      height: 100%;
      background-color: lightblue;
      text-align: center;
    }
    

    【讨论】:

    • 如果我将您建议的 CSS 添加到 App.js,那么它甚至不会显示关于组件。您能否详细说明为什么将 Landing 和 About 都添加 100% 会使高度加倍?它们不是将组件分开吗?
    • 哦,我可能被误解了。如果它们是独立的组件,你不能指望它们相互堆叠,你需要给它们position: absolute 来实现这一点
    • 我根本不希望它们堆叠在一起。我希望他们分开。但似乎 About(蓝色)与 Landing(绿色)重叠
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-01-26
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多