What are some solutions for solving a sticky header issue in conjunction with a grid layout?

Why does the sticky position of the header not work when I scroll more than 100% height?

* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
}

html,
body {
  height: 100%;
  width: 100%;
}


/* -------------------- Headers -------------------- */

.social-menu {
  display: none;
}

header {
  top: 0;
  position: -webkit-sticky;
  position: sticky;
  display: flex;
  justify-content: center;
}

header nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  max-width: 1100px;
  width: 100%;
}

#nav-check {
  display: none;
}

#nav-check:checked~ul {
  display: grid;
}

header nav ul {
  display: none;
}


/* -------------------- Hero -------------------- */

.hero {
  height: auto;
}


/* -------------------- Noticias -------------------- */


/* -------------------- Patrocinadores -------------------- */

@media (min-width: 600px) {
  /* -------------------- Headers -------------------- */
  header nav label {
    display: none;
  }
  #nav-check:checked~ul {
    display: flex;
  }
  .social-menu-father {
    display: flex;
    justify-content: center;
  }
  .social-menu {
    display: grid;
    grid-template-columns: 1fr 1fr;
    height: 46px;
    align-items: center;
    padding: 0 40px;
    max-width: 1100px;
    width: 100%;
  }
  .social-menu div {
    display: flex;
    justify-content: flex-end;
  }
  header nav {
    height: 80px;
  }
  header nav ul {
    display: flex;
    justify-content: space-between;
  }
  /* -------------------- Hero -------------------- */
  .hero {
    height: calc(100% - 126px);
  }
  /* -------------------- Noticias -------------------- */
  /* -------------------- Patrocinadores -------------------- */
}
  <div class="social-menu-father">
    <div class="social-menu">
      <a href="">Twitch online</a>
      <div>
        <a href="">a</a>
        <a href="">a</a>
        <a href="">a</a>
        <a href="">a</a>
      </div>
    </div>
  </div>

  <header>
    <nav>
      <a href=""><img src="https://i.pinimg.com/originals/08/b5/07/08b5070ece24d17eea517ba0e2b188b4.png" width="50px"></a>
      <input type="checkbox" id="nav-check">
      <label for="nav-check">asd</label>
      <ul>
        <li><a href="">Home</a></li>
        <li><a href="">Noticias</a></li>
        <li><a href="">Equipos</a></li>
        <li><a href="">Patrocinadores</a></li>
        <li><a href="">Club</a></li>
      </ul>
    </nav>
  </header>

  <div class="hero">
    asd
  </div>
  <div>
    <p>a</p>
  </div>

Answer №1

To ensure proper layout, the body element should have a min-height instead of setting the height to 100%. Additionally, change height: calc(100% - 126px) to height: calc(100vh - 126px) for the hero header as percentages will no longer work effectively.

* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
}

html {
  height: 100%;
}

body {
  min-height: 100%;
}



.social-menu {
  display: none;
}

header {
  top: 0;
  position: sticky;
  display: flex;
  justify-content: center;
}

header nav {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  max-width: 1100px;
}

#nav-check {
  display: none;
}

#nav-check:checked~ul {
  display: grid;
}

header nav ul {
  display: none;
}

@media (min-width: 600px) {
  header nav label {
    display: none;
  }
  #nav-check:checked~ul {
    display: flex;
  }
  .social-menu-father {
    display: flex;
    justify-content: center;
  }
  .social-menu {
    display: grid;
    grid-template-columns: 1fr 1fr;
    height: 46px;
    align-items: center;
    padding: 0 40px;
    max-width: 1100px;
    width: 100%;
  }
  .social-menu div {
    display: flex;
    justify-content: flex-end;
  }
  header nav {
    height: 80px;
  }
  header nav ul {
    display: flex;
    justify-content: space-between;
  }
  .hero {
    height: calc(100vh - 126px);
  }
}
<div class="social-menu-father">
    <div class="social-menu">
      <a href="">Twitch online</a>
      <div>
        <a href="">a</a>
        <a href="">a</a>
        <a href="">a</a>
        <a href="">a</a>
      </div>
    </div>
  </div>

  <header>
    <nav>
      <a href=""><img src="https://i.pinimg.com/originals/08/b5/07/08b5070ece24d17eea517ba0e2b188b4.png" width="50px"></a>
      <input type="checkbox" id="nav-check">
      <label for="nav-check">asd</label>
      <ul>
        <li><a href="">Home</a></li>
        <li><a href="">Noticias</a></li>
        <li><a href="">Equipos</a></li>
        <li><a href="">Patrocinadores</a></li>
        <li><a href="">Club</a></li>
      </ul>
    </nav>
  </header>

  <div class="hero">
    asd
  </div>
  <div>
    <p>a</p>
    <p>a</p>
    ...
    <p>a</p>
    <p>a</p>
  </div>

Changing the body's height to 100% can result in restricted sticky areas and overflow issues. To illustrate this better, a border has been added:

* {
  margin: 0;
  padding: 0;
  list-style: none;
  text-decoration: none;
}

html {
  height: 100%;
}

body {
  height: 100%;
  border: 2px solid;
}


...
<div class="social-menu-father">
    <div class="social-menu">
      <a href="">Twitch online</a>
      <div>
        <a href="">a</a>
        <a href="">a</a>
        <a href="">a</a>
        <a href="">a</a>
      </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

Align text vertically within a link box by overriding inherited CSS styles

Fiddle: http://jsfiddle.net/jgallaway81/ax9wh/ <a href="lcars.jfx.php" class="leftbuttons buttonlinks antibutton"> LCARS Locomotive O.S. </a> My issue pertains to the alignment of text within a graphic. I am utilizing this particular button ...

Is it possible to use Bootstrap without using rows?

I am a fan of using bootstrap, but I am currently facing a challenge that requires a grid layout where cells stack vertically without any visible lines separating them. Similar to the layout seen on Pinterest. While Bootstrap's normal grid system wor ...

A step-by-step guide on toggling between light mode and dark mode using water.css

Implementing the water.css on my website, I have this JavaScript code: function setTheme(themeName) { localStorage.setItem('theme', themeName); document.documentElement.className = themeName; } // function to toggle between light and dark ...

Lag experienced in Chrome browser on Mac due to Bootstrap CSS framework

I created a basic HTML and CSS file using Bootstrap that runs smoothly on my work computer with Chrome version 27.0.1453.110. However, when I attempt to open the file in Chrome on my personal Mac (unknown Chrome version), it loads very slowly and experienc ...

The contenteditable div's selectAll feature doesn't function properly when it gains focus

I'm working with divs in a table structure and here's an example: <div contenteditable="true" onfocus="document.execCommand('selectAll',false,null)">Something</div> Clicking on a div to focus works perfectly, selectin ...

Whenever I insert an http link for FontAwesome, the icons work perfectly fine. However, when I try to host it locally,

After providing the CDN link: <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" rel="stylesheet"> The icons are displaying correctly and everything is working fine. You can view the code on ...

What is the reason for the additional space and irregular alignment of my divs when utilizing the 960 grid system?

One issue I'm facing is that elements within divs are aligning randomly without responding to any alignment tags. Additionally, some divs are creating extra space above or below their elements. I am using the 960 grid system and have not made any chan ...

How come my button is overflowing the container on Chrome?

I have adjusted a div to be 78% of the viewport height (78vh). Since there are 13 buttons, each button is set to be 6% of the viewport height (6vh) so that all 13 buttons should fit within the parent container size of 78vh. In Firefox, everything appears c ...

What is the best way to position a button and text at the bottom right corner in HTML?

Is there a way to align a button and text on the bottom right in html? I am trying to create a layout where a button is at the bottom right with text next to it, similar to online stores ... Why are there 3 buttons at the top and the last one at the bottom ...

Building Nested Div Tags

I'm struggling with creating nested divs similar to the layout shown in the linked image. Image Could someone please assist me in achieving this? .demo-container { padding: 30px; border: 1px solid #e2e4e7; background-color: #f5f ...

Guide to creating animated sections using only CSS as you scroll the webpage

I am searching for a method to add animation effects (using @keyframes, transform...) to certain elements as the user scrolls down the page. For instance: When the offset is 0: the div will have a height of 100px. When the offset is between 0 and 100: th ...

Having trouble implementing font css file in Reactjs

When working with Reactjs (Nextjs), every time I try to incorporate "css" into my project, I encounter the following error on my screen: Module not found: Can't resolve '../fonts/fontawesome-webfont.eot?v=4.7.0' How can I solve this issue? ...

Automatically adapt the height of elements to ensure they are consistently aligned on a single page

I'm working on adjusting the height of elements on my page https://i.sstatic.net/tqRs9.png My goal is to dynamically change the height of A and B based on screen height so they always fit within a single screen without requiring scrolling. Currentl ...

Hey there, I was wondering if it's possible to modify the color of the navbar when scrolling on a specific page

After the first 3 pages with a black background and a transparent navbar with white navigation items, I encounter visibility issues when scrolling to the colorful 4th page. Is there a way to change the navbar color on this specific page using ONLY HTML a ...

Design personalized social media icons that allow users to easily share the link of the current page

Currently, I am working on creating a widget that includes social sharing buttons. The goal is for these buttons to share the URL of the current page when clicked on various social media platforms. For instance, I attempted to integrate a Twitter share but ...

Modify the color and CSS styling of inactive text boxes according to a boolean variable

<label [invalid]="false" invalidText="" placeholder="Placeholder text"> Middle names (optional) <input ibmText [attr.disabled]="isApplicationLocked" [invalid]="false" placeholder="&quo ...

Adjust the size of the list items to fit the window as it

My full-width and height div is overlaid by li elements. The li's have a width of 10% and a height of 20vh to create square shapes within the entire div. However, resizing is causing alignment issues for the div. Despite my attempts using % and vh un ...

Having trouble selecting an option within an options wrapper using Python's Selenium?

I'm currently facing an issue with selecting an option in a popup bubble (I am unsure of the exact type of popup it is). I don't have a preference for which option to choose, I just need to click one of them. Here is how it appears in the code: & ...

Selenium WebDriver: The challenge of using visibilityOfElementLocated when the element is not actually visible

I've encountered an issue with the Firefox Webdriver where it fails to accurately determine if an element is visible. Here is the code I am using, which incorrectly indicates that the element is visible: wait.ignoring(UnhandledAlertException.class).u ...

Is it possible to instruct Vue to prioritize the inherited class over others?

I have a situation in my component where I need Vue to disregard the existing class and instead use the inherited class, but only if it is of the same type as the inherited class. Let me illustrate with an example: Consider a button component like this M ...