<header class="hero">
<div class="container">
<h1>CSS Position</h1>
<ul class="navbar">
<li><a href="#">static</a></li>
<li><a href="#">relative</a></li>
<li><a href="#">absolute</a></li>
<li><a href="#">fixed</a></li>
<li><a href="#" class="current-link">sticky</a></li>
</ul>
</div>
</header>
<main class="main container">
<section class="section position-sticky">
<h2><code>position: sticky;</code></h2>
<div class="sticky">This element has <code>position: sticky;</code></div>
<p class="separator">the above example, the header and the text with blue background and class <code>fixed</code> has<br /><code>position: sticky;</code> <br /></p>
<p>The header's parent is the document, while the fixed div's parent is the main (with skyblue background)</p>
<strong><em>
<p>The text acts as a relative positioned element until it's away 5px away from top. As the main element is scrolled, it acts as a fixed element</p>
</em></strong>
<strong><em>
<p>Similarly, the header text acts as a relative positioned element until it's away 0px away from top. As the page is scrolled it acts as a fixed element</p>
</em></strong>
<p>
<strong style="color: black; display: inline-block; margin-top: 0.1rem; padding: 0.5rem 1rem; background-color:yellowgreen;">
<code>
header.hero {
position: sticky;
top: 5px;
}
<br />
<br />
main.main {
position: relative;
}
<br />
<br />
div.sticky {
position: sticky;
top: 5px;
}
</code>
</strong>
</p>
</section>
</main>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
height: 250vh;
}
.container {
margin: auto;
max-width: 900px;
padding: 1rem;
}
a,
a:visited {
text-decoration: none;
}
a.current-link {
background-color: navy;
}
header.hero {
background-color: royalblue;
position: sticky;
top: 0;
box-shadow: 0px 0px 10px #000;
font-size: 1.2rem;
font-family: "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;
text-transform: uppercase;
}
.hero .container {
display: flex;
justify-content: space-between;
align-items: center;
}
.hero h1 {
font-size: 1.2rem;
letter-spacing: 0.3rem;
padding: 0.5rem;
}
.hero ul {
list-style: none;
}
.hero ul li {
display: inline-block;
margin-right: 1rem;
}
.hero ul li a {
color: #fff;
padding: 0.5rem 0.75rem;
border-radius: 0.5rem;
}
.main {
height: 100vh;
position: relative;
}
.separator {
text-align: center;
margin: 1rem 0;
font-size: 1.1rem;
}
.section {
padding: 1rem;
margin: 2rem auto;
max-width: 400px;
background-color: lightskyblue;
/* text-align: center; */
color: white;
max-width: 100%;
height: 100%;
}
.section h2 {
font-size: 1.3rem;
background-color: darkgreen;
padding: 1rem;
}
.section p {
font-size: 1.1rem;
background-color: darkgreen;
padding: 1rem;
}
.section .sticky {
background: navy;
padding: 1rem;
position: sticky;
top: 5px;
font-size: 1.3rem;
}
@media screen and (max-width: 800px) {
.hero .container {
flex-direction: column;
justify-content: center;
}
.hero h1 {
margin-bottom: 1rem;
}
}
@media screen and (max-width: 600px) {
.hero h1 {
margin-bottom: 0;
}
.hero ul {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.hero ul li {
padding: 0.25 0;
margin: 1.5rem 0 0 0;
font-size: 1rem;
}
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.