<nav>
<ul>
<li><a href=#>Home</a></li>
<li><a href=#>Blog</a></li>
<li><a href=#>Experiments</a></li>
<li><a href=#>About</a></li>
</ul>
</nav>
/* The border-bottom slider here */
ul::before {
content: "";
position: absolute;
left: anchor(var(--target) left);
right: anchor(var(--target) right);
bottom: anchor(var(--target) bottom);
transition: all 0.3s;
border-bottom: 0.3rem solid hsl(from var(--color) h s calc(l * 0.8));
pointer-events: none;
}
ul:has(a:not(:hover, :focus-visible))::before) {
visibility: hidden;
opacity: 0;
transition: all 0.3s;
}
a {
anchor-name: var(--anchor);
font-size: 1.2rem;
font-weight: 600;
}
li:nth-child(1) a {
color: red;
}
li:nth-child(2) a {
color: orange;
}
li:nth-child(3) a {
color: green;
}
li:nth-child(4) a {
color: blue;
}
li:nth-child(1) { --anchor: --item-1 }
ul:has(:nth-child(1) a:is(:hover, :focus-visible)) {
--target: --item-1;
--color: red;
}
li:nth-child(2) { --anchor: --item-2 }
ul:has(:nth-child(2) a:is(:hover, :focus-visible)) {
--target: --item-2;
--color: orange;
}
li:nth-child(3) { --anchor: --item-3 }
ul:has(:nth-child(3) a:is(:hover, :focus-visible)) {
--target: --item-3;
--color: green;
}
li:nth-child(4) { --anchor: --item-4 }
ul:has(:nth-child(4) a:is(:hover, :focus-visible)) {
--target: --item-4;
--color: blue;
}
a:focus-visible {
outline: none;
}
/* Base UI */
body {
font-family: system-ui;
}
nav {
background: aliceblue;
padding: 1rem;
}
ul {
display: flex;
justify-content: end;
anchor-name: --slider-menu;
}
a {
padding: 1rem;
text-decoration: none;
}
// Inspired by a demo by Roman Komarov https://kizu.dev/anchor-positioning-experiments
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.