I want to use sticky position for an element that is inside for example header element, but stay on top of the page when I scroll down. Now I just can stick it to the top of my header, not the whole page. For example something like the header of this page you are. I can use header selector in the css file instead of nav, but in this case the image also sticks to the top that I don't want.
nav {
position: sticky;
position: -webkit-sticky;
top: 0px;
}
ul {
list-style-type: none;
padding: 0px;
margin: 0px;
overflow: auto;
background-color: #333;
}
li {
float: left;
border-right: thin solid rgba(255, 255, 255, 0.2);
}
li:last-child {
border-right: none;
border-left: thin solid rgba(255, 255, 255, 0.2);
float: right;
}
a {
display: block;
color: white;
text-decoration: none;
padding: 10px;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header style="height: 500px;">
<img src="https://html.com/wp-content/uploads/html-hpg-sublime.png" alt="image" style="width: 100%; height: 300px;">
<nav>
<ul>
<li><a class="active" href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
</nav>
</header>
<article style="height: 1000px">
some article here...
</article>
</body>
</html>