Tips for positioning two divs side by side with a fixed position and full height

I want to set up 2 divs, one as a sidebar on the left and the other for displaying content on the page. Here's what I'm aiming for:

  • Have the sidebar div height at 100%
  • Set the body height to 100% as well
  • Make sure the body's width adjusts when the sidebar width changes

This is the code snippet I have been experimenting with:

#Sidebar{
  background-color:#F0F0F0;
  height: calc(100% - 80px);
  width: 257px;
  position: fixed;
  left: 0;
  bottom: 0;
  overflow:hidden;
  white-space: nowrap;
}

#content {
  margin: 0;
  position: fixed;
  height: 100%;
}

However, the content in the div ends up showing inside the Sidebar instead!

#Sidebar {
    height: calc(100% - 80px);
    width: 257px;
    position:fixed;
    top:0;
    left: 0;
    bottom: 0;
    overflow:hidden;
    white-space:nowrap;
    border:1px solid #000;
}
#content {
    margin: 0;
    position:fixed;
    height: 100%;
    border:1px solid tomato;
}
<div id="Sidebar">
  Hello World!!
</div>
<div id="content">
  Content Div
</div>

Note that I am using jQuery .Resizable to adjust the width dynamically. You can also check out the working example on jsfiddle here.

Answer №1

Is it possible to simply place the sidebar

.sidebar {
    position: fixed;
    height: 100vh;
    left: 0px;
    top: 0px;
    width: 275px;
}

and adjust the body container like this

body {
    padding-left: 275px;
}

This way, there won't be any overlap and you can design your body content freely. An interesting feature of this approach is that when using media queries for mobile responsiveness, you can remove the padding and move the sidebar outside the body, making it slide into view on a button click or other trigger.

Answer №2

To ensure proper alignment, adjust the margin-left of your main content to match the width (plus any desired gap) and make adjustments as needed when resizing the sidebar.

#content {
    margin-left: 257px;

For an updated demonstration, view this fiddle: https://jsfiddle.net/j64r3bm1/2/


The issue arises when using position:fixed, causing elements to 'hover' on the page without taking up space. This results in the content div being placed at left:0 due to the absence of neighboring elements occupying space.

Applying position:fixed to both the sidebar and content can prevent them from occupying the full height of the page, as they would have 0 width and height. Alternatively, consider using float:left instead of fixed positioning.

Answer №3

After some experimenting, I finally cracked the code on how to achieve it. Using JQuery was the key component in implementing this feature. Here's the snippet of code that did the trick:

#Sidebarlist
{
    background-color: var(--SidebarBackgroundColor);
    height: calc(100% - 80px);
    color:var(--SidebarTextColor);
    width: 257px;
    position: fixed;
    left: 0;
    bottom: 0;
    overflow:hidden;
    white-space: nowrap;
}
#MainContentdiv {
    height: 100%;
}

Furthermore, with the help of jQuery:

$('#Sidebarlist').resizable({
       start: function( event, ui ) {},
       stop: function( event, ui ) {},
       handles: 'n,w,s,e',
       minWidth: 200,
       maxWidth: 400
});
$( "#Sidebarlist" ).on( "resizestart", function( event, ui ) {
     var marginleft = $("#Sidebarlist").width();
     $('#MainContentdiv').css('margin-left',marginleft);
});
$( "#Sidebarlist" ).on( "resizestop", function( event, ui ) {
    var marginleft = $("#Sidebarlist").width();
    $('#MainContentdiv').css('margin-left',marginleft);
});

A shoutout to everyone who contributed their knowledge to assist me along the way.

Answer №4

If you want to achieve a specific layout, try adding the following CSS properties:

left: 257px;
top: 0;

To make sure your content is responsive, you can use a container element like this:

#Container {
    height: 100%;
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
    display: flex;
    flex-direction: row;
}

#Sidebar {
    height: calc(100% - 80px);
    width: 257px;
    display: block;
    overflow: hidden;
    white-space: nowrap;
    border:1px solid #000;
}
#content {
    margin: 0;
    height: 100%;
    border:1px solid tomato;
}
<div id="Container">
  <div id="Sidebar">
    Hello World!!
   </div>
  <div id="content">
    Content Div
  </div>
</div>

Answer №5

Here's an unconventional solution that I have tested in both Firefox and Chrome with success. However, due to its innovative nature, I cannot guarantee compatibility with Internet Explorer.

This solution leverages CSS variables, allowing for different calculations based on the variable specified at the beginning (in the :root section).

To enhance clarity, borders have been removed and background colors are used to distinguish between divs.

UPDATE:

I've incorporated a small amount of JavaScript to demonstrate how updating the variable dynamically triggers adjustments across the page.

changeSidebar = function() {
  var customSize = document.querySelector('.sidebar-width').value;
  document.documentElement.style.setProperty('--menu-width', customSize + 'px');
}
:root {
  --menu-width: 257px
}
#sidebar,
#sidebar-footer {
  width: var(--menu-width);
  left: 0;
  overflow: hidden;
  position: fixed
}
body {
  margin: 0
}
#sidebar {
  height: calc(100% - 80px);
  top: 0;
  white-space: nowrap;
  background: orange
}
#sidebar-footer {
  height: 80px;
  top: calc(100% - 80px);
  background: pink
}
#content {
  position: fixed;
  right: 0;
  width: calc(100% - var(--menu-width));
  height: 100%;
  background: tomato
}
<div id="sidebar">
  Hello World!!
</div>
<div id="sidebar-footer">
  I'm just here to fill that space...
</div>
<div id="content">
  Content Div
  <br />
  <input class="sidebar-width" value='257'>
  <button onclick="changeSidebar();">Enter</button>
</div>

Answer №6

Here is the resolution:

let sidebarHeight = document.getElementById('Sidebar').offsetHeight;
document.getElementById('content').style.height = sidebarHeight+'px';
#Sidebar{
background-color:#F0F0F0;
height: 700px;
width: 10%;
float:left;
left: 0;
bottom: 0;
overflow: hidden;

}
#content {
margin: 0;
width:90%;
float:right;
background-color:green;
}
<div id="Sidebar">
Side bar
</div>
<div id="content">
Hello Universe!
</div>

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

display or conceal a div when a radio button is selected

I need a way to display a specific div containing unique text once a radio button is selected, while hiding all other div sections. <?php foreach ($items as $item){ $i=1; echo ' <div class="form-check"> <input class="fo ...

A guide on updating the appearance of a list of comma-separated tags using HTML, CSS, and jQuery

Is there a way to replicate the functionality of the tag section on this website for creating question entries? How can this be achieved? ...

Minimize ring size through mesmerizing smoke effects

I came across this stunning ring with a captivating smoke animation, but I am struggling to comprehend its design fully. My goal is to resize the ring to about 80px and maintain only a single color throughout. However, my attempts to simply reduce the siz ...

What is the functionality of the CSS switch button?

I'm curious about how the cool turn on/off switch button actually works. Take a look at for reference. I'm interested in implementing a prevention check to confirm whether the user truly wants to switch the button. Here's a snippet of the ...

Tips for keeping the footer consistently at the bottom of the page without using a fixed position (to avoid overlapping with the main content)

I've been struggling to keep the footer at the bottom of my webpage. I came across a solution online suggesting to use fixed CSS positioning. However, when I applied this fix, the footer ended up overlapping with the actual content on the page. Is the ...

Looking for a different method to remove a list item within an unordered list using CSS instead of HTML

I am currently working on designing a mock-up website using CSS codes. If you want to see the mock-up website, you can visit: Here is the specific list I am referring to: https://i.sstatic.net/HA3fC.jpg This is the expected outcome: https://i.sstatic.ne ...

Discovering all input elements by utilizing nextAll in jQuery

Here is the HTML structure: <div class="field phone"> <input type="text" maxlength="3" /> </div> <div class="field phone number1"> <input type="text" maxlength="3" /> & ...

Is there a way for me to design a button that includes an image?

I'm looking to create a button on my webpage using an image that will link to other pages. I want the flexibility to use both small and large text on this button. The specific image I want to use is: So far, I've attempted some coding but haven ...

The table value is only updated once with the onclick event, but the updated value is not displayed when the event is clicked again

I am facing an issue with updating names in a table through a modal edit form. The first name update works perfectly, but the second update does not reflect in the table. I want every change made in the modal edit form to be instantly displayed in the tabl ...

Expanding the width of the containing div using CSS

I have a main container with multiple smaller divs inside it. None of these small divs have specified widths. I want the black div to expand and fill up the entire space, stretching all the way to the ABC div on the right. This only needs to work in Chr ...

problem of underlining links in bootstrap

Recently, I integrated a template into my ASP.Net MVC project and added all the required files to it. I made sure to include all the necessary calls to format files (CSS) in the layout file while removing any references to default styling files. However, ...

Unveiling the Invisible: A Guide to Revealing Conce

I have a dilemma when it comes to displaying a hidden form upon clicking a button. The problem is that the form shows up for just a brief moment before the page reloads. Here is the code I am using: Here is my JavaScript: function show(){ document.ge ...

Interactive game created using JavaScript input

I've scoured the internet for guidance on creating a text-based game that utilizes user input, but all I come across are tutorials focusing on button interactions. What I envision is triggering different responses based on specific user inputs. For i ...

Enhance the progression bar using JavaScript

Is there a way to dynamically update a progress bar in HTML while a function is running? function fillProgressBar() { var totalIterations = 100; for (var i = 1; i <= totalIterations; i++) { //perform calculations progressBarWidth = (i/to ...

The React Navbar dropdown items appear to be displaying only text, without any menu showing up behind

I'm having an issue with the background of my dropdown menu not showing properly. The text within the dropdown items is appearing without a background, making it look like they are floating in a line on the page. I've tried adjusting the z-index ...

Having trouble using Jquery Selector to locate a div element in my Polymer project. Seems like it should be simple, but I can

Purpose: My objective is to add a new class to the div that contains a form when a specific icon is clicked. Challenge: The Jquery selector in pullupClose function is not able to identify the element. Check out the console.log result for a visual represent ...

How to create a four-box grid with a specific right margin for every 4th child using CSS

What is the proper way to add a margin of 20px to every fourth list item in a four-box layout using CSS? ...

Delayed suggestions using Typeahead and Bloodhound technology

I have a situation where certain fields require a three-letter shortcode for locations. These shortcodes are not widely known and are mainly familiar to a select group of long-time users. For example: LND - London SWN - Swindon The dropdown menu displa ...

Conceal a <div> element that includes a link using CSS within the @

Having trouble finding the right css selector to hide this div on mobile screens: <div w3-include-left-html="borders/border-left.html"></div> Tried various selectors with no success. For example: <style> @media screen and (max-width: 9 ...

On a medium-sized screen, a collapsible button unexpectedly appears amidst the various links

Can anyone help me with sorting out the order of navbar elements? I'm facing an issue where on small screens, the collapse button appears above the links, but on medium screens, it shows up before the last link in the menu. Any suggestions on how to f ...