What is the best way to align the footer in my React Router application?

I am struggling to ensure that my Footer component always appears at the bottom of each page on my app. Despite trying different approaches, I still can't identify the root cause of the issue.

Below is a snippet of my App.js file:

import "./App.css";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import Navbar from "./components/layout/Navbar";
import Landing from "./components/layout/Landing";
import Contact from "./components/layout/Contact";
import Footer from "./components/layout/Footer";

function App() {
  return (
    <Router>
      <Navbar />
      <Routes>
        <Route path="/" element={<Landing />} />
        <Route exact path="/contact" element={<Contact />} />
      </Routes>
      <Footer />
    </Router>
  );
}

export default App;

The current issue is that the navbar is displayed at the top of the page while the Landing component seems to be positioned behind it. Any suggestions on how to resolve this would be highly appreciated.

Within the Landing Component, there are two sub-components:

import React from "react";
import LandingInner from "../pages/landing/LandingInner";
import LandingCards from "../pages/landing/LandingCards";

const Landing = () => {
  return (
    <section className="landing">
      <div className=" dark-overlay">
        <LandingInner />
        <LandingCards />
      </div>
    </section>
  );
};

export default Landing;

Currently, the Footer is immediately visible at the bottom of the screen upon loading the page, but I intend for it to be fixed at the bottom and only appear when scrolling down. It should consistently remain the last component on every page navigation, similar to how the Navbar persists as the first component.

Here is the existing CSS styling for the Footer:

.footer {
  border: 5px solid black;
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
}

To summarize, I aim for the footer to consistently display at the bottom of every page within my app, rather than on the screen itself upon loading.

Answer №1

To ensure the footer always stays at the bottom of the page, regardless of scrolling or content length, simply set its position to fixed, align it to the bottom with left 0 and give it a width of 100%.

If you want the footer to stay at the bottom when content is minimal and only move to the end of the content when there's enough to scroll, add a height of 100% to both body and HTML elements along with some additional adjustments that I can provide in a sandbox environment.

Let me know if this resolves your issue or feel free to ask for more details if needed.

You can also try implementing flexbox using the code snippet and sandbox provided below:

https://codesandbox.io/s/empty-cloud-b6h4c?file=/index.html

body,
html {
  height: 100%;
}

.container {
  display: flex;
  flex-flow: column nowrap;
  height: 100%;
}

main {
  flex: 1;
}
<section class="container">
  <main>
    main content
  </main>
  <footer>Some content </footer>
</section>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Three.js - Object marked as either chosen or unselected

Within my three.js environment, I have the ability to add and modify objects. Recently, I implemented a DAT.GUI folder that allows me to adjust the color of these objects. However, in cases where no object is SELECTED, I use jQuery to hide it: function on ...

Error message: `Socket.io-client - Invalid TypeError: Expected a function for socket_io_client_1.default`

I have successfully installed socket.io-client in my Angular 5.2 application, but after trying to connect (which has worked flawlessly in the past), I am encountering a strange error. TypeError: socket_io_client_1.default is not a function at new Auth ...

What procedures should I follow to transition from my pygame client to a browser-based game?

After developing a game server in Python to connect with multiple clients using Pygame via sockets, I realized that the turn-based card game I created doesn't require a lot of data flow since it's in JSON format. To avoid the need for clients to ...

Reset the child JSP page to its original appearance

Displayed below is a JSP page: <div id="tabs-7" style="width: 100%;"> <form:form id="deviceForm" name="" modelAttribute="" enctype="multipart/form-data"> <div class="inputWidgetContainer"> <div class="inputWidget"> <table> ...

How can I identify an event occurring on two sibling elements that are overlapping?

I have 2 overlapping siblings as shown below: document.querySelector("#item1").addEventListener('mouseup',()=>{ console.log("Mouseup item 1!"); }); document.querySelector("#item2").addEventListener('mouseup',()=>{ console. ...

There was an issue when trying to input the caph-input in CAPH for AngularJS

I am currently using CAPH-angular to create an app for Tizen Smart TV. Following a tutorial from here, I added the input tag as instructed but encountered an error. <caph-input class="input-user-login" input-type="text" on-focused=& ...

Client event triggered twice by server

Hey there! I'm currently working on creating a simple chat app using socket.io and express. One issue I'm facing is that when a user sends a message, the server broadcasts it to the rest of the clients, but it seems to be happening twice. I can& ...

Trouble with Showing Bootstrap 5 Icon

My current project involves creating a website using Bootstrap 5 icons, but I'm encountering an issue where the icon is not displaying correctly. The expected appearance should be like this [![Example1][1]][1], but it actually looks like this [![Examp ...

Excessive gap located above the section element

On the web page I'm practicing with, I've noticed an unwanted space at the top of the "Favorite Food" section. To center my unordered list, I made the "section" elements inline-block, but this created the issue. How can I remove that space at the ...

Firefox yielding unexpected results with document.scripts

I've been encountering a puzzling issue trying to debug a script that won't work in Firefox, even though it runs smoothly in other programs. In the code snippet from chirp.js, there's a line that reads: script = document.scripts[document.sc ...

Creating a Bootstrap 4 layout where columns have 100% height and internal scroll capability

Is it possible to achieve this specific layout using only Bootstrap 4? +-------------------------------------------------+ | Navbar fixed on top | +------------+------------------------------------+ | | ...

"Enormous NextJS logo greets users upon the initial page load following the project

I'm having some issues with the icons and styles in my NextJs project. I am using material-ui, and during development everything looks fine, but once I build the project, the icons initially appear large before resizing to normal. I have tried using d ...

Align the div in the center horizontally

Looking at this page, my goal is to center the main #container div horizontally in relation to the page. In a typical scenario, I would simply add a CSS rule like this, #container { margin: 0 auto } However, the layout of this specific page (which I didn ...

Obtaining a dependency directly from the Injector within a directive

I am currently working on developing a versatile directive that can take a class type for validating rules. Depending on the rule specified in the class, the directive will either display or hide an element. This is my progress so far. Check out the PLUN ...

When copying text from React-PDF Display, the values may appear altered or varied

This snippet of text was excerpted from a brief SHA provided at the git summit. Generated using React-pdf, this is a PDF document with some interesting quirks. Although the displayed text reads as 4903677, it changes to •G07THH when copied. The font ...

Is there a method to globally import "typings" in Visual Code without having to make changes to every JS file?

Is there a method to streamline the process of inputting reference paths for typings in Visual Studio Code without requiring manual typing? Perhaps by utilizing a configuration file that directs to all typings within the project, eliminating the need to ...

Whenever I click on the menu icon, the material ui menu component adds an unwanted margin to the right side of my page

This is my customized menu component. <Menu id="My-Custom-Menu" aria-labelledby="My-Custom-Menu" anchorEl={anchorEl} open={open} onClose={handleClose ...

Retrieving values using the jQuery .each() function and passing them to an AJAX request

Is it possible to set jQuery AJAX outside of .each in the script provided below? $('#btnUpdate').click(function() { $('#result').html(''); $('.moduleIDInput').each(function() { var uid = $(this). ...

Is it feasible to organize quantities even when they are formatted with commas using React.js and material-ui?

Currently, I am using a material-table that includes a column for the Total Amount. The sorting functionality in this particular column was functioning properly until I implemented a comma masking feature for amounts, such as 1,000.00. Is there a way to ...

Is the stepper's vertical line separating when the label extends onto multiple lines?

I'm facing an issue with the text inside my MaterialUI Stepper // StepLabel, where it sometimes wraps over multiple lines. Is there a way to keep the vertical StepConnectors attached to the StepIcons regardless of the number of lines of text in the l ...