【问题标题】:How to make the footer always at the bottom [duplicate]如何使页脚始终位于底部[重复]
【发布时间】:2020-11-30 16:53:16
【问题描述】:

我正在制作一个网站,并试图让页脚始终位于底部。我希望页脚始终位于浏览器窗口的底部,但如果内容足够长,它会将页脚推到视线之外并进一步推到底部。现在,页脚总是紧跟在它上面的内容之后。

我尝试设置display: flex, flex-direction, align-items: flex-end, margin-bottom: 0px,但没有成功。我也在尽量避免设置固定高度。

相关代码

页脚.js

<footer className='footer'>
    <div className='icons'>
        <span><a href='/'><img src={LinkedIn} alt='LinkedIn'/></a></span>
        <span><a href='/'><img src={Mail} alt='Email' /></a></span>
        <span><a href='/'><img src={Resume} alt='Resume' /></a></span>
    </div>
    <div>
        <span className='footer-link'><a href='/'>Home</a></span>
        <span className='footer-link'><a href='/about'>About</a></span>
        <span className='footer-link'><a href='/blog'>Blog</a></span>
        <span className='footer-link'><a href='/contact'>Contact</a></span>
    </div>
    <div className='footer-copyright'>2020 Daniel Zhang. This site was made by Daniel Zhang from scratch with React.</div>
</footer>
.footer {
    align-items: center;
    border-top: 1px solid grey;
    border-width: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    font-size: 12px;
    margin: 0px auto;
    padding: 10px 0px;
    text-align: center;
}

.icons img {
    display: inline-block;
    padding: 10px;
    width: 25px;
}

.footer-link {
    padding: 0px 5px;
}

.footer-link a {
    color: grey;
    text-decoration: none;
}

.footer-link a:hover {
    color: black;
    text-decoration: none;
}

.footer-copyright {
    padding: 5px 0px;
}

App.js

<NavBar />
<Router>
  <Switch>
    <Route exact path='/' render={() => <Home />} />
    <Route exact path='/about' render={() => <About />} />
    <Route exact path='/blog' render={() => <Blog />} />
    <Route exact path='/contact' render={() => <Contact />} />
    {/* Adds page not found. */}
    <Route render={() => <Home />} />
  </Switch>
</Router>
<Footer />

App.css

* {
    font-family: 'Lato', sans-serif;
}

html, body {
    height: 100%;
    margin: 0px;
}

body {
    display: flex;
    flex-direction: column;
}

.container {
    flex: 1 0 auto;
    margin: 0px auto;
    width: 65%;
}

【问题讨论】:

    标签: html css reactjs sticky-footer


    【解决方案1】:

    试试flex-direction: column;

    document.getElementById('add').addEventListener('click', e => {
      document.getElementById('content').innerHTML += '<br />content<br />content<br />content';
    });
    html,
    body {
      margin: 0;
      height: 100%;
    }
    
    body {
      display: flex;
      flex-direction: column;
    }
    
    .content {
      flex: 1 0 auto;
    }
    
    .footer {
      background: #999;
      flex-shrink: 0;
    }
    <body>
      <div class="content">
        <button id="add">Add Content</button>
        <p id="content">content</p>
      </div>
      <footer class="footer">footer</footer>
    </body>

    【讨论】:

    • 我试过了,它没有做任何改变。我添加了 App.css 供您查看。似乎高度没有使高度达到 100%。
    【解决方案2】:

    你可以使用属性 position: fixed;

    body {
      min-height: 100vh;
    }
    .footer {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
    }
    

    【讨论】:

    • 它与我的内容重叠。
    【解决方案3】:

    你可以利用 bootstrap 并在下面使用这个类。

    <div className="fixed-bottom"> 
    

    【讨论】:

      【解决方案4】:

      请试试这个代码。

      body{
      padding-bottom:50px; //same to the height of footer to avoid overlapping with fixed element
      }
      
      .footer {
          position: fixed;
          bottom: 0;
          left: 0;
          width: 100%;
          height:50px; //set height as per your requirement
          z-index:10;
          overflow:hidden;
      }
      
      

      【讨论】:

      • 这不是我想要完成的我不想修复底部的页脚。
      猜你喜欢
      • 2014-07-05
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 2012-01-26
      • 2020-12-05
      • 2021-10-08
      • 2012-11-16
      相关资源
      最近更新 更多