<!-- Not working properly in Safari and ios doesn't like background-attachment fixed much -->
<div class="wrap">
<nav class="nav">
<ul>
<li><a href='#section1'>S<span>ection</span> 1</a></li>
<li><a href='#section2'>S<span>ection</span> 2</a></li>
<li><a href='#section3'>S<span>ection</span> 3</a></li>
<li><a href='#section4'>S<span>ection</span> 4</a></li>
<li><a href='#section5'>S<span>ection</span> 5</a></li>
<li><a href='#section6'>S<span>ection</span> 6</a></li>
<li><a href='#section7'>S<span>ection</span> 7</a></li>
<li><a href='#section8'>S<span>ection</span> 8</a></li>
</ul>
</nav>
<div class="main">
<h1>Automatic Scroll Menu Highlight</h1>
<section id="section1" class="section1">
<h2>Section 1</h2>
<p>Scroll the page up and down and the top menu will automatically highlight the current section. No JS was harmed in producing this function.</p>
<p>This demo is proof of concept only and requires on a few 'magic numbers' so is of limited use other than an exercise in what can be done with CSS alone.</p>
<p>Enjoy...</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
</section>
<section id="section2" class="section2">
<h2>Section 2</h2>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
</section>
<section id="section3" class="section3">
<h2>Section 3</h2>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
</section>
<section id="section4" class="section4">
<h2>Section 4</h2>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
</section>
<section id="section5" class="section5">
<h2>Section 5</h2>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
</section>
<section id="section6" class="section6">
<h2>Section 6</h2>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
</section>
<section id="section7" class="section7">
<h2>Section 7</h2>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
</section>
<section id="section8" class="section8">
<h2>Section 8</h2>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
<p>scroll</p>
</section>
</div>
</div>
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
html {
overflow: hidden;
}
body {
scroll-behavior: smooth;
overflow-y: scroll;
-webkit-overflow-scrolling: touch; /* Lets it scroll lazy */
height: 100vh;
}
html,
body {
margin: 0;
padding: 0;
}
h1 {
text-align: center;
margin: 0;
padding: 1rem 0;
background: palegoldenrod;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
.wrap {
position: relative;
background: #f9f9f9;
max-width: 1480px;
margin: auto;
}
.main {
background: #8000006e;
}
.main p {
padding: 0 10px;
margin: 0.5rem 0 1rem;
}
.main .item1 {
}
.nav {
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 9;
box-shadow: 0 0 10px 10px rgba(0, 0, 0, 0.3);
background: rgba(0, 0, 0, 0.1);
background: #fff;
font-weight: bold;
font-size: 1.2rem;
}
.nav ul {
display: flex;
}
.nav li {
flex: 1 0 0;
}
.nav a {
padding: 0 10px;
text-decoration: none;
border: 1px solid #000;
display: flex;
height: 2rem; /* magic number alert*/
align-items: center;
justify-content: center;
color: #000;
white-space: nowrap;
}
.nav a:hover {
background: rgba(240, 15, 0, 0.5);
}
.wrap section h2 {
margin: -2rem 0 0;
position: -webkit-sticky;
position: sticky;
top: 2rem;
background: red;
color: #fff;
padding: 5px 10px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}
.wrap section:before {
content: "\2192";
display: block;
position: -webkit-sticky;
position: sticky;
top: 1px;
height: 2rem; /* magic number alert*/
line-height: 2rem;
background: linear-gradient(to left, red, red);
background-repeat: no-repeat;
background-attachment: fixed;
background-size: 100% 2rem;
margin: 0;
color: transparent;
z-index: 999;
opacity: 0.5;
width: calc(100% / 8);
}
/* Safari 9+ hack as it won't draw background all the time */
@supports (-webkit-hyphens: none) {
.wrap .main section:before {
color: red;
background: transparent;
opacity: 1;
font-weight: bold;
font-size: 1.2rem;
}
.wrap section h2 {
padding-left: 1.2rem;
}
}
.wrap .section2:before {
transform: translateX(calc(100% + 1px));
}
/* magic number alerts below */
.wrap .section3:before {
transform: translateX(calc(200% + 1px));
}
.wrap .section4:before {
transform: translateX(calc(300% + 1px));
}
.wrap .section5:before {
transform: translateX(calc(400% + 1px));
}
.wrap .section6:before {
transform: translateX(calc(500% + 1px));
}
.wrap .section7:before {
transform: translateX(calc(600% + 1px));
}
.wrap .section8:before {
transform: translateX(calc(700% + 1px));
}
@media screen and (max-width: 880px) {
.nav {
font-size: 1rem;
}
}
@media screen and (max-width: 660px) {
.nav span {
display: none;
}
}
@media screen and (max-width: 365px) {
.nav {
font-size: 0.9rem;
font-weight: normal;
}
}
//nope
//side menu version here : https://codepen.io/paulobrien/pen/xxXrLVE
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.