Avoid having #content overlap by using a double sidebar layout

My current project involves utilizing Bootstrap to design a webpage featuring two fixed sidebars and a footer. Although I have successfully positioned the sidebars and footer, I am encountering issues with preventing the #content section from overlapping them.

Goal: To dynamically adjust the width and height of the content based on the sidebar width and footer height.

For reference, an example using Bootstrap JS can be found here:

Below is the HTML and CSS code snippet:

.wrapper {
  display: flex;
  width: 100%;
  align-items: stretch;
}

footer {
color: red;
}

.sidebar {
  position: fixed;
  top: 0;
  height: 100vh;
  z-index: 999;
  transition: all 0.3s;
  color: red;
}

#left-sidebar {
  left: 0;
  min-width: 300px;
  max-width: 300px;
}

#right-sidebar {
  right: 0;
  min-width: 420px;
  max-width: 420px;
}


#content {
  width: 100%;
  padding: 20px;
  height: 100vh;
  transition: all 0.3s;
  background: #011627;
}
<body>
<div class="wrapper">

<nav id="left-sidebar" class="sidebar">
  <p>
  Banner
  </p>
  <p>
  Foo
  </p>
</nav>


<div id="content">
<h1 class="title">
Page title
</h1>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua

...

</div>
<nav id="right-sidebar" class="sidebar">
  <p>
  ToC
  </p>
  <p>
  Bar
  </p>
</nav>

<footer class="fixed-bottom">
  Fancy Footer  <br/><br/>
</footer>

</div>

</body>

Answer №1

I have provided the solution below in the code snippet along with a Youtube video link for reference.

Feel free to check out the complete tutorial on Youtube:

https://youtu.be/TWdvIdmJcsU

Your feedback is highly appreciated :-)

body{margin:0}
.wrapper {
  display: flex;
  width: 100%;
  align-items: stretch;
}
footer, .sidebar, #content{position:absolute;}
footer {
color: red;
bottom:0;
background:#000;
width:100%;
height:40px;
}

.sidebar {
  
}

#left-sidebar {
  left: 0;
  width:150px;
  top:0;
  background:#ccc;
  height:calc(100vh - 40px);
  z-index:1;
}

#right-sidebar {
  right: 0;
  width:150px;
  top:0;
  background:#ccc;
  height:calc(100vh - 40px);
}


#content {
  width: calc(100% - 300px);
  height: calc(100vh - 40px);
  transition: all 0.3s;
  background: #011627;
  left:150px;
}
.content-wrapper{padding:15px;}
<body>
<div class="wrapper">

<nav id="left-sidebar" class="sidebar">
  <p>
  Banner
  </p>
  <p>
  Foo
  </p>
</nav>


<div id="content">
<div class="content-wrapper">
<h1 class="title">
Page title
</h1>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua

...

</div>
</div>
<nav id="right-sidebar" class="sidebar">
  <p>
  ToC
  </p>
  <p>
  Bar
  </p>
</nav>

<footer class="fixed-bottom">
  Fancy Footer  <br/><br/>
</footer>

</div>

</body>

Answer №2

Utilizing the CSS property position: fixed will detach the sidebars from their natural position in the layout, effectively removing them from the influence of their parent flex container. Fixed elements remain stationary and do not affect the positioning of surrounding elements.

An alternative approach would be to center the #content div by adjusting its side margins to match the widths of the sidebars.

.sidebar {
  position: fixed;
  top: 0;
  height: 100vh;
  z-index: 999;
  transition: all 0.3s;
  color: red;
}

#left-sidebar {
  left: 0;
  min-width: 300px;
  max-width: 300px;
}

#right-sidebar {
  right: 0;
  min-width: 420px;
  max-width: 420px;
}

#content {
  width: 100%;
  margin: 0 420px 0 300px; // center content between the sidebars
  padding: 20px;
  min-height: 100vh;
  transition: all 0.3s;
  background: #011627;
}

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

Ascending and descending functions compared to Ext.getCmp()

I'm feeling a bit lost trying to figure out which method to use when using grep object - should I go with up(), down(), or Ext.getCmp(ID)? Personally, I find it simpler to assign an ID to the object and then retrieve it using Ext.getCmp('ID&apos ...

My HTML gets rearranged by browsers, causing a change in appearance

UPDATE: I have discovered that the issue lies with Internet Explorer which alters the class attribute of an HTML element from: "<img class="blah".." to "<img class=blah..". This inconsistency only occurs in IE for me. It does not affect other attr ...

I am having trouble importing Material UI icons. Can anyone guide me on how to successfully use Material UI icons without any

While working on my project with React and Material UI, I encountered some difficulties when trying to import the material icons. I followed the code examples from the Material UI documentation, using version "material-ui": "^1.0.0-beta.41" and "material-u ...

Getting files from CDN using NuxtJS content module - a beginner's guide

Is there a way to fetch files from an external server, such as a CDN, using the @nuxtjs/content module? This would allow me to manage the .md files independently without relying on Nuxt.js. In my current nuxt.config.js file, I have the following setup: ex ...

How can I optimize my Javascript applications for better search engine rankings?

Recently, I've been exploring the idea of implementing a fresh workflow process for web development. Yemoan, Grunt, and Bower in conjunction with AngularJS look promising as a solution for front-end development. However, one major drawback is their po ...

Assistance needed in creating a MVC3 and Razor 2-level horizontal navigation menu

I am attempting to create a 2-level horizontal menu, similar to the one featured on the following site: TV.Com Despite searching on google, I am struggling to put it all together. Below is the sample code I am working with: <div id="divNav"> <u ...

Can you tell me the Bootstrap 4 equivalent class for '.well'?

While using bootstrap-3, the .well class can be utilized to create a rounded border around an element with a gray background color and padding. <div class="well">Basic Well</div> However, it seems that there is no .well class available in boo ...

Tips for adding content to a textarea after making manual changes to its existing content

One issue I encountered is with capturing the enter key on an input field and appending it to a textarea. While this works initially, any edits made directly on the textarea seem to disrupt the functionality. How can this be resolved? I have tested this i ...

Arranging a Bootstrap 4 card and tab panel in a side-by-side layout

Hello experts, I am trying to create a user profile page using Bootstrap with card and tab panel side by side but unfortunately, my code is not working. Here is the code snippet, can someone please help me? <div class="col-md-2"> <div class=" ...

Getting a 401 error when using the Companies House API with jQuery and ajax

I attempted to retrieve JSON data from the UK Companies House API but encountered a 401 error An error occurred: the server responded with a status of 401 (Unauthorized) I implemented jQuery with the ajax() method for this task, and here is a snippet o ...

Tips for integrating Material UI Drawer with Redux

I have been attempting to incorporate a Drawer feature that displays on the left side while utilizing Redux to maintain the state of the Drawer. Unfortunately, I seem to have made an error as my onClick events are not responsive. Here is the code: Reduce ...

Press the button to modify the titles of the table

One of the tasks I'm working on involves a table with column titles. In this table, there's an edit button that, when clicked, should convert the title into an input field with the current title as its initial value. The edit button will then cha ...

Elements that are added dynamically will not inherit the correct CSS styles, requiring JavaScript to style them properly

My <ul> element dynamically adds <li> elements, and I am attempting to make the first <li> wider at 63% while the others are at 60%. However, the first one is only getting a width of 60%. Any thoughts on why this might be happening? I ne ...

What is the process for converting and transferring the date in Google Apps Script to generate a new event in Google Calendar?

To create an event in GAS, follow this link: https://developers.google.com/apps-script/reference/calendar/calendar#createEvent(String,Date,Date,Object) var event = CalendarApp.getDefaultCalendar().createEvent('Apollo 11 Landing', new Date(& ...

Using Node.js and the Jade templating engine, display the value of a passed variable

Asking such a basic question makes me feel guilty. app.get('/skumanagement/:id', function (req, res){ var options = req.params.id; // req.params.id = itemidx database.skuGetDetail(options, function (error, data){ winston.log('inf ...

What is the best way to set the height of an SVG element to match the height of the entire

https://i.sstatic.net/HSdlQ.png I am working with an SVG element and I have a query regarding its positioning. <svg style="height: 100%" viewbox="0 0 100 25" preserveAspectRatio="none"> <path fill="blue" d="M0 30 V12 Q30 17 55 12 T10 ...

Issue with Electron dialog.showOpenDialog() Filters malfunctioning

Recently, I've been working on a modified version of an IDE on GitHub and encountered a major issue where the files were being saved to cookies instead of the computer. This prompted me to find a way to save and open files efficiently. While I managed ...

The standard bootstrap navbar does not automatically create spacing for content below it

Hey there everyone, I’m facing an issue with my responsive bootstrap menu. When I click the toggle button to open the dropdown menu, it doesn’t push the other content down like it should. Instead, it just overlaps the content. I’ve been trying to sol ...

Tips for creating a useState variable within the render() function in reactjs

I'm completely new to reactjs, and I've been trying to define useState variables within the render() method of a reactjs component. However, I keep running into an error: "Invalid hook call." class ProductDefinition extends Component { construc ...

Placing the ul tag within a div at the top of the div for proper alignment

I am facing an issue with my code where I cannot get the UL element to appear at the top of the div as it is a child element. <!doctype html> <html> <head> <title>xxx</title> </head> <body> <div id="page" style ...