How can the target pseudo-class be utilized to replace the 'active' state on navigation across several pages effectively?

Within a large application, I am managing a micro site and looking to implement tertiary navigation with an active state for pages. This involves inserting a single html blob into multiple pages.

My goal is to use an active class to highlight the page in the tertiary navigation that the user is currently on.

<nav>
   <ul>
   <li class="active"><a href="#">Page 1</a></li>
   <li><a href="#">Page 2</a></li>
   <li><a href="#">Page 3</a></li>
   </ul>
</nav>

Since I lack control over the backend application but have full control of html, css, I aim to avoid solutions involving jQuery to detect urls.

The CSS approach would be:

nav li:target {background:#ccc}

Instead of:

nav li.active {background:#ccc}

I have observed similar concepts applied to anchor tags like this. The inquiry is whether it's possible to achieve this for different urls and how to properly utilize the :target class.

Based on feedback, this is my current implementation:

<nav>
   <ul>
   <li><a id="page1" href="page1.html">Page 1</a></li>
   <li><a id="page2" href="page2.html">Page 2</a></li>
   <li><a id="page3" href="page3.html">Page 3</a></li>
</ul>
</nav>

However, this method does not work consistently across multiple pages. For a functioning project example, refer to this GitHub repository.

Answer №1

Your approach to using the target class seems to be reversed based on the example you provided earlier. Currently, you are requesting the target class to highlight the selected link, but in reality, the target pseudo class highlights the content section associated with the selected link. You can see this concept in action on the w3schools site demo here. The content changes when the link is clicked, not the other way around.

If you want your navigation links to be highlighted based on the selection, you will need to dynamically add the 'active' class, most likely using jQuery. I hope this explanation helps clarify things for you.

UPDATE

After our discussion, I reviewed your code and made some improvements.

<!DOCTYPE html>
<html>
<style>
.content:target  {
   background: #ccc;
}
</style>
<body>

<nav>
  <ul>
    <li><a href="page1.html#page1">Page 1</a></li>
    <li><a href="page2.html#page2">Page 2</a></li>
    <li><a href="page3.html#page3">Page 3</a></li>
  </ul>
</nav>

<h1>This is Page one</h1>

<div class="content" name="page1" id="page1">This is Page ONE paragraph</div>

</body>
</html>

You were assigning an ID to the anchor tags which was unnecessary since the nav links don't require an ID. Instead, the ID value should be passed in the URL as #page1. Additionally, your target CSS refers to the content section, not the navigation menu. Follow this setup for the other pages for optimal functionality.

Answer №2

Refer to Section 6.6.2 on The target pseudo-class :target

Certain URIs point to specific locations within a resource, indicated by the presence of a "number sign" (#) followed by an anchor identifier (also known as the fragment identifier).

URIs containing fragment identifiers direct to a particular element within the document, referred to as the target element.

The target element can be selected using the :target pseudo-class. In cases where the URI lacks a fragment identifier, there is no designated target element in the document.

To apply styling based on this concept, you can use:

 nav li:target {
   background: #ccc
 }
<nav>
  <ul>
    <li id="page1"><a href="#page1">Page 1</a></li>
    <li id="page2"><a href="#page2">Page 2</a></li>
    <li id="page3"><a href="#page3">Page 3</a></li>
  </ul>
</nav>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying text and concealing image when hovering over a column in an angular2 table

I am currently using a table to showcase a series of items for my data. Each data entry includes an action column with images. I would like to implement a feature where hovering over an image hides the image and reveals text, and vice versa (showing the im ...

What is the process for connecting CSS to an HTML page?

In my project, I have an index.html file and a style.css file. Currently, I am using inline CSS in my HTML file but now I want to utilize an external stylesheet. Inside my index.html file, just before the closing </head> tag, I have added the followi ...

"Create a dynamic and user-friendly responsive navbar using Bootstrap with toggle

I need assistance with creating a new project template to convert a WordPress template into a Bootstrap template. Currently, I am working on the navbar and facing issues with responsive design and toggle navigation. On laptop devices, the navbar looks goo ...

Is there a way for me to utilize the Bootstrap carousel without it adjusting the height as the user transitions from slide 1 to slide 2?

body{ background-color: #CAF7E3; } .container-fluid{ padding: 3% 15%; } .navbar{ font-family: 'Montserrat'; font-weight: bold; } .navbar-brand{ font-size: 2.5rem; } .nav-item{ margin-right: 2rem; } #title{ padding: 8% 15%; backgr ...

Tips for creating a "sticky" button in Angular that reveals a menu when clicked

Is there a way to incorporate a feature similar to the "Get help" button on this website: The button, located in the lower right corner, opens a sticky chat menu. I am interested in adding dynamic information to the button (such as the number of tasks a u ...

Conceal specific pages within the DataTable without deleting them

Currently, I am facing an issue where the dataTable paginates and removes the DOM pages along with the data. My goal is to extract all the data from the dataTable and convert it to JSON without losing access to the DOM when pagination occurs. I want to m ...

Creating Scalable Vector Graphics without utilizing identifiers

For instance, if we have defs and rect defined separately, we would typically use an ID. <defs> <linearGradient id="MyGradient"> <stop offset="0%" stop-color="#000" /> <stop offset="100%" stop-color="#fff" /> ...

React: Submit button not triggering form submission despite having a valid submit event handler and correct syntax

My question stands out from existing ones as it features valid syntax, correct use of type="submit" on the button, and a simple onSubmit handler on the form. Despite this, clicking the button fails to trigger the onSubmit handler. To illustrate ...

Is there a way to create a multi-page website simulation using jQuery?

My current project involves creating a single page website, but I am looking to give the illusion of a multi-page site by using CSS/jQuery to hide and reveal different sections when specific links in the navigation menu are clicked. Here is the code snipp ...

Enhance VideoJS using custom Ionic icons in a NextJS environment

I'm creating a platform for video content and I want to enhance the VideoJS player by incorporating icons from the Ionic set. However, as I typically use the <ion-icon/> tag to insert icons on my pages, I had to individually specify each icon&ap ...

Utilizing multiple class names in Material UI with identical definitions

When working with traditional CSS, it is common to define shared properties between classes like this: .classA,.classB{ background-color: black; } In Material UI and using theming, the equivalent would be: styles = (theme)=>({ classA:{ ba ...

Top strategies for efficiently managing the loading of extensive PHP pages using Jquery, Ajax, HTML, and other tools

Hey there, I hope you're doing well in this new year. I've been working on a project that creates a "league table" using a large amount of data, similar to those seen in sports like football. The backend is built in PHP to process the data and d ...

When using HTML select, text alignment may not be centered properly in the Chrome

Recently, I encountered an issue where the text in select options is not centered in Chrome. Interestingly, using text-align: center on the select element works perfectly fine on Firefox, but fails to do so on Chrome. I haven't had the chance to test ...

Can markers be positioned on top of scroll bars?

Looking for a way to display small markers on the scrollbar of an overflow: scroll element, similar to features found in IDEs and text editors like this one: https://github.com/surdu/scroll-marker. I've considered using a pointer-events: none overlay ...

One effective way to redirect after a PUT request and display a one-time message

Here's what I'm aiming for in terms of desired behaviour: A user navigates to a password change web page. The user completes the form and it is sent via PUT request to our REST server. Upon successful completion, the user is redirected to their ...

Switch up the Thumbnail Image based on the selected category

While testing a shopping site I created, I encountered an issue with changing the banners at the top of the page based on the category the user is viewing. This site is hosted on a server on my localhost, not using wordpress by the way. <div class=" ...

How can I dynamically update a div without refreshing the page by using an onclick event on a

When working on my project, I found helpful insights from this Stack Overflow thread. The structure of template-comparison.php involves fetching code from header.php to ensure the page displays properly. However, the actual "fetch code" process remains un ...

Tips on aligning text to the left on mobile devices using CSS and HTML

When viewed on a standard 16:9 computer screen, everything looks perfect with text on the left and an image on the right. However, when it comes to smaller screens like phones, the picture appears on top of the paragraph instead. I am new to this, so any a ...

Vue 3 has officially deprecated the use of ::v-deep as a combinator. Going forward, please utilize ::v-deep(<inner-selector>) instead

Recently, an error message has been popping up in Vue 3 regarding the use of ::v-deep. The warning states that using ::v-deep as a combinator is deprecated. Instead, it recommends using ::v-deep(<inner-selector>) Here is an example of the CSS ...

Within the flask framework, I am experiencing an issue where paragraphs are being overwritten. My goal is to find a

Snippet of HTML: <div class="split left" > <div class="centered"> <div class="container"> <p>Hi!</p> </div> <div class="darker"> <p>{{message}}& ...