TheRealFakeNews
TheRealFakeNews

Reputation: 8153

Unable to hide element with height: 0, overflow: hidden, and line-height: 0

I have a div that I want to collapse to a height of 0. I can't have it be display: none or visibility: hidden, because I want to animate on this element.

It's parent is:

position: absolute;
display: flex;
align-items: center;
top: 28px;
background: red;
width: 100%;
boxShadow: 0px 8px 6px -5px black;
zIndex: 4;

When "hidden", the styling of the element has:

height: 0px;
line-height: 0px;
overflow: hidden;

The style of the content that is overflowing has this styling:

position: absolute;
height: 75px;
top: 0;
left: 0;
right: 0;
border-bottom: 1px solid #5A5A5A;
line-height: 75px;

The three elements above are divs.

Because I am animating on div, I don't want to have to change the styling of the overflowing content if possible.

How can I properly hide the element by collapsing the height?

Upvotes: 3

Views: 4850

Answers (2)

Asons
Asons

Reputation: 87191

Since you use position: absolute the .parent will not grow with its content, so you need to give it a height, here done with height: 75px

html,
body {
  margin: 0;
}
div.parent {
  position: absolute;
  display: flex;
  align-items: center;
  top: 28px;
  height: 75px;
  background: red;
  width: 100%;
  box-shadow: 0px 8px 6px -5px black;
  z-index: 4;
  overflow: hidden;
}
.content {
  position: absolute;
  height: 75px;
  top: 0;
  left: 0;
  right: 0;
  border-bottom: 1px solid #5A5A5A;
  line-height: 75px;
  background: rgba(0,0,0,0.2);
  transition: height 1s;
  overflow: hidden;
}
div.parent:hover .content {
  height: 0;
}
<div class="parent">
  <div class="content">
    Hey, there, I'm a test text. Hover me and I disappear.
  </div>
</div>

Upvotes: 4

scoopzilla
scoopzilla

Reputation: 883

You could do it this way:

.testing {
  height: 60px;
  width: 200px;
  background-color: #333333;
  -webkit-transition:all 0.3s ease 0s;
	-moz-transition:all 0.3s ease 0s;
	transition: all 0.3s ease 0s;
  }

.testing:hover {
  height: 0px;
  }

.testing-class {
  font-size: 20px;
  text-align: center;
  color:#fff;
  }

.testing-class:hover {
  display: none;
  }
<div class="testing">
  <p class="testing-class">This is a test.</p>
  </div>

The most important part though is the transition is not present.

Upvotes: 0

Related Questions