Sticky sidebar following a complete-page header

I recently added a fixed sidebar to my website:

However, I'm facing an issue where the sidebar appears in front of my full-page header when I set its position to fixed. What I actually want is for the sidebar to start after the header as if it were positioned absolutely with top: 100vh, but still remain fixed when scrolling down.

Is there a way to achieve this desired effect? Thank you for any help!

body {
  margin:0;
}

#header {
  height: 100vh;
  width:100%;
  position:relative;
  background-color:blue;
}


#main {
        margin-left: 20vw;
        height: 100vh;
    }
    
    sidebar {
        width: 20vw;
        height: 100vh;
        top: 0;
        left: 0;
        background-color: #130E0A;
        position: fixed;
        z-index: 1;
    }
<!DOCTYPE html>
    <html>
        <head>
    <title>Exploring Controversy: The Four Color Theorem and Computer Proofs</title>
            <meta name='viewport' content='initial-scale=1.0'>
            <meta charset='utf-8'>
        </head>
        <body>
    <section id='header'></section>
    
            <section id='main'>
                <sidebar></sidebar>
                <div id='timeline' class='cd-container'></div>
            </section>
        </body>
    </html>

Answer №1

If you're familiar with jQuery, you can implement something like this:

$(window).scroll(function() {    
    var scroll = $(window).scrollTop();

     // Use >= instead of <=
    if (scroll >= 1000) {
        // Make sure to use "clearHeader" instead of "clearheader"
        $("sidebar").addClass("fixed");
    }
    else {
      $("sidebar").removeClass("fixed");
    }
}); // Don't forget to add the closing parentheses );
body {
  margin:0;
}

#header {
  width:100%;
  background-color:blue;
   height: 1000px;
}


#main {
        margin-left: 20vw;
        height:10000px;
    }
    
    sidebar {
        width: 20vw;
        height: 500px;
        display:block;
        background-color: #130E0A;
        position: relative;
        z-index: 1;
    }
    
    sidebar.fixed {
      position:fixed;
      left:20vw;
      top:0;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
    <html>
        <head>
    <title>Controversy Study: The Four Color Theorem and Computational Proofs</title>
            <meta name='viewport' content='initial-scale=1.0'>
            <meta charset='utf-8'>
        </head>
        <body>
    <section id='header'></section>
    
            <section id='main'>
                <sidebar></sidebar>
                <div id='timeline' class='cd-container'></div>
            </section>
        </body>
    </html>

Answer №2

Take a look at this updated code. I've made some changes to adhere to HTML5 standards and adjusted the CSS. Hopefully, these modifications address your needs.

#main {
    margin-left: 20vw;
    height: 100vh;
}

header {
  width: 100%;
  height: 85px;
  background-color: red;
  padding: 20px;
  
}

#sidebar {
    width: 20vw;
    height: 100vh;
    background-color: #130E0A;
    position: fixed;
    left: 0.9vw;
    z-index: 1;
}
<header>
  I am the Header | Je suis le header.
</header>

<section id="main">
  <div id="sidebar">
    I am the sidebar | Je suis le sidebar.
  </div>
</section>

Answer №3

Below is a handy JavaScript solution that keeps track of your scrolling position and switches to fixed once you pass the header:

var sidebar = document.getElementById('sidebar');
window.addEventListener('scroll',function() {
  var st = document.body.scrollTop,
      mt = document.getElementById('main').offsetTop;
  if (st > mt) {
    sidebar.classList.add('fixed');
  } else {
    sidebar.classList.remove('fixed');
  }
})
body {
  margin: 0;
}

#main {
  height: 100vh;
  position: relative;
}

#header {
  background: red;
}

sidebar {
  background-color: #130E0A;
  width: 20vw;
  height: 100vh;
  font-size: 5em;
  color: white;
  position: absolute;
  top: 0; left: 0;
  overflow-y: scroll;
}

.fixed {
  position: fixed;
}

#timeline, sidebar {
  font-size: 5em;
}

#timeline {
  margin-left: 20vw;
}
<!DOCTYPE html>
<html>

<head>
  <title>Exploring Controversy: The Four Color Theorem and Computer Proofs</title>
  <meta name='viewport' content='initial-scale=1.0'>
  <meta charset='utf-8'>
</head>

<body>
  <section id='header' style='height: 100vh; margin: 0;'></section>

  <section id='main'>
    <sidebar id='sidebar'><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p><p>asdf</p></sidebar>
    <div id='timeline' class='cd-container'><p>asdf</p></div>
  </section>
</body>

</html>

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

AngularJS call to the web service

I am facing an issue where I do not receive any response upon clicking the button. <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular.min.js"></script> <sc ...

Arranging form elements and a map in a side-by-side layout using HTML5

How can I align the contents of a map and a form side by side with responsive design? I've attempted using tables and lists, but the form keeps appearing on top of the map. Ideally, I want the contents to be displayed side by side on wider screens and ...

showing a pair of divs in a split layout on the homepage

I have a partially split homepage with two sections using Twitter Bootstrap v5.1.3 for the layout. The left side consists of two divs, one for text and one for an image. These divs are contained within a flex-parent, giving them an inline-block appearance ...

Guide for assigning a custom style class to a selection field in a Django form

I've been attempting to apply a bootstrap-select class to my choice field, but I keep encountering a 'TypeError: init() got an unexpected keyword argument 'attrs'' error message. class constraintListForm1(forms.Form): region ...

The functionality of hover and custom attribute is not operational on IE8

Unfortunately, I am facing issues with the hover and custom attribute functionality in IE8. Even in compatibility mode, it refuses to work properly. The code snippet provided (fiddle) works perfectly fine on Mozilla, Opera, Safari, and Chrome but encounter ...

AngularJS causing issues with page redirection functionality

Incorporating angularjs routing into my web app has been a big task. I've set up the $locationProvider and included the base tag in the HTML head as <base href="/webapp/"> to get rid of the hash symbol in the URL. It's been smooth sailing w ...

What is the best method for accessing a value in IndexedDB while utilizing service workers?

I am feeling overwhelmed by the concepts of IndexedDB and serviceworkers as I try to transform them into a functional application. Despite my extensive research, including studying various examples, I am struggling to integrate the two technologies effecti ...

R code for extracting data without using CSS path

Hey there, I'm reaching out because I'm struggling to figure out how to extract data from a webpage (""). Learning how to scrape data is my current project, and I'm specifically trying to gather contact information (Office, Fax, email) from ...

What is the reason behind the CSS not rendering when async:false is used?

I find it peculiar and am actively seeking an explanation for this anomaly. In my code snippet, the AJAX call seems to be causing unexpected behavior: $('.edit-config').click(function() { var that = this; var msg; ip ...

Can a custom spellchecking feature be integrated into an HTML textarea?

Question: I am wondering if it is feasible to incorporate a personalized spell checking feature into a Textarea field. Background: Currently, I am utilizing the b-form-textarea component from bootstrap-vue to present a textarea where users can input a li ...

Steps for adding hover color to a div with linear gradient border

My current challenge involves adding a hover color to a specific div, but I'm facing obstacles due to the gradient background that was implemented to achieve border-radius functionality. This task is being carried out within a React Component using c ...

Animating content using CSS keyframes for dynamic changes

I'm trying to change the text of a div using keyframes with the code content:'text';, but unfortunately, the div remains empty and nothing happens. The content inside the #neon-sign div is not updating and stays blank: #neon-sign { p ...

Adjust the padding when hovering using CSS

Hey everyone, newbie here taking a crack at my first Joomla module! I've made some progress but hit a snag with my CSS styling - I think it's an issue with my selectors and my limited understanding of what's going on. Basically, I'm at ...

Tips for preserving the accuracy of data stored in database tables

I need to preserve the character /r/n in the "history" data and ensure it is not excluded when displayed in the HTML. function fetchData($studentId) { return DB::table('tbl_siswa') ->join('tbl_mapel', 'tbl_siswa.id_m ...

Shrink Table Column Size with Bootstrap and Stylus to Optimize Space Usage

Currently experimenting with Bootstrap to ensure my table is responsive on a search list I am developing. However, I am facing an issue where two of my columns are taking up more space than necessary. In the image below, you can see that Date and Link colu ...

Ways to customize the appearance of a unique component

I'm faced with a challenge where I need to make edits to a component that is not editable: import * as React from 'react' import styled from '@emotion/styled' import FlexRow from '../../layouts/FlexRow' const RowContaine ...

Prevent drag and drop functionality in QtWebkit

As I work on creating an HTML GUI for my application using Qt's WebKit, everything is going smoothly with one minor issue. I am trying to prevent users from dragging and dropping images from the GUI itself. While I have successfully disabled text sele ...

Modifying Stroke color in HTML5 Canvas

I am attempting to create a unique visual effect by drawing a circle resembling a clock using canvas 2d context. The idea is to initially draw the circle in black starting at point p1, and then when I complete a full circle tour it should erase as the colo ...

Is it possible to insert an image directly into the <img> tag without having to first send it to the server?

I've created an upload form that allows users to submit images: <form> <input accept="image/jpeg, image/gif, image/png" type="file" name="image" id="image" class="UploadInput" onchange="submitImageUploaderForm()"/> </form> Once t ...

Slide the first div upward to display the text in the second div when hovering over the first div

I'm facing an issue where a div needs to move upwards when hovering over it. To see an example, take a look at this JSfiddle. The desired outcome is as follows: As illustrated, the right div is the one being hovered. I have attempted to create this ...