Exploiting the :target selector to customize numerous elements

My objectives:

  • Create a single HTML document that consolidates all my content for easier copying.
  • Incorporate multiple "pages" within the document to highlight different data sections.
  • Ensure seamless functionality of the browser's back button and support deep-linking capabilities.

An effective method is utilizing the :target pseudo-class to display only the relevant page (

div {display: none} :target {display: block}
).

To enhance this approach, I envision:

  • Integrating a navigation bar that indicates the current page.
  • Implementing a main page display when there is no fragment identifier in the URL (#).

I have devised a solution using empty divs and sibling selectors. The target element (ID provided in the URL fragment) is denoted as an empty div called target, and CSS rules are applied accordingly (

#target1 ~ #page1 { ... } #target1 ~  #link1 { ... }
). For a demonstration, refer to this Fiddle; note the smooth functioning of browser buttons after navigating through the nav bar links (deep-linking may not be fully demonstrable on jsfiddle).

Despite its effectiveness, this solution presents three challenges:

  • Each page requires a separate selector in the CSS, complicating maintenance with numerous pages.
  • The back button operates smoothly on Chrome and Firefox, but Edge may not support this technique completely (other browsers untested). Speculation arises due to ambiguities in regarding :target behavior with the back button (refer also to this Webkit bug).
  • A sense of abusing the system lingers as indicated by the question title.

Hence, I am inquiring if there exists a more efficient methodology for achieving my goals. Preferably without relying on JavaScript to maintain clear deep-linking functionality.

Answer №1

After @Seika85 shared a link to Navigo, a Javascript router that appears to be designed for this specific purpose in a comment, I decided to update the JSFiddle to utilize it instead. This change resolved all the issues I previously mentioned, albeit requiring a small amount of additional Javascript code. Much appreciated!

new Navigo(null, false) 
.on(/target(.)/, function(x) {
  $('.active').removeClass('active');
  $('#page'+x).addClass('active');
  $('#link'+x).addClass('active');
})

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

Grid System - having difficulty with aligning three columns side by side

I've gone through the documentation and searched online, but I can't seem to find a solution. I'm working with a parallax bootstrap slide where I want to display three div columns of equal height and width side by side in a single row on the ...

Adjust the appearance of a glyphicon by modifying its color according to various criteria

Using angularjs, I implemented ng-repeat to display details in a table. In the final column, I aim to display the glyphicon 'glyphicon glyphicon-stop' in a specific color. <tr ng-repeat="ps in $ctrl.personLst"> <td>{{ ps.id}}</td& ...

NgTemplate Bootstrap is not recognizing my CSS class

I made a CSS adjustment to alter the modal width to 50% instead of 100% of the screen. However, the class modal-content is being duplicated after the modification. https://i.sstatic.net/VZxM6.png https://i.sstatic.net/ahLkn.png CSS .modal-content{ ...

Troubleshooting problems with scrolling on JavaScript in Edge, Opera, and Chrome

Currently, I am developing a fun CSGO-inspired box opening feature on my food recipe website that randomly selects recipes for users. However, I have encountered some challenges with the scrolling functionality. It seems to be incompatible with certain bro ...

Implementing full-width responsive background images with srcset and lazy loading for optimal performance

I'm currently developing a website with a striking layout featuring 'hero' panels that cascade down the home page, showcasing text overlaid on captivating background images. My goal is to utilize inline images with srcset and sizes attribut ...

How to transform the bootstrap 4 hamburger icon into a three dots menu icon using css only

The standard bootstrap 4 navigation bar button showcases a classic hamburger icon. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample03" aria-controls="navbarsExample03" aria-expanded="false" aria-label="T ...

Disarray in the form elements

I'm having trouble positioning my form elements as desired. I want the recurrence_interval input field to be below the select (recurrence) input field. My two radio buttons for 'never' and 'on' should be stacked on top of each ot ...

tips for adding multiple elements in a row using bootstrap 4

I recently started using bootstrap-4 to design a single page in HTML. However, I am encountering some challenges in making it responsive for various devices like Laptop-L, Laptop-M, and iPad. Currently, the design is only working properly for Laptop-L but ...

What is the best method for designing a button with both inner and outer shadow effects?

Trying to replicate the CSS effect button exactly as shown in the image provided below (ignoring icons), but struggling to achieve the same results. Is there a way to create an identical CSS button? Below is the code for my button: .my-bt{ display:bloc ...

What is the best way to apply the !important property in jQuery for CSS styling?

When setting the CSS property "padding-top" to a value stored in a variable (currentHeight), such as: jQuery("#page-container").css("padding-top", currentHeight); It functions correctly and produces: <div id="page-container" style="padding-top: 115px;" ...

Struggling to show labels in the correct position for each button?

I need help with aligning labels under buttons. I have 3 buttons, each with a corresponding label. I want the labels to be displayed in line under the buttons, and if the length of a label exceeds the button width, it should wrap to the next line. I'v ...

What is the best way to animate specific table data within a table row using ng-animate?

Are you working with Angular's ng-repeat to display a table with multiple rows? Do you wish to add an animation effect to specific cells when a user hovers over a row in the table? In the scenario outlined below, the goal is to have only certain cell ...

Designing a webpage compatible across different browsers that features an image positioned relatively over two rows of a table

I am working on creating a unique table layout for a web page that resembles the design linked below: Click here to view the image http://kelostrada.pl/upload/test.png Your assistance in achieving this would be greatly appreciated. Currently, I have imp ...

Make the height of the input element equal to 100% of its parent

I am struggling with setting the height of input (type text) to fit 100% of its parent element (td). I attempted to individually adjust the height of each input using jQuery, but this method is time-consuming, especially since the site I am working on ha ...

How can I designate class #1 to the left and class #2 to the right within a loop?

Within a loop in Wordpress, two classes, photos and videos, are generated. As new photos or videos are added, they appear below the last div. The issue I'm facing is that I want all photos to be displayed on the left and all videos on the right, but u ...

Three Pictures Accompanied by Text Below, Which Relocates Next to the Images on More Compact Displays

Although sharing images is not recommended, I believe it would greatly aid in conveying my idea. I have three images that I want to display side by side (blue squares) with text below each one (red squares) on larger screens. For smaller screens, I prefer ...

When the width of the window is hidden, conceal

My webpage has a carousel with pictures, but on smaller devices, they do not appear properly. I am now trying to figure out how to hide the div if the width is less than 1015px. Here is the code for my div: <html> <body> <div id="myCarou ...

What is the best way to alter the state of a button when it is clicked in React, and then return it to its original state when

I'm working on a button that I've defined like this: <button className={class} onClick={() => changeClassProperty()}> {classText} </button> My goal is to update both the CSS styling class and the text of the button A when ...

Only execute jQuery if the last visited page was the homepage

On the homepage, the CSS that is initially loaded on the JSFiddle is present. My goal is to have the inner pages transition to their new CSS class only when visited after the homepage. <div class="container"> <div> Thank you for your ass ...

The footer is refusing to show up at the bottom of the page

Why is my footer floating in the middle of the page? It's driving me crazy. I really hope it's a quick fix. I've searched online and all the solutions point to having a fixed footer, but I don't want that because it looks bad on smaller ...