DEV Community

100% Pure HTML/CSS Page Navigation - No JavaScript Required

Morten Olsrud on October 13, 2018

Build a page navigation system using only HTML and CSS This is a little trick I've acquired over the years, and for most use cases it wo...
Collapse
 
paulbrowne profile image
paulbrowne • • Edited

Slightly simpler version, making use of the :not() and :target
This allows for the use of a non-hash link for the homepage.
And no need for the display:block

<style>
    section:not(:target):not(#home),
    section:target ~ #home {
        display: none
    }
</style>
<nav>
    <a href="/">home</a>
    <a href="#contact">contact</a>
    <a href="#about">about</a>
    <a href="#blog">blog</a>
</nav>
<main>
    <section id="contact">contact section...</section>
    <section id="about">about section...</section>
    <section id="blog">blog section...</section>
    <section id="home">home section...</section>
</main>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
cscarlson profile image
cScarlson •

Love to hear about pushing the needle with pure HTML and CSS!

It's probably good timing for me to mention you should look into RMDs to help organize the workflow. Have a quick look!...

https://tbt.hashnode.dev/pure-native-micro-frontends?utm_source=hashnode&utm_medium=feed

I can show you how to implement the "CDC" pattern for your CSS design system.

It's all 100% native HTML, CSS, and [minimal] JavaScript [required]. Trust me, you won't regret reading that article.

Collapse
 
remostein profile image
Omer Stein •

Thanks for the hack! I learned a lot from it!
One question though, from some reason every time I press a href on a page (that is not the default, lets call it page2) directing to somewhere on page2, it suddenly displays the default page.
Is there a chance that when I press another link, that is not a page, the :target{ display:block;} is activated anyways?

Collapse
 
hakash profile image
Morten Olsrud •

Sorry for the late comeback.

If the other links you are using internally on the page causes the hash in the URL to change, then yes, what you are describing is correct and the expected behaviour. This trick relies on the hash in the URL, as that is what triggers the CSS :target selector.

Collapse
 
equinusocio profile image
Mattia Astorino •

Another big caveat is the accessibility. We should use aria-attributes, but at this point we are already using js.

Collapse
 
hakash profile image
Morten Olsrud •

Yeah, I'd love to do that, but I havent found a way for CSS selectors to work on aria. I'm also unsure how a screen-reader or braille-board would react to most SPAs to be honest.

Collapse
 
equinusocio profile image
Mattia Astorino •

You should probably use the attribute selector [aria-active]{} but as i said, you need js.

Thread Thread
 
hakash profile image
Morten Olsrud •

Regardless of JS, that selector was new to me 😊

Collapse
 
joeberetta profile image
Joe Beretta •

Good lifehack. thanx for idea)

Collapse
 
niloy_barmon_2b6c35811a34 profile image
Niloy Barmon •

Hi is there any way to show active status of menu items when navigation?

Collapse
 
jevanantham profile image
Ananth M •

I want to scroll ,change next page and navbar menu could be active