brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: :target in CSS highlights the section a link just jumped to

Css
~/tips/tip-css-target-highlight-anchor

.footnotes li:target,

After clicking a footnote or heading link, the reader sees which part the link meant.

Clicking a footnote link scrolls to the note, but when several notes fit on the screen it is not obvious which one you landed on. :target matches the element whose id is in the URL fragment.

.footnotes li:target,
h2:target {
  background: #fff3bf;
  animation: fade-mark 2s ease-out 1s forwards;
}
@keyframes fade-mark {
  to { background: transparent; }
}

When the URL ends in #fn-3, the element with id="fn-3" matches :target. I give it a yellow background that fades out after a second, so the reader sees where they are without a permanent marker. It also works when someone opens a shared link with a fragment, which makes it nice for headings with anchor links.

No JavaScript is needed and it has worked in browsers forever.

A few things to know. Only one element can be the target at a time. Changing the hash with history.pushState() does not update :target, while setting location.hash does. And the highlight comes back every time the page is reloaded with the fragment still in the URL, which is mostly fine but can look odd on a page people refresh a lot.

Further reading: MDN: :target

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link