Design featuring fixed sidebar on the left side and scrollable main content on the right side

Is there a way to create a two column layout with a locked (sticky? fixed?) left menu that stays in place as you scroll through the content on the right side?

Additionally, I believe I will need to use JavaScript (without jQuery) to make the columns resizable. While using CSS for 'cursor: col-resize' seems straightforward, I have no idea how to approach this in JS.

.row {
  display: flex;
}

.column {
  flex: 50%;
  padding: 16px;
}

.one {
  background-color: tomato;
  min-height: 100%;
}

.two {
  background-color: #33a8ff;
  height: 5000px;
}

html,
body {
  height: 100%;
  margin: 0;
}
<div class="row">
  <div class="column one">
    <h2> column #1 </h2>
    <p>menu items</p>
  </div>
  <div class="column two">
    <h2>column #2</h2>
    <p> Some contents in column 2 </p>
  </div>
</div>

You can access an example on jsFiddle here:
https://jsfiddle.net/ojx1g64s/

Answer №1

If you want to make a sidebar sticky, try using the CSS property position:sticky. To ensure it works properly, consider adjusting the height of the sidebar element.

Keep in mind that position: sticky may not function correctly if any parent elements of the sidebar have their overflow property set differently than default. More information on this can be found in this GitHub issue.

* {
  margin: 0;
}

.row {
  display: flex;
}

.column {
  padding: 16px;
}

.sidebar {
  background-color: tomato;
  position: sticky;
  top: 0;
  height: min-content;
  flex:1;
}

.content {
  background-color: #33a8ff;
  height: 100vh;
  flex:2;
}

.bottom{
  background-color: lightblue;
  padding: 16px;
  height: 120vh;
}
<div class="row">
  <div class="column sidebar">
    <h2>Sticky Sidebar</h2>
    <p>Menu items.</p>
  </div>
  <div class="column content">
    <h2>Content</h2>
    <p> Some scrollable content.</p>
  </div>
</div>
<div class="bottom">
  <h2>Content On the bottom</h2>
  <p>Some content outside of the block with the sticky sidebar.</p>
</div>

Answer №2

If you want to achieve the layout similar to what I have in my code snippet below, you can implement it this way. In this design, both columns are set with a height of 100%. The right column is configured with overflow-y: auto so that a scrollbar will only appear when needed to scroll the contents inside that specific column:

* {
  box-sizing: border-box;
}
html,
body {
  height: 100%;
  margin: 0;
}
.row {
  display: flex;
  height: 100%;
  background-color: #33a8ff;
}

.column {
  flex: 50%;
  padding: 16px;
}

.one {
  background-color: tomato;
  height: 100%;
}

.two {
  height: 100%;
  overflow-y: auto;
}
<div class="row">
  <div class="column one">
    <h2> column #1 </h2>
    <p>menu items</p>
  </div>
  <div class="column two">
    <h2>column #2</h2>
    <p> Some contents in column 2 </p>
     ... (additional content for column 2)
  </div>
</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

Do AJAX requests make Cross-site scripting attacks more significant?

Currently, I am in the process of developing a React application that utilizes a PHP (Codeigniter) backend. Despite Codeigniter not handling this issue automatically, I have taken it upon myself to delve into the matter. While I comprehend the importance ...

How can I execute a task following a callback function in node.js?

Is there a way to run console.log only after the callback function has finished executing? var convertFile = require('convert-file'); var source, options; source = 'Document.pdf'; options = '-f pdf -t txt -o ./Text.txt'; ca ...

The :not selector in a chained condition is not functioning properly for a child div

I have a child div that is being impacted by the .img class. Even when I use the :not selector for this div like so .main .content .index-exp .img:not(.view-image){ /*rest*/ } it continues to affect my div. You can view the issue at this link. Is this ...

Is it possible to upload several files using multer?

I can't seem to figure out what's wrong with my code below. I'm trying to upload multiple files, but I keep getting an "UNEXPECTED FIELD" error. I am able to upload a single file without any issues, but the problem arises when I try to uploa ...

Get all the classes from the body element of the AJAX-loaded page and update the body classes on the current page with them

I am currently in the process of AJAX-ing a WordPress theme with a persistent music player. In Wordpress, dynamic classes are used on the <body> tag. The structure I'm working with looks like this: <html> <head> </head> ...

background covering the entire span

Can a background spread between two other backgrounds, creating the illusion of three separate sections? For example, having a top background image, a middle section that can vary in length based on the div size, and a bottom background image. It's s ...

Creating a fresh JSON structure by utilizing an established one

I have a JSON data that contains sections and rubrics, but I only need the items for a new listing. The new object named 'items' should consist of an array of all the items. The final JSON output should be sorted by the attribute 'name&apos ...

Implement handleTextChange into React Native Elements custom search bar component

I need help with passing the handleTextChange function in the SearchBarCustom component. When I try to remove onChangeText={setValue} and add onchange={handleTextChange}, I am unable to type anything in the search bar. How can I successfully pass in the ...

The Date_Format field in the JSON-Data is not displaying

Greetings and apologies for my limited English skills :-) I am facing a challenge with my JSON data. In my MySQL database, I have stored dates in the format YYYY.MM.DD. However, I require the format DD.MM.YYYY. So, I modified the query as follows: $rs = ...

Using Scale Transformation on SVG Element Group

I have included an SVG of a lamp that I designed using Sketch as a demonstration. My intention is for the lamp head to enlarge by a factor (e.g., 1.2 in this case) when the cursor hovers over it - indicating interactivity. However, the issue arises when ho ...

Guide to declaring variables using jQuery

Currently tackling a school project, I stumbled upon some online example solutions and managed to decipher most of the code. However, there is one line that has me puzzled. var $newTweet = $('<div class=tweet></div>'); My assumption ...

What is the functioning of property as a method?

One common practice is to use a method like $('div').show(); in jQuery. This show method will make the div visible when it is called. However, what about using $('div').length;? Although length is considered a property and not a method, ...

Show the helper text when a TextField is selected in Material UI

I would like the error message to only show up when a user clicks on the TextField. Here's my code: import React, { useState, useEffect } from 'react'; import { TextField, Grid, Button } from '@material-ui/core'; const ReplyToComm ...

Moving route.param() calls outside of Express.js

Looking to consolidate repetitive Node.js and Express.js code into a single module. Together.js var express = require('express'); var boom = require('express-boom'); var app = express(); var app.use(boom()); app.param('user& ...

Instructions for implementing a jump event with Javascript on Wordpress

I recently created a page on my Wordpress site with a large amount of content divided into different rows. I'm looking to add jump-to-section links at the beginning of each row and a jump-back-to-top link for easy navigation. However, I'm unsure ...

The dropdown feature within a bootstrap button group is malfunctioning

As part of the web track in cs50, I am working on creating a webpage. My goal is to include options for navigating to the next or previous pages, as well as a dropdown menu with all the pages in between. However, when using Bootstrap's code, the dropd ...

The alterations made to a single dropdown option are causing ripple effects across all other dropdowns

There is an add button that continuously adds a div container containing two dropdowns. The selection in one dropdown affects the data in the other dropdown. When the add button is clicked, a second div with both dropdowns is added. However, changing the ...

inventory items - 6 articles listed in the initial column

I am trying to organize my list into two columns, with the first column containing 6 items. How can I achieve this layout instead of having the content split evenly between the columns? ul { columns: 2; column-count: 2; height: 800p ...

Utilizing dynamic attributes within the CSS styling function

I encountered an issue when attempting the following code. It doesn't seem to be working properly. Could you provide some assistance in figuring out why? var propertyName = 'left'; var propertyVal = $("body").width(); $('nav').cs ...

Searching for an AngularJS and Bootstrap Dual Listbox Solution

I need a component like this to integrate into my project: https://i.sstatic.net/W3PSB.png I am hoping to add it using npm. However, I have tried some of the examples available but faced issues (encountered exceptions or only found bower instead of npm) ...