Attempting to add custom styling to the parent container of a specific element targeted using the :target selector in plain HTML and CSS

I have a document with internal links to subheaders from various categories.

My goal is to selectively hide category content based on the specific #target selected.

<html>
<style>
  .tab-folder > .tab-content {
      display: none;
  }
  .tab-folder > .tab-content:target,
  .tab-folder > .tab-content > div:target{
      display: block;
  }
</style>

<div class="tab-folder">
  <a href="#first">Show the first div</a><br>
  <a href="#second">Show the second div</a><br>
  <a href="#third">Show the third div</a><br>
  <a href="#sub-first"> show dummy in first</a><br>
  <a href="#sub-third"> show dummy in third</a><br>

  <div class="tab-content" id="first">First body! <div id="sub-first">dummy</div></div>
  <div class="tab-content" id="second">Second body!</div>
  <div class="tab-content" id="third">Third body!<div id="sub-third">dummy3</div></div>
</div>
</html>

For example, when linking to sub-first, I want only the content related to the first tab to be visible.

Answer №1

experiment with visibility and font-size instead of using display property:

.tab-folder>.tab-content {
  visibility: hidden;
  font-size:0;
}

.tab-folder > .tab-content:target,
.tab-folder > .tab-content > div:target {
  visibility: visible;
  font-size:initial;
}
<div class="tab-folder">
  <a href="#first">Display the first section</a><br>
  <a href="#second">Display the second section</a><br>
  <a href="#third">Display the third section</a><br>
  <a href="#sub-first"> show dummy in first section</a><br>
  <a href="#sub-third"> show dummy in third section</a><br>

  <div class="tab-content" id="first">First section!
    <div id="sub-first">dummy content</div>
  </div>
  <div class="tab-content" id="second">Second section!</div>
  <div class="tab-content" id="third">Third section!
    <div id="sub-third">dummy content 3</div>
  </div>
</div>

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

Using jQuery to load and parse a JSON file stored on a local system

I am a beginner in scripting languages and recently searched for ways to load and parse JSON files using jQuery. I found helpful resources on Stack Overflow. The JSON file I am working with is called new.json. { "a": [ {"name":"avc"}, ...

How can you style a two-item list in Material-UI React to display the items side by side?

I have a list that contains two items: 'label' and 'value'. This is the current layout: https://i.stack.imgur.com/HufX7.png How can I rearrange the items on the right to be positioned next to the label on the left? https://i.stack.im ...

What could be causing the mysterious line appearing underneath my bootstrap forms?

I have created a CSS sticky footer that is designed like so: #foot { position:fixed; left:0px; bottom:0px; height:30px; width:100%; text-align: right; padding-top: 7px; font-size: small; background-color: none; } However, a mys ...

Difficulty encountered when trying to use Bootstrap tooltip with Bootstrap icon

Attempting to implement bootstrap tooltips on bootstrap icons with the following code: <body> <script> const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]') ...

Declare a jQuery variable containing HTML elements

Greetings! I am facing an issue while attempting to create a jQuery variable that stores a table meant for displaying in different sections of a website. Unfortunately, I am encountering an error and struggling to comprehend the reason behind it. Below is ...

HTML/JavaScript: Embrace the Power of Dynamic Page

I have a unique element in my HTML code: <image src="http://..." style='...'> Using Python-Flask, I pass on a dynamic source address and save it as window.dynamicEmbedding. Now, during page load, I want to change the image's ...

Unable to obtain the width of images on Chrome browser

I've been developing a basic slider that moves images from right to left. It's functioning properly on Firefox, but I'm encountering an issue with Chrome when trying to obtain the width of the images. $(window).load(function() { // Loa ...

A convenient method for converting horizontal columns into vertical columns on mobile screens without using <div> tags or setting a minimum width, perfect for email formatting

After experimenting with using <div> elements and applying a min-width attribute to an email, I have come to the conclusion that these methods do not yield satisfactory results. While the alignment of horizontal columns works well on desktop screens, ...

Gatsby causing issues with Material UI v5 server side rendering CSS arrangement

I shared my problem on this GitHub issue too: https://github.com/mui-org/material-ui/issues/25312 Currently, I'm working with the Gatsby example provided in Material UI v5: https://github.com/mui-org/material-ui/tree/next/examples/gatsby After imple ...

The height of my "img" element is not being fully matched by my "a" tag

As part of a project redesigning the Kurzgesagt home page, I encountered a challenge when trying to make my a tag stretch the full height of my img. The a tag was wrapping the img in the HTML code. Below is the code I used: HTML <a href="http://kurzg ...

The function is not being called as expected when using `onclick` or `eventListener('click')`

I'm in the process of developing a login form for my website that will offer 2 options - "login" and "signup". The concept is similar to mini tabs and iframe windows. Essentially, I have two divs side by side for "login" and "signup". When the user cl ...

Creating a variable in JavaScript

In my HTML body, I have set up a global variable named index with the value <%var index = 0;%>. This allows me to access it throughout the code. I'm trying to assign the value of $(this).val() to <% index %>, but I can't seem to get ...

Tips on arranging text around an image to prevent any overlapping

CLICK HERE FOR THE ISSUE This is the outcome. I prefer the text to be separate from the image. ...

Utilizing CSS Masks in Bootstrap 4

Is it possible to create a "Mask" effect in CSS without using Bootstrap 4 or 5? Adding a background color and z-index to an image doesn't seem to achieve the desired effect. Learn more about creating masks in CSS here. ...

Tips on solving the Navigation bar burger problem with HTML and CSS

## I am having trouble making my navigation bar responsive using HTML and CSS as the navigation burger does not appear in mobile view ## To take a look at my code on jsfiddle, click here: [jsfiddle] (https://jsfiddle.net/abhishekpakhare/nxcdso7k/1/ ...

Is there a way to modify node attributes in the tick function of a d3 force-directed graph?

When working with a force directed graph, I am tasked with creating either circle or square nodes based on the group they belong to. The node creation process involves: const node = svg .append('g') .attr('stroke', &apo ...

jQuery and ajax method are failing to insert data into the database

I am having trouble inserting data into the database using jQuery and Ajax. Can someone please assist me in figuring out if I'm doing something wrong? Here is the code snippet: form.html <!DOCTYPE html> <html> <head> & ...

What is the best way to automatically adjust the size of an image to fit its

One of the challenges with the parent container is fitting an image inside it: .parent { position: relative; text-align: center; overflow: hidden; display: flex; align-items: center; align-content: center; justify-content: cente ...

Update dynamically generated CSS automatically

Is there a way to dynamically change the CSS? The problem I'm facing is that the CSS is generated by the framework itself, making it impossible for me to declare or modify it. Here's the scenario at runtime: https://i.sstatic.net/IovGr.png I a ...

"Troubleshooting: Why Won't My Entire Div Display on the

Struggling with a website rebuild due to my limited knowledge of HTML/CSS. For some reason, this whole div section isn't showing up as it should. Here is the problematic div: <div class="container clearfix"> <div class="border-shade sli ...