在页面底部反应页面保持页脚

An *_*yen 6 html javascript css reactjs

我尝试在页面底部修复页脚时出现问题,如下图所示:

在此输入图像描述

虽然我谷歌并看到很多建议,但我仍然面临着这个问题.我怀疑这个问题是<div data-reactroot></div>不能将身高100%设为父母.谁能帮助我?

提前致谢!

更新:页脚样式:

borderTop: '1px solid #ddd',
height: '60px',
lineHeight: '60px',
backgroundColor: 'white'
Run Code Online (Sandbox Code Playgroud)

mwo*_*elk 12

您需要告诉您的页脚将自己定位到周围容器的底部:

页脚css:

position:absolute;
left:0;
bottom:0;
right:0;
Run Code Online (Sandbox Code Playgroud)

对于容器(react-root div):

padding-bottom:60px;
Run Code Online (Sandbox Code Playgroud)

作为替代方案(如果您不需要支持IE 8),您可以尝试以下方式div.container:

height: calc(100% - 60px);
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案不适合滚动,滚动时页脚会停留在其初始位置 (4认同)
  • 如果您需要滚动友好的解决方案,请使用position:fixed; 始终将其保留在底部。您可能还需要添加 z 索引以使其保持在其他元素之上。 (3认同)

Ank*_*han 8

我相信每个人都忽略了一个技巧,那就是在 React 中,在 html 和 body 元素之后,还有一个带有#root的 div包含整个内容。请参考下图。

在此输入图像描述

因此,需要将 html、body 和 #root 三个部分的高度设置为 100%。

html, body, #root {
  height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

然后在#root中添加这些属性:

#root {
 display: flex;
 flex-direction: column;
}
Run Code Online (Sandbox Code Playgroud)

您一定想知道为什么 #root 需要弯曲而不是主体。原因是它是最里面的父级,或者我应该说是页脚最接近的父级。

现在,最后对页脚执行以下操作:

footer: { margin-top: auto }
Run Code Online (Sandbox Code Playgroud)

上面一行的作用是将页脚推到其父级的末尾。就如此容易。这里没什么特别的。无需对高度进行任何计算或更改页脚的位置。


Ike*_*alu 7

对于上述解决方案不适用的任何其他人,您可以尝试以下步骤:

  1. 父母 div一个非静态的,position比如relative(记住默认positionstatic
  2. div 的最小高度100vh;这使它能够垂直占用所有可用空间
  3. 然后对于页脚(child),它应该被包裹在一个divif not one 中,给它以下属性;position: absolute; bottom: 0; width: 100%.

更新:在某些情况下,将页脚设置div positionabsolute可能不起作用。在这种情况下,请relative改用。

希望上面的步骤应该可以解决它:-)


小智 6

拥有内容包装并将其最小高度设置为 100vh 非常重要:

min-height: 100vh; (100% of the viewport height)

min-height: 100%; (100% of the parent's element height)
Run Code Online (Sandbox Code Playgroud)

看这里解释得很好并且对我有用: https://medium.com/@zerox/keep-that-damn-footer-at-the-bottom-c7a921cb9551


小智 6

我将更改页脚 css 如下:

  position: fixed;
  left:0;
  bottom:0;
  right:0;
Run Code Online (Sandbox Code Playgroud)

可以有 aposition: absolute但它不适合滚动。