Position:Sticky Is Not Working

How does the position: sticky; property work?

Sticky positioning is a hybrid of relative and fixed positioning. The element is treated as relative positioned until it crosses a specified threshold, at which point it is treated as fixed positioned.

...

You must specify a threshold with at least one of top, right, bottom, or left for sticky positioning to behave as expected. Otherwise, it will be indistinguishable from relative positioning.
[source: MDN]

So in your example, you have to define the position where it should stick in the end by using the top property.

html, body {
height: 200%;
}

nav {
position: sticky;
position: -webkit-sticky;
top: 0; /* required */
}

.nav-selections {
text-transform: uppercase;
letter-spacing: 5px;
font: 18px "lato", sans-serif;
display: inline-block;
text-decoration: none;
color: white;
padding: 18px;
float: right;
margin-left: 50px;
transition: 1.5s;
}

.nav-selections:hover {
transition: 1.5s;
color: black;
}

ul {
background-color: #B79b58;
overflow: auto;
}

li {
list-style-type: none;
}
<nav>
<ul align="left">
<li><a href="#/contact" class="nav-selections" style="margin-right:35px;">Contact</a></li>
<li><a href="#/about" class="nav-selections">About</a></li>
<li><a href="#/products" class="nav-selections">Products</a></li>
<li><a href="#" class="nav-selections">Home</a></li>
</ul>
</nav>

Why position sticky is not working in my code?

Its because you have overflow-x-hidden in parent . sticky doesn't work if parent overflow is hidden.

{!categorySearch && (
<div className="max-w-2xl mx-auto z-40 relative font-metropolis_semibold">
<div className="sticky top-0">
<div className=" z-40 bg-white w-full transition-all">
<div
id="logo"
className={
logo
? "px-md mt-10 font-medium"
: "px-md mt-10 font-medium animate"
}
>
<div className="overflow-x-hidden ">
<img
src={settings.logo_intro_image_url}
alt={settings.name}
className="max-w-full object-contain company-logo"
style={{ height: settings.logo_height }}
id="company-logo"
/>
</div>
</div>
<div
className={`${
logo
? "shadow-none border-none"
: "border-b-2 border-gray-200"
}`}
>
<TopOptions
showOption={showOption}
setShowOption={setShowOption}
addBorder={logo}
outlets={outlets}
languageLabels={languageLabels}
settings={selectedOutlet.settings}
/>
</div>
</div>
{Object.keys(bannerArray).length === 1 ? (
<div className="w-full px-md border-b-8 border-banner pb-md">
{bannerArray &&
bannerArray.map((banner, index) => {
return (
<>
<PosterSlider
key={index}
image={banner.image}
loadingBanners={loadingBanners}
isSingle={Object.keys(bannerArray).length === 1}
/>
</>
);
})}
</div>
) : (
<div className="max-h-full h-19.7 hide-scroll px-md flex overflow-x-auto gap-5p border-b-8 border-light-bg-gray">
{bannerArray &&
bannerArray.map((banner, index) => {
return (
<PosterSlider
key={index}
image={banner.image}
loadingBanners={loadingBanners}
isSingle={Object.keys(bannerArray).length === 1}
/>
);
})}
</div>
)}
<div className="flex flex-row justify-between items-center px-md my-25p">
<p className="font-metropolis_regular text-sm break-words">
{languageLabels.like_to_order}
</p>
<img
src={Search}
alt="search"
onClick={() => setCategorySearch(true)}
/>
</div>
<div className="grid grid-flow-row w-full grid-cols-2 place-items-center gap-10p px-md pb-20">
{categoryList.map((category) => {
// console.log(category);
return (
<Dishes
key={category.category_id}
category={category}
languageLabels={languageLabels}
loading={loadingCategories}
/>
);
})}
</div>
<BottomNavigation
languageLabels={languageLabels}
isLoyalty={settings.enable_reward_points_functionality}
/>
</div>
</div>
)}

Position sticky not taking effect

The sticky element in your fiddle has no height setting - use a set height to avoid that, then the sticky position works:

https://jsfiddle.net/rocz5nL1/

position:sticky is not working

It works fine if you move the navbar outside the header. See below. For the reason, according to MDN:

The element is positioned according to the normal flow of the document, and then offset relative to its flow root and containing block based on the values of top, right, bottom, and left.

For the containing block:

The containing block is the ancestor to which the element is relatively positioned

So, when I do not misunderstand, the navbar is positioned at offset 0 within the header as soon as it is scrolled outside the viewport (which, clearly, means, you can't see it anymore).

.navbar {
background: hotpink;
width: 100%;
height: 50px;
position: sticky;
top: 0;
}

.header {
height: 150px;
background: grey;
}

body {
height: 800px;
position: relative;
}
<div class="header">
<div class="desc">Description</div>
<div class="logo"><img src="" /></div>
</div>

<div class="navbar"></div>

Why is my position:sticky not working?

When you place a position:sticky element inside another element things can get tricky. The sticky element will only travel the height of the parent element so you need to have space in your parent element for the sticky element to move because position: sticky is scoped to the parent element and not the page. The parents overflow and display property can also have an effect. You can try to set the display property of your parent elements to display: intital.

try adding the following:

.center, header{
display:initial;
}

You can probably set it to inline or inline-block as well depending on your needs.

Here is your snippet with that added along with a couple of other things just for display purposes:

body{
height:200vh;
}

.center, header{
display:initial;
}

.dropmenu {
display: block;
height: 65px;
width: 100%;
background: url("images/menuicon.png") no-repeat 98% center;
background-color: #404040;
cursor: pointer;
}

nav ul {
padding: 0;
overflow: hidden;
background: #505050;
display: none;
}

nav ul li {
display: block;
float: none;
text-align: left;
width: 100%;
margin: 0;
}

nav ul li a {
color: white;
padding: 10px;
border-bottom: 1px solid #404040;
display: block;
margin: 0;
}

div.sticky {
position: sticky;
top: 0;
}
<div style="height:100px; background:green;"></div>

<div class="center">
<header>
<img class="headerImage" src="images/header.png"/>
<hr class="menu">
<div class="sticky">
<a class="dropmenu"></a>
<nav class="desktop">
<ul>
<li><a href="index.php">Sertet</a></li>
<li><a href="index.php">Rtretrti</a></li>
<li><a href="photos.php">ertettterli</a></li>
<li><a href="index.php">retemi</a></li>
<li><a href="index.php">Kerterti</a></li>
</ul>
</nav>
</div>
</header>
<hr class="menu">
<p>content goes here</p>
</div>

Why is 'position: sticky' not working with Core UI's Bootstrap CSS

The issue is the use of overflow inside .app-body. It's a bit tricky but there should be no overflow property set to any element between the element that has the scroll and the sticky element.

Here is a basic example to illustrate. The scroll is on the viewport and we have a wrapper with overflow:hidden (or even auto) thus the sticky behavior won't work.

.container {  display:flex;  align-items:flex-start;  border:2px solid green;}.content {  flex:1;  height:200vh;  background:red;  margin:10px;}.sticky {  flex:1;  height:100px;  background:blue;  margin:10px;  position:sticky;  top:0;}
.wrapper { overflow:hidden; border:2px solid red;}
<div class="wrapper">  <div class="container">    <div class="sticky"></div>    <div class="content"></div>  </div></div>

Why position:sticky is not working when the element is wrapped inside another one?

position:sticky consider the parent element to behave as it should be. In your case the parent element has its height defined by the sticky element so there is no room for the element to have a sticky behavior

Add border to better see the issue:

.nav-wrapper {
position: absolute;
bottom: 0;
border: 2px solid;
}

.nav-wrapper nav {
position: sticky;
top: 0;
}

body {
min-height:200vh;
}
<div class="nav-wrapper">
<nav>
<a href="#">
<li>Home</li>
</a>
<a href="#">
<li>About</li>
</a>
</nav>
</div>

Css Sticky position not working after apply top:0;

Your sticky item is correct. The problem is because your content has nothing, therefore the sticky content is scrolled along with your content.

* {        box-sizing: border-box;    }        body {        margin: 0;        font: 20px Candara, sans-serif;        background-color: whitesmoke;        height: 2000px;    }            /* #homepage {        position: relative;        top: 0;    } */        .header {        text-align: center;        /* position: absolute; */        /* top: 0px; */        color: snow;        padding: 1px;        width: auto;        /* margin-top: 00px; */        background-color: red;        /* background-color: whitesmoke; */    }        .navigation-panel {        display: block;        background-color: snow;        position: sticky;        /* position: -webkit-sticky; */        /* overflow: hidden; */        top: 0;        /* height: 55px; */        border: 1px solid snow;    }        .navigation-panel a {        text-decoration: none;        color: red;        float: left;        padding: 14px;    }        .navigation-panel a:hover {        color: snow;        background-color: brown;        /* transition: 0.5s; */        /* border: 1px solid snow; */    }        #content-body {        height: 1000px;        background-color: gold;    }
 <!DOCTYPE html>    <html>        <head>        <meta charset='utf-8'>        <meta http-equiv='X-UA-Compatible' content='IE=edge'>        <title>Sample Codings</title>        <meta name='viewport' content='width=device-width, initial-scale=1'>        <link rel='stylesheet' type='text/css' media='screen' href='main.css'>        <!-- <script src='main.js'></script> -->    </head>        <body>        <div id="homepage">            <div class="header">                <h1>Sample Codings</h1>            </div>                <div class="navigation-panel">                    <a href="">Home</a>                <a href="">checking</a>                <a href="">Java</a>                <a href="">Spring</a>                <a href="">ThreeJs</a>                </div>            <div id="content-body"></div>        </div>    </body>        </html>

position: sticky not working after some amount of scroll

One way to solve this:

.slider {
overflow: auto;
display: grid; /* main element as grid */
grid-auto-columns: 20%; /* width of the items */
}
.header,
.body {
display: contents; /* remove the parent boundary to make sticky works */
}
.header > * {
grid-row: 1; /* all header elements in first row */
}
.body > * {
grid-row: 2; /* all body elements in second row */
}
.item {
height: 100px;
background: yellow;
border: 1px solid black;
}

.sticky-item-left {
position: sticky;
left: 0;
background: red;
}

.sticky-item-right {
position: sticky;
right: 0;
background: green;
}
<div class="slider">
<div class="header">
<div class="item sticky-item-left"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item sticky-item-right"></div>
</div>
<div class="body">
<div class="item sticky-item-left"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item sticky-item-right"></div>
</div>
</div>


Related Topics



Leave a reply



Submit