The React sidebar expanded to cover the entire screen width

As I work on creating a dashboard page that will display a sidebar after the user logs in, I am facing an issue where the sidebar is taking up the full width of the page, causing my Dashboard component to drop to the bottom.

You can view the image link of the sidebar here

Here is the snippet of code from App.js :

import Login from './Component/Login';
import Register from './Component/Register.jsx'
import Home from './Component/Home'
import {
  BrowserRouter as Router,
  Routes,
  Route,
} from "react-router-dom";

function App() {
  return (
    <div className="App">
      <Router>
        <Routes>
          <Route exact path="/" element={<Login/>}/>
          <Route path="/register" element={<Register/>}/>
          <Route  path="/dashboard" element={<Home/>}/>
        </Routes>
      </Router>
    </div>
  );
}

export default App;

And here is my code for Home.js:

import React from "react";
import Sidebar from './Sidebar'
import Dashboard from "./Dashboard";
import {Routes, Route} from 'react-router-dom'
import './Home.css'

function Home() {
  return (
    <>
   <Sidebar/>   
      <Routes>
          <Route  index  element={<Dashboard/>}/>
        </Routes>
      </>
  );
}

export default Home;

The Dashboard Component contains Lorem text that goes up to 80 characters.

Below is the code for Sidebar.js and Sidebar.css

import React from 'react'
import './Sidebar.css'
import {Link} from 'react-router-dom'

function Sidebar() {
  return (
    <div>
      <nav className="sidebar">
        <Link to="/dashboard">DashBoard</Link>
      </nav>
    </div>
  )
}

export default Sidebar

Here's the content of Sidebar.css :

.sidebar{
    display:flex;
    width:200px;
    border: 2px solid black;
    height:100vh;
    background-color:#FFD302;
}

I'm not certain if the issue lies with the CSS or the Router dom. Any assistance would be greatly appreciated. Thank you!

Answer №1

The Home component manages the structure of the HTML/JSX it displays. I recommend implementing a grid layout with two columns, one for the sidebar and the other for the sub-routes.

For example:

.home {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

...

function Home() {
  return (
    <div className="home"> // <-- parent element
      <Sidebar />
      <Routes>
        <Route index element={<Dashboard />} />
      </Routes>
    </div>
  );
}

https://codesandbox.io/s/empty-tdd-w8r5s2?fontsize=14&hidenavigation=1&initialpath=dashboard&module=%2Fsrc%2FApp.js&theme=dark

Answer №2

Consider organizing your CSS in separate files and then importing them for cleaner code structure and easier maintenance.

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

Displaying a Facebook iframe on the left side of the page

I'm struggling with positioning the Facebook iframe embed to sit on the left of the text. I want it to be aligned with the left margin, but also have the text flow beside it from the same height. Can anyone offer some guidance on how to achieve this u ...

Combining React-Redux and Bootstrap: A sophisticated modal feature

Struggling with the distinction between components and containers when using Bootstrap's modal in React-Redux. Instead of repetitively creating modals, I aim to develop a React Component that encompasses a modal template. For clarification, here&apo ...

Unable to eliminate the unwanted space at the top of the division

I am encountering an issue where I have a div containing a label, but there seems to be some unwanted space above the div that I can't seem to remove. It's puzzling me as to why this space is appearing. Query: What could be causing the space ab ...

Seeking assistance in creating custom tabs for integration with a jQuery tabs plugin

Attempting to navigate the world of CSS as a newbie, I find myself struggling with creating tabs. Our current tab setup can be viewed here, but unfortunately, these tabs are created using an unattractive <table> tag. Below is the code responsible fo ...

Prevent the overlap of the float right span with text using CSS

I'm currently using rickshaw and have a graph hover/annotation where a color swatch (defined with a <span>) is overlapping text within the same <div>. Check out my HTML code below: <div class="detail" style="left:250px"> <d ...

Issues with CSS causing image resizing problems on Chrome, looking for solutions

On my website, I have favicons from various external domains. However, there seems to be an issue with resizing them when they are not 16px in size. Sometimes only the top or bottom half of the image is displayed, but upon refreshing the page, the entire i ...

evolving the design of mui stepper

I am looking to customize the code below for my specific needs I want to change the icon from to this: I am unable to modify the style and also need to add an intermediate step I have included , '.Mui-active': { color: '#1C6FC9', }, ...

React: Issue with For Loop not recognizing updates in Hook's State

Recently, I successfully created a React application that displays each word of a sentence at a user-defined time interval for fast reading. However, I am now facing a challenge as I attempt to add a pause button functionality to the app. When I press the ...

What is the process for dynamically populating a select dropdown based on the selection made in another select dropdown?

I need to dynamically populate the second select box based on the option selected in the first select box. Here's what I have tried so far, but it doesn't seem to be working as expected. HTML: <form id="step1"> <p> Creat ...

The definition of "regeneratorRuntime" is missing in the rete.js library

After encountering a problem, I managed to find a potential solution. My current challenge involves trying to implement Rete.js in Next.js while using Typescript. The specific error message that's appearing is: regeneratorRuntime is not defined Be ...

Develop a chart featuring sub-categories and column headings

I'm in the process of creating a table with subcategories and headers that resembles the image provided below: Here's what I've come up with so far: <table> <thead> <tr> <th>Item</th> & ...

changing a React useState into a Redux reducer based on a separate value

Within my code, I have a simple useState hook that retrieves an id number from an object in an array of objects. This id number is used to display the data of that specific object as shown below: const [itemId, setItemId] = useState<number | undefined&g ...

What is the procedure to modify a CSS class from the code-behind file?

So I have a CSS style in my .css class where the color is set to blue for hundreds of buttons. But now, my customer wants an option for green buttons which will be saved by a database field. So I check the field like this: if (!String.Is ...

The date displayed in moment.js remains static even after submitting a new transaction, as it continues to hold onto the previous date until

I am currently utilizing moment.js for date formatting and storing it in the database This is the schema code that I have implemented: const Schema = new mongoose.Schema({ transactionTime: { type: Date, default: moment().toDate(), ...

What is the process for deploying a React project located in a subdirectory of the main branch to Github Pages?

Currently, the project repository is structured like this: main: app/ README.md preview.jpg The app directory contains all the necessary files and folders for my react project. I am facing an issue with getting Github Pages to work properly with the sub- ...

Invalid prop type: The Grid component requires the justify prop to be used on a container

I encountered an issue while working with Material-UI in a React project <Grid container justify="center"> <Box className={classes.box}> <Grid item className={classes.item1}> <Typography variant="h5" c ...

How to add an OnClick listener to a cell element in a table built with the Tan

I am currently working on a project using React and trying to implement a table. I want to show an alert when a header cell in the table is clicked, displaying some information. However, I have been struggling to find assistance on adding a click listener ...

A guide to customizing nested elements in react using styled-components

Currently, I am utilizing styled components to apply styles to a child element inside a div using nested css. For a demonstration, you can view my example here const customStyles = theme => ({ root: { ...theme.typography.button, background ...

Customizing columns in React Material-tableIncorporating unique columns

As a MERN newbie, I'm seeking assistance in creating a Stocks app. I've successfully fetched data using axios and presented it in a table using Material-Table. import React, {useState, useEffect} from 'react' import MaterialTab ...

What steps can I take to ensure a JavaScript loading image is displayed until the entire page has finished loading?

Looking to implement a JavaScript loading image that displays until the page has fully loaded? I'm currently developing an online antivirus scanner and in need of help. I am trying to set up JavaScript to show a loading image while a file is being up ...