Is the element refusing to adhere for some mysterious cause?

Hello, I've been attempting to create a navigation bar using the position:sticky property, but for some reason, it's not sticking. I've scoured the internet for solutions, but couldn't find a fix, so here I am seeking help.

/*variables*/

:root {
  --black: black;
  --white: #FFFFFC;
  --pink-white: #FEF7FF;
  --p-pink: #FFC6FF;
  --p-purple: #BDB2FF;
  --p-dark-blue: #A0C4FF;
  --p-light-blue: #9BF6FF;
  --p-green: #CAFFBF;
  --p-yellow: #FDFFB6;
  --p-orange: #FFD6A5;
  --p-red: #FFADAD;
  --box-shadow-val: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  --font-val: 'Montserrat', sans-serif;
  --radius-val: 0.75em;
  --hover-white: #F2F2F0;
}

body {
  margin: 5;
}


/*here*/

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: var(--white);
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  box-shadow: var(--box-shadow-val);
  font-family: var(--font-val);
  border-radius: var(--radius-val);
}

li {
  float: left;
}

li a {
  display: block;
  color: var(--black);
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover {
  background-color: var(--hover-white);
}


/*img is not part of original code just there to show it does not stick*/
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link href="style.css" rel="stylesheet">
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
  <title>Portfolio</title>
</head>

<body>
  <div class="navBar">
    <ul>
      <li><a href="#port" class="Port"><b>What Is Port</b></a></li>
      <li><a href="#priv" class="Priv"><b>Privacy</b></a></li>
      <li><a href="#reg" class="Reg"><b>Get Started</b></a></li>
      <li><a href="#guides" class="Guides"><b>Guides</b></a></li>
      <li><a href="#cred" class="Cred"><b>Credits</b></a></li>
    </ul>
  </div>
  <!-- img is not part of original code -->
  <img src="https://miro.medium.com/max/1400/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg">
</body>

</html>

I am completely baffled as to why it is not working. It could be that I am using it in the wrong place or something else entirely.

My plan is to make it more responsive, but that is a project for another day, so please ignore the poor user interface on smaller windows for now.

Answer №1

When you applied position: sticky; to the <ul>, the element remained in the flow. This means that it will exit the viewport once the parent element leaves the viewport.

Instead of adding sticky to the <ul>, you need to apply it to the containing <div>.

sticky is compatible with all browsers except for IE, which is set for deprecation starting August 16, 2021 and does not support sticky even with vendor prefixes.

/* css changes */
.navBar {
  position: sticky;
  top: 0;
}


/* original css */
/*variables*/
:root {
  --black: black;
  --white: #FFFFFC;
  --pink-white: #FEF7FF;
  --p-pink: #FFC6FF;
  --p-purple: #BDB2FF;
  --p-dark-blue: #A0C4FF;
  --p-light-blue: #9BF6FF;
  --p-green: #CAFFBF;
  --p-yellow: #FDFFB6;
  --p-orange: #FFD6A5;
  --p-red: #FFADAD;
  --box-shadow-val: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  --font-val: 'Montserrat', sans-serif;
  --radius-val: 0.75em;
  --hover-white: #F2F2F0;
}

body {
  margin: 5;
}


/*here*/

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: var(--white);
  box-shadow: var(--box-shadow-val);
  font-family: var(--font-val);
  border-radius: var(--radius-val);
}

li {
  float: left;
}

li a {
  display: block;
  color: var(--black);
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover {
  background-color: var(--hover-white);
}


/*img is not part of original code just there to show it dose not stick*/
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link href="style.css" rel="stylesheet">
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
  <title>Portfilo</title>
</head>

<body>
  <div class="navBar">
    <ul>
      <li><a href="#port" class="Port"><b>What Is Port</b></a></li>
      <li><a href="#priv" class="Priv"><b>Privacy</b></a></li>
      <li><a href="#reg" class="Reg"><b>Get Started</b></a></li>
      <li><a href="#guides" class="Guides"><b>Guides</b></a></li>
      <li><a href="#cred" class="Cred"><b>Credits</b></a></li>
    </ul>
  </div>
  <!-- img is not part of original code -->
  <img src="https://miro.medium.com/max/1400/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg">
</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

Unlock the power of Component level CSS in NextJS with this simple guide!

I don't have much experience with CSS, as I rarely write it these days due to the popularity of libraries like MUI. However, I recently found myself in a tricky situation where I needed to rewrite this example Emabla carousel in typescript and NextJS. ...

Can you explain the connection between CSS and WebKit?

Lately, the tag "webkit" has caught my eye on various questions. These inquiries typically pertain to web-related topics like CSS, jQuery, website layouts, and cross-browser compatibility problems... But what exactly is "webkit" and how does it interact w ...

Is there a way for me to retrieve the value from an input, round it up to the nearest even number, and assign it to a new variable?

I am working on a project that involves two text boxes and a drop-down menu for providing rates. The values in the text boxes are given as inches. I want these inputs to be taken as values, rounded up to the next even number, and then set as variables. How ...

Is there a way to align the Title in the Titlebar to the center?

Hi there! I could use some assistance :) I have an Electron app and I'm working on the title bar. I would like to center the title, but I'm not sure how to do it. Can anyone provide guidance on this? Also, since I am a beginner, I'm unsure i ...

Can someone tell me where I can locate the CSS file once I've finished using Scene Builder?

Currently, I am working on my database project using Scene Builder. I have applied some CSS styling and would like to locate the CSS file in order to attach it to my IntelliJ project for further code editing. Can anyone guide me on where to find the CSS ...

Providing CSS through PHP file on IIS Server

When attempting to output CSS using PHP in order to increase its dynamism with WordPress settings, I encountered a 500 internal server error. Despite setting the header to text/css and trying to add handlers in IIS, the issue persists. Interestingly, there ...

Angular show more button determined by line count, not letter count

I am looking for a solution in Angular or CSS that will automatically add a "read more" button if the text is longer than 5 lines. My Angular page currently limits text to 150 characters. {{post.post_text | limitTo:letterLimit}} However, some posts are ...

Unusual CSS hierarchy observed post AJAX content load

Currently, I am facing a puzzling issue where my CSS rules seem to be losing precedence on a page loaded via AJAX. Despite placing my custom CSS file last in the main page, allowing it to take precedence over any bootstrap styles, after loading new content ...

Having issues with jQuery Overlay functionality on Internet Explorer 8

Sorry if this question has already been addressed elsewhere, but I wasn't able to locate any information on it. I am currently using jQueryTools Overlay which is functioning properly in Chrome. However, I am encountering an issue in IE-8 where my ove ...

Using HTML to showcase various prompt messages based on the screen resolution

For the button click event, when clicked on desktop screens it will show a prompt message saying 'Hi'. On the other hand, when clicked on mobile screens, the prompt message displayed will be "Hello". ...

Center the font in the middle of the text field

I'm having a text field with a jQuery hint. How do I align the font to be in the middle? I've attempted using vertical-align: middle but it doesn't seem to work. <style type="text/css"> #name { border: 1px solid #c810ca; heig ...

Rule for restoring scroll position upon reloading web pages in browsers

When it comes to websites that handle data asynchronously, the behavior of scroll position restoration upon browser refresh can vary. Sometimes the scroll position is preserved, while other times it is not. For example, when reloading after scrolling down ...

What is the best way to set the screen height without needing to scroll vertically?

How can I make the orange and blue area extend to the end of the screen or have the full screen size minus the height of the navbar? When using `vh-100`, it fills the full height but creates a vertical scrollbar which is not desired. Is there a way in Boot ...

align video element to the right within a container div

I'm attempting to align a video element to the bottom right inside a div. Here is the code I have so far, which successfully aligns the video element to the bottom but not to the right side of the div container: video { } .border.rounded-0.border- ...

Hover and hover-off functions in navigation menu

html <div class="hidden-nav"> <h4>lorem</h4> <ul class="decor-menu-list"> <li><a href="#">123</a></li> <li><a href="#">123</a></li> <li><a hre ...

The integration between React hook form and reactstrap input components is not functioning properly

Having an issue with react-hook-form and reactstrap. The component List.jsx is causing trouble: import { useContext, useEffect } from "react"; import { ListContext, ADD_LIST } from '../providers/ShoppingListProvider'; import { Link } from "react- ...

Ensuring consistent width for two divs using CSS

https://i.stack.imgur.com/3n8La.png I am currently working on creating a responsive CSS layout for two div elements. The first div will contain the summary, while the second div will hold the detailed description. My challenge is to make sure that the seco ...

Creating a custom named page using PHP and MySQL

Currently, I am in the process of learning how to create my own database with the help of tutorials and resources like Stack Overflow. I have successfully created a register page where users can enter their username and password to access the upload page. ...

Show an HTML email message on a webpage without disrupting the existing page layout

Looking for a solution to display mail messages on a web page originating from an exchange server with various style elements that might interfere with the existing page layout. Attempting to load these mail messages within a div results in the style elem ...

Accessing and manipulating web elements using either XPath or CSS selectors

Having trouble selecting a specific piece of information using xpath or css selector. Error messages keep appearing. Can someone help identify the issue? This snippet is from my code: output = driver.find_element_by_xpath("//td[@class_= 'sku']" ...