Pure CSS - Fixed top menu after scrolling past header

Can we achieve the following using pure CSS without the use of JavaScript, by utilizing the sticky position property?

* {
  margin: 0;
  padding: 0;
  font-family: arial;
}

header {
  width: 100%;
  height: 200px;
  background: grey;
}

.sticky-nav {
  position: sticky;
  top: 0;
  width: 100%;
  background: black;
  color: pink;
}

.nav-items {
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  list-style-type: none;
}

.nav-items li {
  margin: 10px 20px;
}

.header-content {
  font-size: 3rem;
  padding: 20px 10px;
}

.site-content {
  min-height: 2000px;
  background: pink;
  padding: 30px;
}
<header>
  <div class="sticky-nav">
    <ul class="nav-items">
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
    </ul>
  </div>
  <div class="header-content">
    Part of the Header
  </div>
</header>

<div class="site-content">
  <p>
    When the user scrolls to this point (site-content) - the top nav should now be sticky
  </p>
</div>

Check out the jsfiddle link for reference: https://jsfiddle.net/eyfbh3v2/

Is it feasible to make the top sticky-nav fixed to the top of the page only after the header has scrolled out of view or when .site-content reaches the top of the page during scrolling?

Answer №1

Unfortunately, the sticky property can only be used within the parent tag where it is defined.

This value always creates a new stacking context. It's important to note that a sticky element will "stick" to its nearest ancestor with a scrolling mechanism, regardless of whether it is the closest scrolling ancestor or not. This can limit the effectiveness of the sticky behavior as discussed in the GitHub issue on W3C CSSWG.

https://developer.mozilla.org/en-US/docs/Web/CSS/position

If you prefer not to alter the structure of your page, one workaround is to utilize fixed positioning for the sticky-nav tag:

.sticky-nav {
  position: fixed;
  top: 0;
  width: 100%;
  background: black;
  color: pink;
}

Another approach is to place the sticky tag at the same parent level as the site-content.

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

Incorporating a variety of images into this hexagonal design framework

Hello there! I am just starting to learn CSS and HTML, and I have a question. Is it possible for me to use the same CSS styling but have different images inside each of the hexagons? I want to replace the image '' with multiple images without rep ...

Scrolling to does not function properly with Material UI tabs

Looking for a solution to scroll to a specific div when clicking on a tab in Material UI Tabs using UseRef and ScrollTo. Check out the sandbox link here: https://codesandbox.io/s/exciting-sound-mrw2v Currently, when I click on Tab 2, I expect it to autom ...

Easily toggle between different content within the same space using Twitter Bootstrap Tabs feature. Display the tabs

I made a modification to the bootstrab.js file by changing 'click' to 'hover': $(function () { $('body').on('hover.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) { e.p ...

Is it possible to use the Shiny conditionalPanel in a horizontal orientation?

Currently, I am working on crafting a shiny app that utilizes sidebarLayout() and the goal is to display either one or two plots side by side in the mainPanel(). The decision on how many plots to show is based on an input value set in the sidebarPanel(). ...

Tips for adjusting the maximum width and content-based width for columns within an HTML/CSS table

I'm working on a simple HTML/CSS code featuring a container with two adjustable slots, controlled by a handler in the middle to modify the space each slot occupies. The first slot contains a table with fixed columns, automatic columns, and limited col ...

IE9 does not properly display SVGs used as background images

Utilizing SVG files as backgrounds for my HTML elements has been successful in most major browsers. However, an issue arises when trying to ensure compatibility with Internet Explorer 9. The problem lies within IE9's rendering of the SVG backgrounds, ...

Combining two kebab-case CSS classes within a React component

import React from 'react'; import styles from './stylesheet.moudle.css' <div className={styles['first-style']} {styles['second-style']}> some content </div> What is the correct way to include styles[&ap ...

Is there a way to adjust the sizes of images within a flexbox layout?

I am currently working on developing a website for my portfolio and I want to achieve a similar layout to this: https://i.stack.imgur.com/giJNF.png However, I am facing some difficulties with displaying the Twitter/Instagram and Deviantart icons. I am usi ...

Learn the steps to show VAT following the price in Magento

I'm encountering a bit of difficulty. Our client has requested that the prices displayed on the listing and product pages should not include VAT, which is understandable. However, they also want it to be indicated after the price as '+VAT (£0.00 ...

Distribute uniform width among child elements using CSS styling

In a fixed-width area, I may need to place either four divs or two divs based on different circumstances. If there are only two divs, they should occupy 50% of the width each: On the other hand, if there are four divs, they should be stacked like this: ...

Issues with the transparency of a basic tab image gallery (CSS + jQuery)

My tab image gallery project is nearly complete, but I'm facing some challenges with the styling. The default thumbnail doesn't load with maximum opacity as intended and my jQuery code seems to be interfering with the hover effect of my CSS. I di ...

Is there a bug with Kendo UI? The Kendo grid pager seems to be misaligned with the bottom

Recently, I've been experimenting with Kendo UI and encountered a specific issue: The pager for my Kendo grid is not aligning properly at the bottom of the grid. Although I've attempted to adjust the CSS styling for k-grid-pager, the issue persis ...

Iterate through Javascript quotes and automatically navigate to the first quote once the loop is completed

I've encountered an issue with the script where I am unable to loop back to the first element once all child elements have been traversed. https://jsfiddle.net/pad5bp0o/2/ Here is the jQuery code snippet: $(document).ready(function() { var length ...

Fluid centering of DIV content both vertically and horizontally

Check out this example: When dealing with fluid divs, line height won't work as expected. My current code relies on line-height which doesn't work when the box sizes change. How can I ensure that a link (content) always appears perfectly centere ...

What could be causing the images to not be found on my web server?

Recently, I've encountered an odd issue with my website. For some reason, the images in my folders are not displaying when I try to access them. Despite trying various solutions, I am still unable to figure out the problem. In addition to this, my CS ...

Utilizing numbered lists through HTML, CSS, and/or JavaScript

I am looking to accomplish something similar to this... My guess is that the images should be stored in an array in Javascript with the image links. However, I am uncertain if this can be done using only HTML and CSS. I found a solution on this thread, w ...

Incorporating a fade-in effect on a navigation data sprite

Help needed with creating a fade-in effect for sprite buttons in my navigation menu. I want the timing to match the icons at the bottom of my website, but I'm struggling to apply it. Currently, the navigation buttons change instantly or have a slide e ...

Position icons and images on the right side, and the textarea on the left side of the page (using Twitter

Summary: Here is the desired end result shown in the image below. Alternatively, you can refer to the JSFiddle. Ideally, only CSS should be used without changing the DOM structure. The icons need to be aligned completely to the right using the .pull-right ...

Upon returning to the website homepage from another page, the JavaScript animation ceases

I implemented a unique typewriter animation on my homepage by utilizing code from this source and making minor HTML edits to customize the content. https://codepen.io/hi-im-si/pen/DHoup. <h5> <body>Hello! I am Jane.</body> < ...

Unable to get the DIV to float within its parent div

I'm having an issue with 2 DIV elements (the ones labeled "Module" below) not behaving as expected! When I try to float them so they appear side by side, they end up jumping outside of the parent container and falling below it. I've tried to trou ...