Unable to display link within a div using CSS hide/show functionality

I came across an example of CSS/HTML on this site and made some modifications. Everything seems to be working fine, except for the <a> link inside the div - it's visible without any issues but when I click on it, all that happens is the div gets hidden.

Here is the code snippet:

.clicker {
  outline: none;
  text-align: center;
  font-size: 1em;
  font-style: italic;
  color: blue;
  text-decoration: underline;
  cursor: pointer;
  margin-top: -2em;
  margin-bottom: 3em;
}
.hiddendiv {
  display: none;
}
.clicker:focus + .hiddendiv {
  display: block;
}
body#l-autore div.main div.hiddendiv ul.opere {
  font-size: 0.75em;
  list-style-type: none;
  font-style: italic;
  text-align: center;
}
body#l-autore div.main div.hiddendiv ul.opere li {
  display: inline;
}
<body id="l-autore" xml:lang="it-IT">
  <div class="header">
    <h1>Brevi profili biografici degli autori</h1>
  </div>
  <div class="main">
    <h1 id="sigil_toc_id_1">EXAMPLE</h1>
    <div class="clicker" tabindex="1">
      <h2 id="sigil_toc_id_2">
<p class="autori" id="id_romano">Romano</p>
</h2>
    </div>
    <div class="hiddendiv">
      <ul class="opere">
        <li>
          <a href="Cap02sez1.html">
            <span class="nr">I</span>
          </a>
        </li>
        <li>
          <a href="Cap02sez2.html">
            <span class="nr">II</span>
          </a>
        </li>
      </ul>
    </div>
  </div>
</body>

Answer №1

Upon loading the page, the div containing the links is initially hidden by setting display: none. When Romano is clicked, a temporary visibility is set using .clicker:focus + .hiddendiv. However, this only works when .clicker is in focus, which changes once a link is clicked.

Unfortunately, it seems impossible to solve this with pure CSS due to the lack of a previous selector or parent selector. This makes targeting the display of .hiddendiv from a child link challenging without JavaScript.

To address this issue with minimal changes, consider using JavaScript (preferably jQuery) as shown below:

HTML:

<p class="autori" onclick="show()"/>

CSS:

.visible { display: block; }

(Remove the .clicker:focus + .hiddendiv selector)

JavaScript / jQuery:

function show() {
    $(".hiddendiv").toggleClass("visible");
}

This script toggles the visibility of .hiddendiv when 'Romano' is clicked. If you prefer the div to always be visible, replace .toggleClass() with .addClass().

If JavaScript is not an option, there is a workaround using raw CSS! By utilizing a pseudo-checkbox and modifying the HTML structure, you can toggle visibility effectively. See the updated solution without JavaScript provided below for detailed instructions.

Updated Solution Without JavaScript

You can achieve the same effect without JavaScript by simulating button clicks with modified HTML structure. By creating a label for a hidden checkbox that triggers visibility changes, you can control the display of .hiddendiv solely through CSS:

<input type="checkbox" id="show" />
<label for="show">
   <div class="clicker" tabindex="1">
      <h2 id="sigil_toc_id_2">
         <p class="autori" id="id_romano">Romano</p>
      </h2>
   </div>
</label>

The CSS modifications are essential for this method to work. Hide the checkbox and set up styles based on its :checked status:

#show {
    display: none;
}

#show:checked ~ .hiddendiv {
    display: block;
}

#show:not(:checked) ~ .hiddendiv {
    display: none;
}

By removing the previous selectors and implementing these changes, you can create a visibility toggle for a separate div using only HTML and CSS!

Enhanced Solution With No JavaScript And Multiple Hidden Content Divs

For scenarios where multiple hidden sections need individual toggling, you can expand on the previous technique by introducing distinct checkboxes with similar naming conventions. Adjust the structure and CSS styling accordingly to handle various hidden content divisions simultaneously.

Here's an example of how to implement this feature with multiple toggles controlling different hidden divs:

HTML:

<input type="checkbox" id="show_1" />
    <label for="show_1"></label>
</input>

<input type="checkbox" id="show_2" />
    <label for="show_2"></label>
</input>

CSS:

[id^=show] {
  display: none;
}

[id^=show]:checked ~ .hiddendiv {
  display: block;
}

[id^=show]:not(:checked) ~ .hiddendiv {
  display: none;
}

With these modifications, you can efficiently manage multiple hidden content divs without relying on JavaScript, ensuring seamless toggling functionality across different sections.

Answer №2

Due to the CSS styling, the href link becomes disconnected before it can be executed.

When you click on "I" or "II", every code within '.hiddendiv' is released.

If you wish to show and display the ".hiddendiv" with a click event,

I recommend using JavaScript with an onclick event

[Recommended Actions]

  • Remove the CSS ".clicker:focus + .hiddendiv {display: block;}"
  • Add JavaScript to handle hiding and showing in the click event.
  • Change the 'a' to 'div' for usage with "onclick".
  • Before hiding the '.hiddendiv', execute the movePage function when clicking on "I" or "II".

function viewList(){
  document.getElementsByClassName('hiddendiv')[0].style.display = 'block'
}

function movePage(path) {
  window.location.href = path;
  document.getElementsByClassName('hiddendiv')[0].style.display = ''
}
.clicker {
  outline: none;
  text-align: center;
  font-size: 1em;
  font-style: italic;
  color: blue;
  text-decoration: underline;
  cursor: pointer;
  margin-top: -2em;
  margin-bottom: 3em;
}
.hiddendiv {
  display: none;
}
.clicker:focus + .hiddendiv {
/*  display: block; */
}
body#l-autore div.main div.hiddendiv ul.opere {
  font-size: 0.75em;
  list-style-type: none;
  font-style: italic;
  text-align: center;
}
body#l-autore div.main div.hiddendiv ul.opere li {
  display: inline;
}
<body id="l-autore" xml:lang="it-IT">
  <div class="header">
    <h1>Brief biographical profiles of authors</h1>
  </div>
  <div class="main">
    <h1 id="sigil_toc_id_1">EXAMPLE</h1>
    <div class="clicker" tabindex="1">
      <h2 id="sigil_toc_id_2">
<p class="authors" id="id_romano" onclick=viewList()>Romano</p>
</h2>
    </div>
    <div class="hiddendiv">
      <ul class="opere">
        <li>
          <div onclick=movePage('Cap02sez1.html')>
            <span class="nr">I</span>
          </div>
        </li>
        <li>
          <div onclick=movePage('Cap02sez2.html')>
            <span class="nr">II</span>
          </div>
        </li>
      </ul>
    </div>
  </div>
</body>

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

Animating a Bootstrap 4 card to the center of the screen

I am attempting to achieve the following effect: Display a grid of bootstrap 4 cards Upon clicking a button within a card, animate it by rotating 180 degrees, adjusting its height/width from 400px - 350px to the entire screen, and positioning it at the c ...

CKEditor is completely altering the format of my HTML code

Dealing with CKEditor has been a real challenge for me all day long. It seems to be reformatting my HTML code without any rhyme or reason. I've noticed that it's replacing certain tags like <dl> with <div> and <p>. To tackle thi ...

Is it possible to utilize CSS for styling page.aspx controls?

Is it possible to apply CSS styles to the controls on page.aspx? Can CSS be used for everything on the page? ...

Excessive spacing on the left-hand side of the navigation bar

I am noticing a small gap between my navigation links, approximately 2px wide, and I am struggling to remove it. Is there any CSS code that can help with this? Upon hovering over the "Downloads" link, you can see the space on the left side. Check out the ...

A step-by-step guide on incorporating box-shadow for the jackColor in the Switchery Plugin

I am looking to add a box shadow to my iOS7 style switches for checkboxes when they are checked. Here is the code I have so far: var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch')); elems.forEach(function (html) { va ...

Steps to create a clickable background element for an AFTER li:

How can I make the background arrow clickable in my list, just like the link? .listacategorias li:after { float:right; content: ""; display: block; background: url("//d26lpennugtm8s.cloudfront.net/stores/567/260/rte/setaazul.png") no-repeat; ...

Instructions for creating a zoomIn effect for a pair of images within a single div using HTML

I'm working on an index page that incorporates animation, and I am looking to create a single div with two images positioned on the left and right sides. I want to apply a zoomIn animation to both images using CSS. Any assistance from someone experien ...

Customizable Angular template that dynamically adjusts based on database configuration

In my Angular application, I have interfaces where certain divs need to be shown or hidden based on user configuration retrieved from the database. Instead of using multiple *ngIf statements like so: <div *ngIf=“hasConfig(‘blue’)> Blue ...

What is the best method for determining the current value of an On/Off switch with labels for both states?

How can I retrieve the current value of an On/Off switch that is labeled as On and Off? Below is the HTML code for this particular switch: <div class="make-switch has-switch"> <div class="switch-on switch-animate"> <input type="checkbox" d ...

Utilizing data attributes for storing and selecting dual prices on an option

I need help creating a form with options that have two potential values. The user will first choose between "Low Price" (lp) or "High Price" (hp), and then select either Type 1 or Type 2, both of which have data attributes for "hp" and "lp". My goal is to ...

When I test my jQuery scripts in jsfiddle, they run smoothly. However, they do not seem to work properly when I

My code is almost perfect, but the jQuery function is giving me trouble. It works fine in jsfiddle, but for some reason, it's not functioning in my HTML file. I don't believe extra characters are being added when copying from the HTML file. I hav ...

The jQuery datatable column header demands the presence of two cursors

I am currently working on a jquery datatable that has drag and drop column enabled along with a sorting icon. I am trying to set the cursor to change to pointer when hovering over the sorting icon, and to move when hovering over other areas of the column h ...

Picture is not appearing on the image carousel

I'm currently working on a dynamic image slider, but I'm encountering an issue where the images are not showing up. I can successfully upload images to my file directory, so I'm not sure if the problem lies within the database or elsewhere. ...

Application that utilizes Django to determine the location of a mobile device user

I'm looking to develop a mobile-responsive web application using Django-Python that will primarily be accessed on smartphones. The main functionality I want to incorporate is the ability for the app to track the user's location and display it on ...

Does Less undergo a compilation process in a single pass?

Does Less execute a single pass on the files, or does it perform multiple passes? I am particularly worried about what will happen if I include another file that redefines variables and mixins. Will the generated output use the original values, or will it ...

tips for aligning text and buttons on the same line

Is there a way to align the button with the text on the same line, instead of it dropping down (refer to the image below)? https://i.sstatic.net/hQ3YC.png This is my current html and css code (utilizing bootstrap): <a href="#" class="mx ...

Creating a table from a PHP associative array

I've been attempting to organize an associative array in ascending order and then display it in an HTML table, but I've hit a roadblock with an error. I tried searching for solutions here on SO and followed the advice provided in some threads: P ...

Issue with Navbar Menu Button Alignment not Vertical Centered

My goal is to have the navigation menu bar centered in my navbar, and it looks great on every screen size except for medium. In medium-sized screens, the menu shifts to the bottom right and I've tried various solutions like adjusting margins, padding, ...

My Angular Router is creating duplicate instances of my route components

I have captured screenshots of the application: https://ibb.co/NmnSPNr and https://ibb.co/C0nwG4D info.component.ts / The Info component is a child component of the Item component, displayed when a specific link is routed to. export class InfoComponent imp ...

Turn off automatic zooming for mobile device input

One common issue observed in mobile browsers is that they tend to zoom in on an input field or select drop-down when focused. After exploring various solutions, the most elegant one I came across involves setting the font-size of the input field to 16px. W ...