Tips for preventing a div from scrolling beyond the header with the use of position: sticky?

My header utilizes the CSS property position: sticky; to stick at the top of the page. However, this creates a gap between the header and the top of the page that I intentionally left there. As you scroll down, you can see the content div moving under and past the header. I could use top: 0; to prevent this scrolling behavior, but the gap serves a purpose. How can I adjust this so that the scrolled content disappears before passing the header?

I attempted to solve this issue by using overflow, but it resulted in a scrollbar appearing inside the div itself, which is not the desired effect. Instead, I would like to be able to scroll anywhere on the page. Additionally, when zooming in on the page, two scrollbars appear rather than just one. Another problem I encountered was that the rounded corners of the header border are disrupted when using overflow, as the content div stops at the same point as the header, causing the hard edges of the content div to show below the rounded corners.

body {
  margin: 40px;
}

#container {
  display: flex;
  flex-direction: column;
}

#header {
  height: 100px;
  background-color: lightblue;
  border: 6px solid black;
  border-top-left-radius: 12px;
  border-top-right-radius: 12px;
  position: sticky;
  position: -webkit-sticky;
  top: 40px;
}

.content {
  background-color: green;
  border-left: 6px solid black;
  border-right: 6px solid black;
}

#footer {
  height: 100px;
  background-color: lightblue;
  border: 6px solid black;
  border-bottom-left-radius: 12px;
  border-bottom-right-radius: 12px;
}
<div id="container">
  <div id="header">
    <h1>Header</h1>
  </div>

  <div class="content">
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
    <h2>CONTENT</h2>
  </div>

  <div id="footer">
    <h1>Footer</h1>
  </div>
</div>

Answer №1

Feel free to incorporate the following:

display: inline-block;

Insert this into the element and test its functionality.

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

Create a triangular shape to fit on the right side of a 'li' element

After defining the following HTML: <ul class="steps d-flex"> <li class="step">Basic Details<div class="triangle triangle-right"></div></li> </ul> With this given CSS: .steps li { @ex ...

What is the best way to restrict the number of iterations in ngFor within Angular HTML

I want to use ngFor to display a maximum of 4 items, but if the data is less than 4, I need to repeat the loop until there are a total of 4 items. Check out this example <img *ngFor="let item of [1,2,3,4]" src="assets/images/no-image.jpg" styl ...

What steps can I take to successfully implement Tailwind CSS's max-w class?

After successfully using the proper class for setting max width in tailwind, it suddenly stopped working. I'm baffled by the fact that the max-w has refused to take effect. <section class="max-w-[80em]"></section> Previously, I ...

Set the radio button in Angular to be checked on the first option

I recently created a dynamic form with data from an API, including an ng-repeat to generate multiple radio buttons. Everything is working well, but I'm struggling to set the first radio button as checked by default. Any suggestions or solutions would ...

Uncovering the Mystery: Extracting a Video Thumbnail from Brightcove Videos

Is there a way to extract the video thumbnail from Brightcove Video? <x:out select="$item/description" escapeXml="false" /> At the moment, the thumbnail is being retrieved within the description. ...

Extracting data from a text editor using React.js

Currently, I am using a Quill JS component, which is a text editor designed for React JS. I am thoroughly testing its features and functionalities. I have successfully created an editor with a toolbar. However, I am facing a dilemma. I am unsure of how to ...

Is it possible to "preload" an image using JavaScript in order to utilize it as a CSS background-image?

Is it possible to pre-load images into a web page using Javascript in order to use them as CSS background images without experiencing any delay due to requests or uploads? If it is possible, how can this be achieved? ...

What causes items within an Array() to be interdependent? (Javascript)

Here is what I attempted: let array = Array(2).fill([]); array[0].push(1); Expected result: array = [[1], []] Actual result: array = [[1], [1]] Can someone explain why the second element is affected by the first in this scenario? Appreciate any help in ...

Focus on a primary window

When a new window is opened using the target attribute of _blank, is it possible to target links to open in the original "parent" window? Appreciate your help! ...

Obtaining the height of a div's parent element while utilizing the d-flex class in Bootstrap

I'm having trouble getting the navigation menu to be the full height of the bordered div and I can't seem to pinpoint where the problem lies. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_em ...

"Typescript: Unraveling the Depths of Nested

Having trouble looping through nested arrays in a function that returns a statement. selectInputFilter(enteredText, filter) { if (this.searchType === 3) { return (enteredText['actors'][0]['surname'].toLocaleLowerCase().ind ...

JavaScript's functionality is limited to using alert() and Debug mode

There seems to be an issue with Chrome autofill fields where the 'autocomplete="false|off|others"' attribute is being ignored. I've attempted various solutions such as using hidden fake fields, this, this, and others, but they don't app ...

Updating Bootstrap modal content based on button clickExplanation on how to dynamically change the content

I am looking to dynamically change the content displayed inside a modal based on the button that is clicked. For example, clicking button one will only show the div with the class 'one' while hiding the others. $('#exampleModalCenter&a ...

Tips for setting the id parameter on the asp-route-id for the modal button

My code is causing me some trouble. I have a table with 3 columns: category, subcategories and actions. In the third column, I display buttons "Edit" and "Delete". Here is a snippet of my code: <tbody> @foreach (var category in Model.ToList() ...

Steps for serving audio files in a Spring Boot application

Can someone advise on the best location to place my audio folder in order to reference it as src="audio/audio_name" within an HTML audio tag? I attempted placing it in the resources folder, but when using src="http://localhost:9191/resource ...

Error: Unable to find axios module

screenshot of browser developer tool - network I have embedded the script in my base.pug file script(scr='https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js') Upon loading the page and inspecting the browser developer tool > n ...

Undefined React custom hooks can be a tricky issue to troub

Just delving into the world of React and trying to wrap my head around custom hooks, but I keep hitting roadblocks. Here's the latest hurdle that I'm facing, hoping for some guidance. I'm taking a step-by-step approach to creating a functio ...

React Conditional State Setting

I have created a component that dynamically changes its background color based on the rating of a school. According to the specifications, if the school's rating falls between 1 and 3, the background color should be orange. For ratings between 4 and ...

Working with promises and Async/Await in Express/node can sometimes feel ineffective

Currently, I am delving into the world of node and express with the aim to create a function that can extract data from a CSV file uploaded by the user. My challenge lies in the fact that the data is being outputted as an empty array before it goes through ...

Text that weaves around in a circular motion

Greetings, I'm relatively new to coding but have made progress so far. I'm eager to learn how to wrap my text within a circle, ensuring it stays within the boundaries of the circular shape. Any guidance or tips on achieving this would be greatly ...