0

Sorry for me newbie question, but I'm a rooky in web-dev.

I have a code which allows me to place a hooverable elements on my website. Problem is that I want to transfer it now to clickable element - best is right click.

I tried to do it with :active option in css but it didn't work.

.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;
}

Is there a simple way to achieve that in pure css?

4
  • You need to use jQuery for this. Commented Aug 2, 2019 at 6:31
  • You have tagged your question as javascript and you want a pure css solution? Commented Aug 2, 2019 at 6:33
  • Sorry, my mistake, already edited. Commented Aug 2, 2019 at 6:33
  • please provide snippet of what you've tried so far Commented Aug 2, 2019 at 6:34

1 Answer 1

3

In pure CSS you may have to use what's called the checkbox hack. Basically you use a checkbox to record whether the dropdown is open or not, and use :checked pseudo class to decide whether the dropdown-content is shown or not.

#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:checked ~ .dropdown-content {
  display: block;
}

input[type=checkbox] {
   position: absolute;
   top: -9999px;
   left: -9999px;
}
<label for="dropdown">Dropdown</label>
<input type="checkbox" id="dropdown">
<div class="dropdown-content">
  And this is the content.
</div>

I would suggest using Javascript for this instead.

Typically, in JS, this is done by adding a class such as "active" or "open" to the dropdown component itself. Then you could modify the CSS selector to display the dropdown-content from .dropdown:hover .dropdown-content to .dropdown.open .dropdown-content.

To do that, you will need to have an event listener for click (and possibly even some keyboard events such as Enter keypress) and then add the "open" or "active" class to the dropdown. Here's a snippet to show how that can be done:

var dropdown = document.querySelector(".dropdown");
dropdown.addEventListener("click", function(e) {
  var dropdown = e.target;

  // If it has "open" class, remove it. Else add it.
  if (dropdown.classList.contains("open")) {
    dropdown.classList.remove("open");
  } else {
    dropdown.classList.add("open");
  }
});
.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.open .dropdown-content {
  display: block;
}
<div class="dropdown">
  Let's say this is dropdown.
  <div class="dropdown-content">
    And this is the content.
  </div>
</div>

Sign up to request clarification or add additional context in comments.

8 Comments

How does it not work? What is the problem? Did you try clicking on the dropdown?
Clicking on the element don't provide any action. I copied JS function into my JS file and changed the CSS content.
Does it work when you click "Run code snippet" here? If yes, then you might be missing something. Also, check the console to see if there are any errors.
Yes I have an error, dropdown is null. But I have class dropdown in my html code.
It is hard to diagnose the issue without looking at the code. Can you try debugging in the developer tools? Or you can create a reproducible example in JSFiddle or maybe post a separate question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.