Arrange two divs next to each other on the page: the div on the right can be scrolled through, while the div on the

I am attempting to create a layout similar to this

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

My goal is to:

  • Have two main divs positioned side by side inside a wrapper div
  • Ensure the wrapper expands to fill 100% of the browser size
  • The left div (banner) should be fixed and overlayed, acting as a vertical navigation container. When the list of items exceeds the visible area without scrolling, the div should expand in height accordingly
  • The right div (content) should have overflow:auto so that if its content exceeds the allowed height (browser height), users can scroll down to view the rest while keeping the left div fixed in place

I've spent some time working on a solution, here is what I have come up with so far:

<style>
    html, body {
        height: auto;
    }
    #wrapper {
        overflow: hidden;
        height: 100%;
    }
    div#banner {
        width: 35%;
        float: left;
        padding-bottom: 1000px;
        margin-bottom: -1000px;
        overflow:hidden;
        height:100%;
        background: rgba(0, 0, 0, 0.74);
        z-index: 999;
    }
    div#content {
        width: 65%;
        float: left;
        background-color: grey;
        padding-bottom: 1000px;
        margin-bottom: -1000px;
        overflow:auto;
        background: blue;
        height:100%;
        color:white;
    }
</style>
<div id="wrapper">
    <div id="banner">
      <header>
            header content
        </header>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text...
    </div>
    <div id="content" style="">
        right
        text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<br>text<b...
    </div>
</div>

(update) https://jsfiddle.net/nze5ug5t/5/

Is there a better way to achieve this layout?

Answer №1

It's impossible to achieve this without incorporating JavaScript into the solution. To handle different scenarios based on viewport height, left column height, right column height, and scroll position, specific CSS rules must be applied. While this method may require further fine-tuning, the following code snippet outlines a potential approach:

document.checkBanner = function() {
  var ch = $('.contentWrapper').innerHeight(),
    left = $('.left-banner-content'),
    lh = left.innerHeight(),
    s = $(document).scrollTop(),
    wh = $(window).height();
  if (wh > lh) {
    left.css({
      'top': s + 'px'
    })
  } else {
    if ((s + wh) > lh) {
      left.css({
        'top': (s + wh - lh - 20) + 'px'
      });
    } else {
      left.css({
        'top': 0
      });
    }
  }
  if ($(document).height() > ch) {
    $(document).scrollTop(ch - wh);
  }
}
$(document).scroll(function() {
  document.checkBanner();
});
$(document).resize(function() {
  document.checkBanner();
});
body {
  margin: 0;
}
.wrapper {
  margin: 0;
  position: relative;
}
.left-banner {
  position: absolute;
  min-height: 100%;
  height: 100%;
  width: 210px;
  background-color: rgba(0, 76, 0, .84);
  color: white;
}
.left-banner-content {
  position: relative;
  padding: 0 20px 5px;
}
.contentWrapper {
  padding-left: 210px;
  min-height: 100%;
  background-image: url(http://www.plafondchauffant.fr/modules/pm_advancedbackgroundchanger/uploads/slides/53d177879b0e2.jpg);
  -webkit-background-size: cover;
  background-size: cover;
}
.content {
  min-height: 100vh;
  background: rgba(255, 255, 255, .9);
  padding: 0 20px;
  overflow-y: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="left-banner">
    <div class="left-banner-content">
      <h3>This is left banner</h3>
      <ul>
        <li>some item</li>
        <li>some other item</li>
        <li>yet another item</li>
        <li>and another one</li>
      ... (truncated for brevity)
      </ul>
    </div>
  </div>
  <div class="contentWrapper">
    <div class="content">
      <h1>This is content</h1>
      <p>Ennui pinterest kogi freegan +1, try-hard leggings pickled chillwave chartreuse raw denim 8-bit brooklyn... (truncated for brevity)</p>

      <p>Actually selvage before they sold out, affogato dreamcatcher squid taxidermy chia cornhole deep v ethical meggings quinoa... (truncated for brevity)</p>

      <p>Locavore kickstarter freegan, pinterest authentic celiac portland four dollar toast affogato cray kogi hashtag direct trade... (truncated for brevity)</p>

      <p>Cronut mixtape +1 YOLO helvetica bicycle rights. Offal polaroid authentic mixtape vinyl... (truncated for brevity)</p>
    </div>
  </div>
</div>


UPDATE: While addressing rendering issues with the previously mentioned solution, I discovered an existing implementation that tackles similar challenges. Meet lockfixed, elegantly packaged as a jQuery plugin. It's always advisable to search for existing solutions before reinventing the wheel :).

Cheers!

Answer №2

Depending on the context, the answer to your query may vary significantly. Is this the desired outcome you are aiming for?

Link to Example

The following CSS code demonstrates the use of position: absolute along with specifying values for top, right, bottom, and left. It also includes instructions for handling overflow-x and overflow-y.

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  font-family: arial;
}
.header {
  padding: 1rem;
  font-weight: bolder;
}
.wrapper {
  position: absolute;
  top: 10%;
  right: 0;
  bottom: 0;
  left: 0;
  border: 1px solid black;
}
.menu {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 20%;
  border: 1px solid green;
  overflow-x: hidden;
  overflow-y: scroll;
}
.menu-item {
  margin: 1rem;
  padding: 0.5rem;
}
.menu-item:hover {
  color: white;
  background-color: #505050;
}
.content {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 20%;
  border: 1px solid blue;
  overflow-y: scroll;
}
.content p {
  margin: 1rem;
}

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

Is it possible to include padding within a textarea element?

Is it possible to add some left margin inside a textarea? I would like there to be some extra space because I am using an image icon as my button within the textarea, and when I type, the words end up covering the image. <div id="inputBox"> < ...

PHP - Unable to store the input content

I've encountered an issue with my website where I need to create a form for users to submit new information or news. Below is the code snippet that I have: <?php include "0begin.php"; $title=$_POST["title"]; isset($title) or $title=$_GET["tit ...

If a span element does not exist, a bullet can be added within it

Is there a way to add and remove a bullet from an li element with the selected class, only if there is no span already present? Currently, every time the link is clicked, a bullet gets added. What would be the best approach to avoid adding multiple bullets ...

Using CSS to style PHP function results

I am working on a shopping cart application and I want to incorporate a condensed version of the cart onto my home page. The main shopping cart page is called 'modcart.php' and I have managed to integrate it into my homepage using the following c ...

Bootstrap allows you to create a responsive grid

I need assistance with making my bootstrap grid responsive for multiple input search filters on mobile devices. Currently, the layout is not showing correctly on mobile phones. On desktop, the output looks fine: https://i.stack.imgur.com/bKPUv.png Howev ...

Dimensions of Titles (H1, H2 etc)

I am looking to have a unique background color for my headers from h1 through h6. The width of this background should match the width of the text in the header, along with padding. Currently, the background width is matching the container instead of the te ...

What is the method for determining the width of a Mat-Table once it has been displayed?

When utilizing Angular Material Mat-Table in conjunction with Angular 8, I am passing the dataSource dynamically. The number of rows and columns varies each time. Is there a method to calculate the width of the table once it is rendered on the screen? &l ...

Is it possible to use a JQuery function after a page redirect has occurred

Take a look at this interesting fiddle! View the Fiddle I am interested in creating links that scroll to different sections of content areas on my site, similar to the footer links in the example. I have been suggested to use Anglers routing system, but ...

Using Selenium with C# to input text into an HTML <input> tag is not functioning properly

I need help figuring out how to fill in an input field on a website using HTML. Here is what the section of the website () looks like: <div class="flex-item-fluid"> <input autocomplete="username" autocapitalize="off" ...

How to eliminate looping animations with jQuery

While looping through items, I am encountering an issue where an animation triggers when the loop returns to the first div. I simply need a way to switch between divs without any animations on a set interval. $(document).ready(function () { function s ...

Steps for implementing a conditional statement to handle an empty string in HTML

I need to figure out how to display a different message if my string is empty. Can anyone help me with this? <div class="padding" id="dealBorder"> <pre id="informationDealText"><pan class="inner-pre" style="font-size: 24px; color: whi ...

Conflict between jQuery and dynamically changing page content

I've come across multiple discussions on SO regarding similar topics, but I haven't been able to successfully troubleshoot my code to achieve the desired functionality. I'm currently working on an application that involves dynamically chang ...

Having trouble displaying data from a MongoDB database using ng-repeat in AngularJS

Currently, I am facing an issue while trying to retrieve data from the database and display it using ng-repeat. The getAll function in the factory is fetching the data properly, returning an object with all the information. However, when I try to display t ...

Is there a way for me to determine if a user has engaged with a form?

I'm surprised by how difficult it was to find information on this topic through Google. Currently, my struggle lies in wanting my form fields to change color based on validity only after the user has interacted with the form. I don't want invali ...

I am facing an issue where my Tailwind classes are not functioning properly when passed through props, but they work seamlessly when I directly link the Tailwind CSS using

//Button.jsx import React from 'react'; const Button = (props) => { let color = props.color || 'blue'; return ( <button className={`px-4 py-2 font-bold text-black bg-${color}-500 rounded-full hover:bg-${color}-700 focus ...

Can you please explain the primary distinction between these two identification numbers?

Can you explain the distinction between the following two css ids? p#id1 { insert code here } and #id1 p { insert code here } ...

Creating a hierarchical structure in HTML to represent a JSON object as a tree: techniques and best practices

I need help with displaying a nested JSON object that shows a one to many relationship between a parent node and its children in an organizational chart format using HTML. I have looked into utilizing Google Charts for this purpose, but I am unsure if it a ...

Deactivate event handling for viewport widths below a specified threshold

Currently, I am attempting to deactivate a script when the width of the window falls below 700px. Despite reviewing suggestions from various sources, I have not been successful so far. window.onresize = function () { if(window.innerWidth < 700) { ...

What is the best way to vertically align my image for perfect centering?

On one of my pages at , there is a section titled "FREE PICK UP AND RECYCLING OF LARGE QUANTITIES OF ELECTRONICS" with a picture of a truck. The image is perfectly centered vertically between the pictures on its left and right. Another page I have at fea ...

Is it possible to align an entire column to the right in the Material UI Data Grid?

I'm currently exploring Material UI and grappling with a specific challenge. Is there a method within Material UI to create a Data Grid with two columns, one left-aligned and the other right-aligned? I've managed to align the headers as desired, ...