简体   繁体   English

在 React 中,页脚不会停留在页面底部和内容下方

[英]Footer won't stay at bottom of page and below content in React

I have a React application with this basic layout:我有一个具有这种基本布局的 React 应用程序:

function App() {
  return (
    <div className="app">
      <Header />
      <Main />
      <Footer />
    </div>
  );
}

export default App;

My issue is that I have separate stylesheets for each of these components, but I can't get my footer to both be at the bottom of the page, and always stay below content.我的问题是我为每个组件都有单独的样式表,但我不能让我的页脚既位于页面底部,又始终位于内容下方。 That is, if the page is blank, the footer would still be at the bottom, and if there was content that was larger than the viewport height, the footer would still remain under it.也就是说,如果页面为空白,页脚仍将位于底部,如果有大于视口高度的内容,页脚仍将保留在其下方。

I've seen answers to this issue, but only with regular HTML pages, not with different React components with different stylesheets.我已经看到了这个问题的答案,但仅限于常规的 HTML 页面,而不是具有不同样式表的不同 React 组件。 I have an index.css , App.css , and CSS pages for each of my App components.我的每个应用程序组件都有一个index.cssApp.css和 CSS 页面。

Is there a way I should go about it?有没有办法我应该 go 关于它? Which stylesheet should I add the code to have the footer stay at the bottom and below the content.我应该添加哪个样式表以使页脚保持在内容的底部和下方。 I currently have code in my Footer.css , but the code doesn't keep the footer at the bottom of the page and below content.我目前在Footer.css中有代码,但代码不会将页脚保留在页面底部和内容下方。

I think the issue is that usually all the components are on the same HTML page within the body , but React is broken up a little differently.我认为问题在于通常所有组件都位于body内的同一个 HTML 页面上,但 React 的分解方式略有不同。 Any help would be great.任何帮助都会很棒。

You could add the below lines inside index.css for example.例如,您可以在index.css中添加以下行。 You can change auto to a fixed value if you want, like 100px.如果需要,您可以将 auto 更改为固定值,例如 100px。

.app {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 3rem;
}

That would do the job.这样就可以了。 Additionally you can make sure there isn't some padding or margin coming from the outside, I mean the div with root id, body and html , to avoid any unnecessary horizontal scroll.此外,您可以确保没有来自外部的paddingmargin ,我的意思是具有root id、 bodyhtml的 div,以避免任何不必要的水平滚动。

You need to add style to global container:您需要将样式添加到全局容器:

 html,body,#root {
   display: flex;
   flex-direction: column;
   min-height: 100%;
  }

then add style to your footer component:然后将样式添加到您的页脚组件:

.footer {
 padding-top: auto;
}

I won't recommend using我不建议使用

min-height: 100vh;

it will break on mobile, because of how its calculated.由于其计算方式,它将在移动设备上中断。

In index.css add this:index.css添加:

Footer {
width: 300px;
text-align: center;
position: relative;
bottom: 0;
left: 50%;
-webkit-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}

Example: https://codesandbox.io/embed/quirky-sound-bitt9k?fontsize=14&hidenavigation=1&theme=dark示例: https ://codesandbox.io/embed/quirky-sound-bitt9k?fontsize=14&hidenavigation=1&theme=dark

You can change position: fixed;您可以更改position: fixed; if you want the footer section to show at the bottom of the page relative to the viewport, rather than relative to the page content.如果您希望页脚部分相对于视口显示在页面底部,而不是相对于页面内容。

Sources for your reference:供您参考的来源:

https://getridbug.com/html/transform-translate-50-50/ https://www.w3schools.com/css/css_positioning.asp https://getridbug.com/html/transform-translate-50-50/ https://www.w3schools.com/css/css_positioning.asp

I was having an issue using grid and grid-template-rows, but switching to flex did the trick.我在使用 grid 和 grid-template-rows 时遇到了问题,但切换到 flex 就成功了。

My understanding is that using a flex-direction: column;我的理解是使用flex-direction: column; and justify-content: space-between;justify-content: space-between; spaces my three components and forces my <Footer /> underneath <Main /> no matter the viewport height.分隔我的三个组件并强制我的<Footer /><Main /> > 下面,无论视口高度如何。

If anyone has a better understanding of why my answer works better than others, I'd love an explanation.如果有人更好地理解为什么我的答案比其他人更好,我希望得到解释。

index.css file: index.css 文件:

.app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM