Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

html - Sticky footer with flexbox

I found out a very simple way of creating a sticky footer for my website, and at first sight it seems to work perfectly.

But since I don't see other people using the same thing, I am wondering if this way of doing it is broken, outside of browsers which don't support flex-box at all?

I use bootstrap for setting flex-box, and I am working within React, here is my code:

<div className="body-div d-flex flex-column justify-content-between">
  <div>  <!-- inner div -->
    <MainNav/>
  </div>
  <MainFooter className="d-flex flex-column"/>
</div>

For people who don't know react: the outer div can be seen as the body element on a 'normal' html page.

css for the body-div:

min-height: 100vh;

So basically I let the inner div and the main footer be pushed to the top and bottom respectively by setting their container to flex-box with the property of justify-content set to space-between.

Also I'd like to add that my site's content, except for the footer that is, will go inside the inner div.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, this is a normal set-up. That's what justify-content: space-between is supposed to do: Pin the first and last elements to the edges of the container.

main {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100vh;
}

article { background-color: lightgreen; }
footer  { background-color: orangered;  }
body    { margin: 0; }
<main>
  <article>inner div</article>
  <footer>footer</footer>
</main>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...