Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Javascript classList.add to element with 0px height

Not sure if I'm missing something obvious here, but I'm trying to add a class to an element that has 0px height to begin with.

const teamMemberBioShow = function() {
  const bio = document.querySelector(".bio");
  if (bio.classList.contains("hidden")) {
    bio.classList.remove("hidden");
    bio.classList.add("show");
  } else {
    bio.classList.remove("show");
    bio.classList.add("hidden");
  }
}

const teamMemberBioMore = document.querySelector(".bio-more");

teamMemberBioMore.addEventListener('click', teamMemberBioShow);
.bio.hidden {
  opacity: 0;
  height: 0;
}

.bio.show {
  opacity: 1;
  height: auto;
}
<div class="bio hidden">
  Bio goes here
</div>

<div class="bio-more">Read Bio</div>

On click, the class .show is added and the .hidden class is removed. But it doesn't work if the .hidden class has 0px assigned to it.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The problem is that if you put height 0 but do not set the overflow: hidden the text content of that div will overflow and cover the clickable text, so the click event won't be dispatched:

const teamMemberBioShow = function() {
  const bio = document.querySelector(".bio");
  if (bio.classList.contains("hidden")) {
    bio.classList.remove("hidden");
    bio.classList.add("show");
  } else {
    bio.classList.remove("show");
    bio.classList.add("hidden");
  }
}

const teamMemberBioMore = document.querySelector(".bio-more");

teamMemberBioMore.addEventListener('click', teamMemberBioShow);
.bio.hidden {
  opacity: 0;
  height: 0;
  overflow: hidden;
}

.bio.show {
  opacity: 1;
  height: auto;
}
<div class="bio hidden">
  Bio goes here
</div>

<div class="bio-more">Read Bio</div>

about 4 years ago · Juan Pablo Isaza Report

0

This is because even if your element .bio.hidden has an opacity of 0, it messes up with the click event, and it overflows over the other div.

Try putting display: none if you want to hide an element

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!