Creating a bottom bar using React and Material UI

I'm currently working on implementing a footer that will be displayed on every page of my Next.js application. To style this, I am utilizing Material UI.

The following component is responsible for defining the layout across all pages of the app:

import React from "react";
import NavBar from "./NavBar";
import Header from "./Header";
import styles from "../styles/Layout.module.css";
import { Container } from "@mui/material";
import Footer from "./Footer";

export default function Layout(props) {
  return (
    <>
      <NavBar></NavBar>
      <Container className={styles.container}>
        <main className={styles.main}>
          <Header />
          {props.children}
          <Footer />
        </main>
      </Container>
    </>
  );
}

The Container element centers its children on the page. The footer has a distinct color compared to the background of other pages, so I aim to have the footer's background cover the entire viewport while keeping its content centered with the rest of the page.

Initially, I attempted achieving this by using translations:

import React from "react";

const footerHeight = 300;
const footerEltMarginTop = 15;

const div1Style = {
  width: "100vw",
  height: `${footerHeight + footerEltMarginTop}px`,
  backgroundColor: "blue",
};

const div2Style = {
  transform: `translate(0px, -${footerHeight}px)`,
  color: "white",
  width: "100%",
  height: `${footerHeight}px`,
  marginTop: `${footerEltMarginTop}px`,
};

export default function Footer() {
  return (
    <>
      <div style={div1Style}></div>
      <div style={div2Style}>
        <div>footer content</div>
      </div>
   </>
  );
}

The issue here was that there was residual whitespace below the footer equal to the height defined for the footer. To address this, I modified the positioning of the footer background and content to use absolute positioning:

import React from "react";

const footerHeight = 300;
const footerEltMarginTop = 15;

const div1Style = {
  width: "100vw",
  height: `${footerHeight + footerEltMarginTop}px`,
  backgroundColor: "blue",
  marginTop: "30px",
  position: "absolute",
};

const div2Style = {
  width: "100%",
  position: "absolute",
  color: "white",
  height: `${footerHeight}px`,
  marginTop: `${footerEltMarginTop}px`,
};


export default function Footer() {
  return (
    <div style={{width: "100%"}}>
      <div style={div1Style}></div>
      <div style={div2Style}>
        <div>footer content</div>
      </div>
    </div>
  );
}

However, even with these adjustments, the footer background still had unwanted space to the left and did not extend fully across the viewport.

If anyone has suggestions or solutions for accomplishing this using React and Material UI, your input would be greatly appreciated. Thank you!

Answer №1

Depending on the styles of Container and main, there are various approaches that can be taken.

To ensure that the Footer spans the width of the viewport, you could try adding disableGutters and setting maxWidth={false} on the Container so that it extends to the full width without default spacing on the sides.

Another option is to include CssBaseline in the main Layout to apply MUI's recommended CSS reset, although this step is not mandatory for every use case.

For a demonstration of a simple example, refer to this live demo: stackblitz

import CssBaseline from '@mui/material/CssBaseline';

function Layout(props) {
  return (
    <>
      <CssBaseline />
      <NavBar></NavBar>
      <Container maxWidth={false} disableGutters>
        <main>
          <Header />
          {props.children}
          <Footer />
        </main>
      </Container>
    </>
  );
}

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

`Troubleshooting: Inability to chain selectors for custom styling in Next JS module`

When I chain selectors in a CSS file, the styles don't seem to apply as expected. I'm importing the styles into the component and it only applies for the main class. For example: import styles from './Home.module.css' const Home = () = ...

How come TinyMCE is showing HTML code instead of formatted text?

I have been working on integrating TinyMCE with React on the frontend and django(DRF) on the backend. After saving data from TinyMCE, it retains the HTML tags when displayed back, like this: <p>test</p> <div>Test inside div</div> ...

CSS styles are combined and included at the beginning of the index.html file

During the creation of a production version of my Angular 9.0.4 application, I noticed that the CSS is bundled and placed at the top of the dist/index.html file as shown below: <link rel="stylesheet" href="styles.6ea28d52542acb20a4c6.css"><!docty ...

Issue with bottom margin on the last flex item located at the end of the container is not being applied

My button is currently touching the end of the container, but I want to create some space between them. However, adding a margin bottom to the button does not seem to be working as expected. Below is the CSS and HTML code: .events{ height: 100%; d ...

What is the best way to place text side by side with an image?

How can I align text below a picture like this? https://i.stack.imgur.com/WcNRa.jpg Here is the code snippet: https://jsfiddle.net/vzjj9eLy/ HTML: <span class="test"> <img src="http://cdn.sstatic.net/Sites/stackoverflow/img/<a href="/cd ...

"Incompatibility between Tailwind and NextJS causing avatars to disappear when certain props are applied to the render

Encountering issues with the avatar size - changing it to 28, 18, or 16 works fine. However, when adjusting the size to 24, 22, 20, and other values, the image disappears. Below are my screenshots and code snippets: Img Component: import Image from " ...

Styling with CSS to format a table so that each cell occupies one row when viewed on narrower

I've come across some HTML that resembles this structure <html><body><table> <thead><tr><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th></tr></thead ...

Positioning elements in a header using CSS techniques

I am currently working on designing a navigation menu header that includes a logo, along with some buttons aligned to the left side of the logo. However, I am facing an issue where these buttons appear at the bottom of the logo instead of aligning to the t ...

The Enchanted URL Folder Name

Spent two painstaking hours dealing with this issue. It's incredibly frustrating. Struggling to load css files in PHP pages when the URL contains a folder named "adsq" Comparing two identical pages, only differing in the folder name: One works perf ...

Looking for help installing mui 5 in a project using [email protected]? I've already included a peerDependencies script in my package.json, but I'm still getting warnings to add it in

Having encountered issues with peerDependencies, I sought solutions on platforms like stackOverflow and YouTube. One recommendation was to include a peerDependency in the package.json file. Here is an excerpt from my package.json: { "name": ...

Examples of merging responsive design and equal height columns in CSS:

As a beginner in CSS, I am facing challenges in creating a responsive layout that adjusts based on the screen it is viewed on and achieving equal heights in columns. While I have been able to address these issues individually, combining them has proven to ...

What is the most effective method for implementing a fallback image in NextJS?

Lately, I've been immersed in a NextJS project that involves utilizing the YoutubeAPI to retrieve video details, such as thumbnail URLs. When it comes to fetching a full resolution image, the thumbnail URL typically follows this format: https://i.yti ...

SharePoint Framework SPFx: Embracing React Dropzone

Is there anyone who can provide me with a functional code example that utilizes the React Dropzone npm package for SharePoint Framework, specifically in a React environment? Your assistance would be greatly appreciated. Thank you. ...

Can the scroll position be accessed in a material-ui select list while scrolling?

Is there a way to track the scroll position in a material-ui list while scrolling? If it's possible, how can it be achieved? I'm currently working on implementing infinite scroll for a select list as it contains 5300 elements and rendering all o ...

Sneaky spam and ads embedded within Joomla template

Today, while examining the source code of a Joomla site I am developing, I stumbled upon some hidden links that seem to be spam. I have spent an hour searching through various template files but have been unable to locate them. The hidden links in questio ...

The error message "TypeError: undefined is not an object when evaluating 'iter[Symbol.iterator]'" appeared in the context of a React Native application

I have successfully integrated react-redux and redux-persist into my react-native project. However, I encountered an error when trying to add an object to the Redux state: [TypeError: undefined is not an object (evaluating 'iter[Symbol.iter ...

React router not rendering component as expected

I am currently utilizing React Router along with the Link tag to manage the routing between pages on my application. However, I am facing an issue where clicking on the home page from the landing page changes the URL, but the DOM does not display any of th ...

Encountered an issue while attempting npm install, with the error message "Error: Undefined variable standalone_static_library in binding.gyp" and node-sass chokidar error

I've been working on updating a Ruby on Rails and React project from 3 years ago. However, I encountered an issue while trying to npm install. $ npm install gyp: Undefined variable standalone_static_library in binding.gyp while trying to load binding ...

Do the values returned by window.screen.width and height represent CSS pixels or physical screen pixels?

After exploring the API named screen and visiting this documentation link, my initial anticipation was that I would receive information regarding actual screen size pixels. https://developer.mozilla.org/en-US/docs/Web/API/Screen/width https://i.sstatic.n ...

Create a chronological timeline based on data from a JSON object

Here is a JSON object generated by the backend: { "step1": { "approved": true, "approvalTime": "10-11-2021", "title": "title 1", "description": "description 1" ...