When a specific height threshold is surpassed, the sticky element loses its stickiness

Having trouble with implementing a sticky header? Here's a simplified version of the code:

HTML:

<div class="page">
  <div class="header">
    kkk
  </div>
  
  <div class="nav">
    kkk
  </div>
  
  <div class="main">
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
  </div>
</div>

CSS:

.page {
  height: 100vh;
    display: grid;
    grid-template-rows: auto 1fr;
    grid-template-columns: auto;
      grid-template-areas:
        "header header header"
        "nav main main";
}

.header {
  position:sticky;
  top:0;
  height: 120px;
  background: blue;
  grid-area: header;
}

.nav {
  height: 100%;
  background: red;
  grid-area: nav;
}

.main {
  background: green;
  grid-area: main;

  
.item {
    height: 50px;
    padding: 20px;
 }

Looking for a solution to make the header stay on screen while scrolling down? Any suggestions that would work with this layout?

Answer №1

Replace height with min-height in the page element

.page {
  min-height: 100vh;
    display: grid;
    grid-template-rows: auto 1fr;
    grid-template-columns: auto;
      grid-template-areas:
        "header header header"
        "nav main main";
}

.header {
  position:sticky;
  top:0;
  height: 120px;
  background: blue;
  grid-area: header;
}

.nav {
  background: red;
  grid-area: nav;
}

.main {
  background: green;
  grid-area: main;

  
.item {
    height: 50px;
    padding: 20px;
 }
<div class="page">
  <div class="header">
    kkk
  </div>
  
  <div class="nav">
    kkk
  </div>
  
  <div class="main">
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
    <div class="item">kkk</div>
  </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

Prevent horizontal HTML scrolling while displaying a layer

I am currently working with a .layer div element that darkens the page to highlight a modal. However, I have encountered an issue where upon triggering the event, the div does not occupy 100% of the screen and the original browser scroll bar disappears. I ...

Upgrade your input button style using jQuery to swap background images

I have an input button with an initial background image. When a certain condition changes, I want to update its image using jQuery without overriding the button's global CSS in the stylesheet. Is there a way to only change the background attribute wit ...

Having trouble retrieving the routeName as it consistently returns empty

I attempted to extract the routeName from the URL in order to apply a different class to the body's layout when on the /Category page. https://i.stack.imgur.com/J0hsp.png @{string classContent = Request.QueryString["routeName"] != "/ ...

Difficulty adapting CSS using JavaScript

I am looking to adjust the padding of my header to give it a sleeker appearance on the page. I attempted to achieve this with the code below, but it seems to have no effect: function openPage() { var i, el = document.getElementById('headbar' ...

Ways to conceal an HTML checkbox while displaying its accompanying label

Recently delving into jQuery, I have been exploring how to utilize the jQuery-ui Selectable component as a substitute for checkboxes. http://jqueryui.com/demos/selectable/ I believe I may be able to achieve this if I can discover a method to conceal the ...

Retrieve CSS content from a file hosted on the local server

Is it feasible to use a locally hosted CSS file for styling a website managed by a server-based CMS? I am interested in utilizing SASS for local CSS development while retrieving the HTML content from a centrally hosted CMS server. ...

How can I use ontouchstart and ontouchend events in jQuery?

Currently, I am using the following code to change the class of elements on touch: ontouchstart="$(this).addClass('select');" ontouchend="$(this).removeClass('select');" I was wondering if there is a more efficient way to achieve this ...

Challenges migrating from Bootstrap 3 to Bootstrap 4

My current challenge involves updating some code that previously worked with a version of Bootstrap 3 and now I am transitioning to version 4.3.1. Specifically, I am working on altering the navbar code, which has undergone significant changes from version ...

The feature to select a cell on a Bootstrap table is not functioning as expected

Is there a way to retrieve the Cell value from a Bootstrap table when clicked? I have a method set up, but for some reason, after clicking on the table heading, the tdOnClick() function stops working. It seems to only be functional before clicking on the t ...

Tips for positioning a div between two vertically aligned input elements

I'm trying to figure out how to place a div between two inputs with the same height and vertically aligned. It seems like a simple task, but for some reason, the code below isn't working: input{ width: 35%; border: 1px solid #A7A7A7; ...

how to position one div above another using css

I currently have a page layout with 7 different div elements structured like this; <div id="container"> <div id="head"></div> <div id="A"> <div id="A1">A1</div> <div id="A2"></div> ...

Setting a fixed width for the body element results in alignment problems

I'm struggling with aligning divs properly on my HTML page that has a tab using CSS and jQuery. Whenever I try to set a fixed width for the body or main container, everything goes out of place... How can I ensure that the body has a minimum fixed widt ...

What is the best method for creating a fade effect on a div background?

I am trying to animate a div with the id #test from a 0 opacity background to an opacity of 0.7 using CSS rgba values, but for some reason, the .animate function is not working as expected. Here is my CSS: #test { background-color: rgba(0, 0, 0, 0); ...

Extend child flex boxes vertically when the main axis is horizontal

My flex boxes have the flex-direction set to row and align-items set to stretch. Despite setting align-self to stretch for the child boxes, they only fill the top part of the browser window. Can someone help me troubleshoot this issue? #containerDiv { ...

Is there something I'm overlooking in my image navigation? It doesn't seem to be interactive

Looking for assistance regarding the issue below. I can't seem to make my image clickable, and I'm not sure what's causing this problem. <img src= "makeup.html.jpg" alt= "Make up by Lauren Evans logo" title= "Make ...

Creating a list of font sizes for each <p> tag in my HTML document

I am looking to create an array containing the font sizes of all the p tags in my HTML document. How can I specifically target only the p elements and not their parent elements? ...

What could be causing the image not to appear on the React app?

I'm currently working on a react website, and I'm facing an issue with the image display on the single product page. Despite trying to tweak both the react and CSS code, I haven't been able to resolve the problem. Below is my react file: im ...

Using AngularJS to handle click events and blur events

I am attempting to dynamically change the class based on user interaction in my code. The class should switch between "large" and "active" depending on whether the input field is filled or not. Please see the modified script below. <div class="snippe ...

Tips for displaying a setCustomValidity message or tooltip without needing to submit the event

Currently, I am utilizing basic form validation to ensure that the email entered is in the correct format. Once validated, the data is sent via Ajax for further processing. During this process, I also check if the email address is already in use and whethe ...

disappearance of background image at bootstrap breakpoint

Hey there, I'm diving into a new journey in web design and I've encountered my first newbie issue. I'm attempting to place an image and text side by side on my web layout, but as soon as the breakpoint hits below 560px, the image disappears ...