How do I change this hover function to a clickable one?

https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_text

The above page explains how to show a dropdown menu by hovering over a link:

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  padding: 12px 16px;
  z-index: 1;
}

.dropdown:hover .dropdown-content {
  display: block;
}

<div class="dropdown">
  <span>Mouse over me</span>
  <div class="dropdown-content">
  <p>Hello World!</p>
  </div>
</div>

However, I want to execute the code when it is clicked on. How do I change it? Changing hover to click doesn’t work.

I found a clickable version here:

Yes generally you will need JS for click actions but you can utilise the checkbox hack if you want to avoid JS.

Semantics are debatable though but a more accessible version for keyboards is to use :target instead.

JS is still probably the way to go though.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.