What is the best way to align my content alongside my sidebar?

I am facing some challenges with my website layout because I used Bootstrap to integrate a sidebar. The sidebar has caused issues with the positioning of my content, and I'm struggling to align it properly next to the sidebar on the left side. Please take a look at the image for reference. As shown in the image above, the sidebar appears on the right while the content sits below it. Below, you'll find the code snippets for both the sidebar and the base template.

// Not bringing sidebar when active
const menu_toggle = document.querySelector('.menu-toggle');
const sidebar = document.querySelector('.sidebar');

// When the button is clicked the sidebar will be active in the following code
menu_toggle.addEventListener('click', () => {
  menu_toggle.classList.toggle('is-active');
  sidebar.classList.toggle('is-active');
})
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}


/* Sidebar Section */

.app {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  flex: 1 1 0;
  max-width: 300px;
  padding: 20px;
  background: linear-gradient(#ed7014, #fa5b3d);
  /* With Scrollbar */
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 1000;
  display: block;
  overflow-x: hidden;
  overflow-y: auto;
  /* Scrollable contents if viewport is shorter than content. */
  border-right: 1px solid #eee;
}

.sidebar h3 {
  font-family: 'Bungee', cursive;
  color: #fff;
}

.sidebar .menu {
  margin: 0 -1rem;
}

.sidebar .menu .menu-item {
  display: block;
  padding: 1em;
  color: #fff;
  text-decoration: none;
  transition: 0.2s linear;
}

.sidebar .menu .menu-item:hover,
.sidebar .menu .menu-item.is-active {
  color: rgb(255, 238, 151);
  ;
  border-right: 5px solid rgb(255, 238, 151);
}


/* Content Scrollbar */

::-webkit-scrollbar {
  width: 10px;
}

::-webkit-scrollbar-track {
  border-radius: 5px;
}

::-webkit-scrollbar-thumb {
  border-radius: 5px;
  background-color: #ffa500;
}

::-webkit-scrollbar-thumb:hover {
  background-color: #d67229;
}


/* Content Section */

.content {
  flex: 1 1 0;
  padding: 2rem;
  background: linear-gradient(#dd5713, #d67229);
}

.content img {
  width: 100%;
  max-width: 300px;
  display: block;
  margin: auto;
}

...

</ul>

    </div>
</body>
</html>

How can I adjust the layout of my content to sit alongside the sidebar?

Answer №1

To make the sidebar sticky and always stretch to the full height of the screen, you can use the following CSS:

position: sticky;
height: 100vh;

The combination of position: sticky and height: 100vh ensures that the sidebar behaves like it has a fixed position but does not impact the document flow. Additional positioning values such as top: 0 and left: 0 can also be applied if needed.

Here's some JavaScript code that toggles the active state of the sidebar when a menu toggle button is clicked:

// Not bringing sidebar when active
const menu_toggle = document.querySelector('.menu-toggle');
const sidebar = document.querySelector('.sidebar');

// Toggle sidebar visibility on button click
menu_toggle.addEventListener('click', () => {
  menu_toggle.classList.toggle('is-active');
  sidebar.classList.toggle('is-active');
})
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
...
@media (max-width: 768px) {
  /* Sidebar Section */
  .menu-toggle {
    display: block;
  }
  ...
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  ...
  <link rel="stylesheet" href="/CSS/List of Course/HTML Course.css">
</head>

<body>
  <section class="app">
    <div class="menu-toggle">
      <div class="button">
        <span></span>
      </div>
    </div>
    <aside class="sidebar">
      ...
    </aside>
    <main class="content">
      <h1>Introduction To HTML</h1>
      ...
    </main>
  </section>

  <script src="https://kit.fontawesome.com/c9df5e490e.js" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3b1bcbca7a0a7a1b2a393e6fde2fde0">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
  <script src="/JS/List of Course/HTML Course.js"></script>
</body>
</html>

Answer №2

My understanding might be off, but:

You could consider incorporating the following into the content section:

width: 100%;
padding-left: 300px; /* Accounting for menu width */

Answer №3

After reviewing the layout, one suggestion is to organize it using rows and columns:

<div class="row">
    <div class="col-lg-3 col-xs-4">
     //Code for sidebar
    </div>
    <div class="col-lg-9 col-xs-8">
     //Code for content
    </div>
</div>

This approach may improve the structure and ensure responsiveness.

If adjusting the styling is challenging, consider removing the position: fixed in .sidebar, as others have suggested. This could provide a temporary solution to the issue.

Answer №4

.sidebar is set to have a position: fixed;.

To style the main content, you can use

<main class="content" style="left: 272px;position: absolute;">
. However, it's important to consider the media queries that adjust the sidebar width. For example:

@media (max-width: 1024px){
  .sidebar {
     max-width: 200px;
  }
  main.content {
     left: 200px;
  }
}

Additionally,

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: -300px;
    height: 100vh;
    width: 100%;
    max-width: 300px;
    transition: 0.2s linear;
  }

  main.content {
     left: 0;
     height: 100%;
  }
}

I suggest using the width property instead of max-width to ensure the content always aligns correctly.

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

Managing post requests in node.js using busboy and then multer

I'm currently facing an issue with busboy (or multiparty) and multer while trying to parse my request. Initially, the request is received successfully using busboy, where I proceed to create a folder and update my database. However, when I attempt to ...

What could be causing the observable collection to display the correct number of objects, yet have them all appear empty?

I am offering the following service @Injectable() export class LMSVideoResulful { getVideos( enrolmentId : number ) :Observable<Array<Video>> { var x = new Array<Video>(); //https://www.youtube.com/embed/MV0vLcY65 ...

Utilizing Browserify routes and configuring Webstorm

When building my project using gulp and browserify, I made use of path resolution for easier navigation. By following this guide, I configured browserify as shown below: var b = browserify('./app', {paths: ['./node_modules','./src ...

What is the best way to send a custom property through Vue router?

I'm currently working with a route instance: const router = new Router({ routes: [ { path: '/', name: 'Home', component: MainContainer, redirect: '/news/list', children: [ { ...

Guide to incorporating animated material icons in vuetify

in Vuetify makes it easy to utilize mainstream material design icons through the use of the v-icon component. An example of this would be: <v-icon>home</v-icon> I am curious if animated material icons are supported and can be integrated in ...

How can VueJS dynamically incorporate form components within a nested v-for loop?

I've encountered a challenge while working on my project. Here is the form I'm currently using: https://i.sstatic.net/LyynY.png Upon clicking the 'New Deal Section' button, a new section like this one is created: https://i.sstatic.n ...

What causes the failure of $event binding when using RowGroup tables with PrimeNG?

I have successfully implemented RowGroup to store 3 different tables, which is working great. However, I am facing an issue with the onRowSelect function not functioning properly. When I click on any row of the RowGroup tables, nothing happens. On another ...

Angular post request does not update the table

After displaying a table and a form on a page, I encountered an issue where the table does not update with new data when submitting the form. Even after refreshing the page, the new data is not reflected. As a newbie to Angular, I'm unsure of what exa ...

What is the best way to accommodate 4 columns within a 3-column HTML table layout?

I am facing a challenge with my table layout. Normally, it has 3 columns, but I am trying to figure out how to fit 4 cells into one row while maintaining the same width. Any suggestions on how to achieve this? ------------- | 1 | 2 | 3 | ------------- | ...

The functionality of Jquery UI is not compatible with version 1.12

Incorporating jQuery UI into my current project has presented some challenges. Both the jquery-ui.min.css and jquery-ui.min.js files are version 1.12, so I opted for the latest jQuery version, jquery-3.2.1.min.js. Specifically, I decided to test the datep ...

Ways to implement a fixed navigation bar beneath the primary navbar using ReactJS

Utilizing ReactJS, I am endeavoring to create a secondary (smaller) navbar in the same style as Airtable's product page. My primary navbar is situated at the top and transitions from transparent to dark when scrolled. The secondary bar (highlighted in ...

Encountering infinite loop in Node modules triggering a Catch-22 situation while attempting to use M

I have been working on a project that involves using some customized Node.js modules. I have developed a 'helpers' module that helps with loading various helper methods: /helpers/index.js: var mutability = require('./mutability'), ...

What impact does setting a variable equal to itself within a Dom Object have?

Within my code example, I encountered an issue with image sources and hrefs in a HTML String named tinymceToHTML. When downloading this html String, the paths were set incorrectly. The original image sources appeared as "/file/:id" in the String. However, ...

Get rid of any empty space in the image preview icon

Is there a way to eliminate the white space that appears when mixing landscape and portrait images? I want the images to move up and fill the space, even if they don't align perfectly. Additionally, I would like the images to resize based on the scal ...

Retrieve specified elements from Bootstrap SelectPicker

My coffee selection script uses the Bootstrap SelectPicker plug-in to choose my favorite coffees. After submitting the form, I want to save the selected values and send them to addCoffee.php?coffee=${coffee}. How can I achieve this? HTML: < ...

Oops! Looks like there's an issue with the NextJS Custom Carousel bug. The error message reads: ReferenceError: document is not defined and is unable to read properties of undefined (

I have developed a unique carousel component to showcase text testimonials from scratch. The custom carousel utilizes a specialized script, carouselFunction.js, for navigational functionality to cycle through each testimonial. Here is an image of the carou ...

Encountering difficulties when attempting to send an AJAX request, receiving an internal server error 500

I've encountered an issue while sending Ajax calls in a loop. The process seems to work fine for the first two iterations, but then throws an internal server error 500 with the description "The JSON request was too large to be serialized". Here is the ...

Passing props from Vue3 to a component being rendered through RouterView

I'm facing an issue with the following code snippet: <template> <router-view /> </template> <script setup lang="ts"> ... const product: Ref<IProduct|undefined>: ref(); ... </script> The challenge is ...

Can we create a collection of Vue highcharts components in a library format?

I am in search of an answer, with hope that it lies here. I have used the following: "vite": "^2.7.13", "highcharts": "^9.3.3", "highcharts-vue": "^1.4.0" I aim to create a library of Vue compon ...

html table displaying incorrect data while using dynamic data in vue 3

example status 1 Hello there! I'm trying to create a table (check out example status 1 for guidance). Here's the code snippet I am using: <table> <tr> <th>Product</th> <th>Price</th> <th>Av ...