Skip to content
the poor coder

how to make sticky footer with react router

Poor Man
Published
Reading time
2 min read
On this page

If you want your website to have a footer that sticks to the bottom of the page, even when there isn't enough content to fill the entire page, you can use CSS to make a sticky footer. Here's how to do it with React Router:

Create a new component for your footer. This can be a simple div element that contains your footer content:

Text

import React from 'react';

function Footer() {
  return (
    <div className="footer">
      <p>This is the footer.</p>
    </div>
  );
}

export default Footer;

Add the following CSS to your stylesheet to make the footer sticky:

Text

.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 60px; /* adjust height to match your footer content */
}

This will position the footer at the bottom of the page and make it stay there even if there isn't enough content to fill the page.

Finally, import your Footer component into your React Router file and place it inside your Router component:

Text

import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Footer from './Footer';

function App() {
  return (
    <Router>
      <div className="App">
        <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/about" component={About} />
          <Route path="/contact" component={Contact} />
        </Switch>
        <Footer />
      </div>
    </Router>
  );
}

export default App;

Make sure to place the Footer component outside the Switch component so that it appears on every page.

Alternative Method: Use Flexbox#

If you prefer to use Flexbox instead of absolute positioning, you can use the following CSS:

Text

.App {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.main {
  flex: 1;
}

.footer {
  flex-shrink: 0;
}

This will create a flex container that takes up the full height of the viewport, with the main content stretching to fill the available space and the footer sticking to the bottom.

Both methods are effective for creating a sticky footer in React Router. Choose the one that works best for your project!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC