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

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

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

Can you explain how to utilize a function on a client component in Next.js?

I'm a bit lost on how client components function. I am working on an image uploader project where I need to extract the userId from supabase, utilize the supabase server function, and then upload the image to supabase storage with the "userId/filename ...

Tips for effectively displaying elements on a page

After making changes to my css/html code, all the elements within a div are now displayed in a single line instead of appearing as separate <p>contents</p> tags with each on a new line. An example of this issue can be seen here: Dealing with C ...

"Can you explain the process by which React grabs the props using the code `const props = this.props

I recently came across an interesting article authored by Dan. The example provided in the article caught my attention: class ProfilePage extends React.Component { showMessage = (user) => { alert('Followed ' + user); }; handleClick ...

implementing nested key property specifications in React

Is it recommended to explicitly set the keys for the children of components when using arrays of Components? I understand that when using arrays of Components the key property is assumed to be the index of the array and should be explicitly set. {arr.ma ...

React - Can you explain the purpose of the callback function in the forceUpdate method?

The React documentation mentions that the forceUpdate method includes a parameter called callback. Does this function in any way resemble the second parameter found in setState, which is executed after setting state? Or does it serve a different purpose? ...

Protractor experiencing difficulty in adjusting price slider

As a newcomer to protractor, I am attempting to test a price slider that sorts products based on the provided price range. Unfortunately, I am facing difficulty in dragging the slider (min point) using protractor. Can anyone provide guidance on how to move ...

Having difficulty locating audio files within a Next.js project

I am facing challenges when trying to import my audio files into a redux slice. Currently, I am attempting to retrieve my audio files from the app > public > audio directory. I have made efforts to access my audio files through 2 different director ...

Encountering an issue: The API call for /api/orders was resolved but no response was sent, potentially causing requests to become stuck in Next.js

Struggling with an error while working on an ecommerce website using Next.js. The error message reads: error: API resolved without sending a response for /api/orders, this may result in stalled requests The error also indicates: API handler should not ...

Having trouble bringing in css files

I'm having trouble loading a CSS file when trying to import it into another CSS file. I've tried various ways of defining the path but nothing seems to work. Any insights on what might be causing this issue? https://i.sstatic.net/IwaY1.png ...

Protecting against overflow for text inside a container that changes when hovered over

There is text displayed on top of an image within a div container that functions as a link. <a href="https://link.com" style="color:white"> <div class="thumbnail"> <img src="image.jpg" clas ...

Creating individual components for <select> and <option> elements in ReactJS

I am uncertain about how references function when the select and option tags are used together. When I split them into separate React components, they stop working. How can I resolve this issue? Here is a basic example: <select> {() => ( ...

The Iframe is preventing the mousemove event from taking place

After attaching an event to the body, a transparent iframe mysteriously appeared in the background of the popup. I am attempting to trigger a mousemove event on the body in order for the popup to disappear immediately when the mouse moves over the iframe ...

Styling the topbar with CSS includes adding a vertical separator that neatly wraps the content within the

I recently created a website using HTML and CSS, featuring a topbar. However, I encountered an issue while trying to insert a vertical separator between some of the icons within the topbar. Initially, when I added the separator-div, only the first three ic ...

Prevent unauthorized access by typing the URL directly in NEXT.js

I am in the process of developing an app using Next.js and React. Within my app, I have routes such as /checkout or /checkout/success that I do not want users to be able to access directly by entering the URL into the search bar. Instead, I would like to ...

Guide on achieving 'justify-content: center' with MUI Pagination?

I have been attempting to center the contents within the Pagination component, but so far, my efforts have been unsuccessful. After inspecting the console, I noticed that styling the ul wrapper needs justification, yet there doesn't seem to be any rel ...

The bond between TypeORM and express

I am working on establishing Many-to-One and One-to-Many relationships using TypeORM and MySQL with Express. The database consists of two tables: post and user. Each user can have multiple posts, while each post belongs to only one user. I want to utilize ...

Tips on designing checkboxes with CSS

Can someone help me figure out how to style a checkbox using the code below? <input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" /> I've tried applying this style but it doesn't seem to work. The chec ...

The evolving impact of the HTML5 <video> element

I've been attempting to find information on this topic, but unfortunately my search has not been very successful. I vaguely recall coming across a tutorial that covered something like this. Does anyone happen to know how to alter the perspective of a ...

Exploring ways to efficiently narrow down results by ID in a graphql query

I'm struggling to understand where I should include a filter parameter in the graphql query below. Despite reading through the documentation, I haven't been able to determine how to incorporate it. It's important for me to maintain the pagin ...

Switching a conditional className to a styled component

Currently, I am in the process of converting my project from plain CSS to styled components using styled-components. So far, I have successfully converted all of my components except for one. I have looked at various examples on Stack Overflow but none of ...