Creating a Vertical Stack of Three Divs in ReactJS

Just Getting Started:

  • As a newcomer to ReactJS and web development in general, I've put effort into researching my question without success.
  • I acknowledge that the solution might be simpler than I think, so I apologize in advance if it turns out to be one of those face-palm moments.
  • Feedback on best practices or improvements to my question are welcome!
  • Appreciate anyone taking the time to read this! Thank you!

Research Efforts:

  1. Float 3 Divs - The z-axis property wasn't relevant to my layout as none of my divs overlap.
  2. 3 Divs LTR - This discussed aligning 3 divs horizontally, not vertically like I needed. Similar methods didn't work for vertical alignment.
  3. 3 Divs LTR #2 - Tried the flex method mentioned here but it was still not sufficient for my case.
  4. Vertical Align etc - Attempted this approach with no success either.
  5. (5...1000) Explored various Google search results using queries like "ReactJS vertical 3 divs" but couldn't find the right solution.

The Challenge:

I'm attempting to create a simple mockup web page layout consisting of 3 div elements:

  1. Header Div - Positioned at the top, non-sticky (doesn't stay visible when scrolling).
  2. Content Div - Positioned in the middle, scrollable both vertically and horizontally.
  3. Bottom Nav Div - Located at the bottom, sticky (remains fixed position during scrolling).

Mockup Design:

https://i.sstatic.net/ooI8t.png

Current Issues:

  • Struggling to make the bottom menu div appear properly; it seems to get stuck under the frame.
  • Uncertain if the bottom menu div is actually sticking to the bottom due to the previous issue.
  • The content tab div has no margin from the header div, causing readability issues at the top end of the text inside it.

https://i.sstatic.net/gdA6M.png

My Code Attempt:

After numerous attempts, this code snippet represents the closest version I have achieved for this seemingly simple task:

App.jsx

import React from "react";
import BottomMenu from "../BottomMenu/BottomMenu";
import Header from "../Header/Header";
import ContentTab from "../ContentTab/ContentTab";

const App = () => {
  return (
    <div style = {{display: "flex", flexDirection: "column", overflow: "visible",
    direction: "rtl"}}>
      <Header/>
      <ContentTab />
      <BottomMenu />
    </div>
  );
};

export default App;

Header.jsx

import React from "react";
import { Toolbar, AppBar } from "@material-ui/core";
import Typography from '@material-ui/core/Typography';

const Header = props => {

  return (
        <div>
          <AppBar color="primary" style={{alignItems: 'center'}}>
            <Toolbar>
              <Typography>
                Test
              </Typography>
            </Toolbar>
          </AppBar>
        </div>
  );
};

export default Header;

ContentTab.jsx

import React from "react";
import Typography from "@material-ui/core/Typography";
import Paper from "@material-ui/core/Paper";

const ContentTab = (props) => {
    return (
        <div style={{height: "80%", width: "100%"}}>
            <Paper align="center" elevation={3}>
                <Typography paragraph>First</Typography>
                <Typography paragraph>TextTab</Typography>
                <Typography paragraph>Last</Typography>
            </Paper>
        </div>
    );
};

export default ContentTab;

BottomMenu.jsx

import React from "react";
import BottomNavigation from "@material-ui/core/BottomNavigation";
import BottomNavigationAction from "@material-ui/core/BottomNavigationAction";
import RestoreIcon from "@material-ui/icons/Restore";
import FavoriteIcon from "@material-ui/icons/Favorite";
import LocationOnIcon from "@material-ui/icons/LocationOn";
import { Toolbar, AppBar } from "@material-ui/core";

export default function BottomMenu() {
    const [value, setValue] = React.useState(0);

    return (
        <div style={{
            position: "fixed", bottom: "0", width: "100%", height: "10%"}}>
            <AppBar
                style={{ background: '#FFFFFF', alignItems: "center" }}
            >
                <Toolbar>
                    <BottomNavigation
                        value={value}
                        onChange={(event, newValue) => {
                            setValue(newValue);
                        }}
                        showLabels
                    >
                        <BottomNavigationAction label="Recents" icon={<RestoreIcon />} />
                        <BottomNavigationAction label="Favorites" icon={<FavoriteIcon />} />
                        <BottomNavigationAction label="Nearby" icon={<LocationOnIcon />} />
                    </BottomNavigation>
                </Toolbar>
            </AppBar>
        </div>
    );
}

Answer №1

It seems like the issue you're facing is related to utilizing the Material-UI AppBar component. If this element was a standard DIV tag, adjusting its position would be straightforward. However, to customize the behavior of the AppBar component, consider the following steps:

  • Remove the outer DIV from the BottomMenu component
  • Style the BottomMenu component's appBar with a top value of "auto", bottom value of "0", and a fixed position property
  • Additionally, ensure that the Header component's appBar has a static position style

For the BottomMenu component:

<AppBar
      position="fixed"
      style={{
        top: "auto",
        bottom: "0",
        background: "#FFFFFF",
        alignItems: "center"
      }}
    >

For the Header component:

<AppBar
    position="static"
    color="primary"
    style={{ alignItems: "center" }}
  >

Here is a link to the documentation demonstrating the desired outcome: https://material-ui.com/components/app-bar/

Additionally, here is a link to a code sandbox containing your code: https://codesandbox.io/s/material-ui-with-bottom-appbar-ugk31

When working with Material-UI components, leveraging their built-in positioning properties instead of relying solely on CSS can often lead to smoother implementation.

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

Exploring the World of Html

I'm struggling with an HTML problem related to a web programming class I'm taking. The assignment involves creating a video game using HTML and JavaScript, where an image moves randomly on the screen and the player must click on it as many times ...

I'm using SASS in my project, can you provide guidance on customizing the UI with @aws-amplify/ui

I've recently taken over a Next.js (React) project from another developer and they've used .scss files (SASS) for styling. The specific project I'm working on can be found here https://github.com/codebushi/nextjs-starter-dimension My task n ...

Preserve the checkbox state upon refreshing the page

I am facing an issue with keeping the checkbox state saved after a page reload. Currently, I have stored my unchecked checkboxes in localStorage, but I am unsure about what steps to take next. In simple terms, I want the checkbox to remain unchecked when I ...

Having trouble displaying the Font Awesome Brand icons on my webpage

Struggling to display the Font Awesome GitHub icon on the client side despite using next.js, tailwind, and react.js for my first portfolio. Below are the dependencies listed in my package.json: "dependencies": { "@fortawesome/fontawesom ...

Cannot retrace steps in file tree using ../

My website has a file structure set up like this: css style.css other files... templates index.html other files... I am attempting to reference the style.css file from my index.html document. My initial attempt was to navigate back a directory u ...

In the desktop view, padding, margins, and shadows mysteriously vanish, yet magically reappear when inspected. This bug stubbornly remains

Currently, I am working on a prototype portfolio site for a school project. My strength lies in the front end development area. Interestingly, when I check the site normally, there seems to be no padding or margin visible. However, upon using inspect eleme ...

The minified version of Bootstrap's CSS will only be loaded when I explicitly import it in my index

I used to rely on BootstrapCDN for my styles, but now I want to download the files and use them locally. However, when I try to load the styles without an internet connection, they don't seem to work properly, especially the grid layout. My current s ...

The local Passport and Express backend is functioning properly when tested with Postman, but encountering issues when integrated with the React and React Query

While utilizing Postman, the backend functions correctly. However, when using React, the backend sends an empty object. I intend for the flow to be: Passport login endpoint redirects to the getUser endpoint in the backend, which then returns user informat ...

Converting a ReactJS input element from controlled to uncontrolled while maintaining a specified value state

The section of my code that is causing issues appears as follows: const normalizeInput = (value, previousValue) => { if (!value) return value; const currentValue = value.replace(/[^\d]/g, ''); const cvLength = currentValue.le ...

Guide to pulling and showcasing information from XML at 30-second intervals using JQUERY AJAX and HTML

Seeking assistance as a beginner. Looking to retrieve and showcase data (HTML / PHP page) from XML every 30 seconds. XML FILE : <MAINData> <LiveData> <Field no="1">ENG ODI</Field> <Field no="2">ENG</Field> ...

The erratic nature of Tailwind actions

Currently, I am immersed in a Livewire project and attempting to implement some Tailwind theme configurations. However, the process does not appear to be coherent whatsoever. Here is an excerpt of my Tailwind configuration: theme: { extend: { f ...

Creating a hover state for a CSS class seems straightforward, but I'm finding it a bit tricky

I am looking to customize my inline menu by changing the last menu item to a different colored box with unique text. I have successfully applied a custom style using the #navbar a.blogbox class, but I am struggling to figure out how to change the hover s ...

What is the best way to generate a search link after a user has chosen their search criteria on a webpage?

In my search.html file, I have set up a form where users can input their search criteria and click the search button to find information within a database of 1000 records. The HTML part is complete, but I am unsure how to create the action link for the for ...

Identifying sluggish hardware or unresponsive browsers using JavaScript

My site features numerous CSS animations and transitions that run very slowly on specific browsers and older hardware. I want to avoid user-agent sniffing; is there a way to identify browsers or hardware configurations using JavaScript or a JS library, and ...

Encountering CORS Issue When Accessing Xero Node/Python SDK APIs within a React Application

While trying to fetch data from the Xero node SDK APIs in my React application, I am facing a CORS (Cross-Origin Resource Sharing) error. Even after setting up the necessary headers and permissions on both the client and server ends, the CORS policy remain ...

How can the default scrolling feature be disabled for all NextJS <Link>s?

Currently, I am in the process of creating a website using NextJS and incorporating a fade-in and fade-out page transition. In order to maintain a smooth user experience, I have implemented a manual scroll to the top of the page between these transitions t ...

Efficient Text Trimming with Material UI Search Function

Is there a way to trim the text entered in the autocomplete UI to ensure a successful search? For example, If I search for "fath," it should display results for the keyword "fath." Visit this link for more information on autocomplete UI. ...

Shifting HTML table in Javascript by toggling checkboxes

When I click the checkbox, the table elements are not displaying inline. I am simply hiding the class "box". Do I need to write a special format? By default, the elements are displayed inline but when I check the checkbox, they shift. The column 'Stat ...

The Bootstrap 4 navigation bar remains expanded on tablet screens

I recently obtained and modified a basic bootstrap 4 theme. By customization, I mean that I ensured all menu items have the same margin and added my logo. The issue I encountered is that on tablets, the navbar does not collapse as expected. I have attache ...

Incorporating AJAX functionality into anchor interactions while preserving href links for search engine optimization benefits

Is it possible to create a link/button that triggers an AJAX/jQuery function to load new content, while still providing a link to the same content on a separate page? I am particularly concerned about SEO and how search engine crawlers will index my sitem ...