Is it possible to highlight the link the the current :target, using only HMTL and CSS?
:target {
border: 2px solid red;
}
<nav>
<a href="#foo">Foo</a>
<a href="#bar">Bar</a>
</nav>
<section id="foo">This is foo.</section>
<section id="bar">This is bar.</section>
The example highlights the targeted element, but the goal is, to add styling to the link, which has been clicked to get to this target.
Edit: The links will not lead to different HTML pages, only to targets within the current page. This is used as a single-page website, so the URL won’t change, but the "#foo" part at the end.
Edit 2: :local-link
seems to be designed to do exactly what is desired, but it’s only a proposal and not supported by any browser. So the question is, if there is a workaround using only supported CSS.
3
Answers
This is a working solution, but the links have to be known and specified in the CSS:
Once a user clicks on the
#foo
link, the body has an element with ID#foo
which also has been targeted, so the selector works and you can access child elements of this body.check this https://www.w3schools.com/howto/howto_js_tabs.asp
you will need to add a similar function to your code
Here is a working implementation: