I have set a white-space: nowrap
with text-overflow: ellipsis
in some text and, when the user hovers, it should change the white-space value to normal. The hover code works fine, but I cant make the transitions work.
This is the CodePen link: https://codepen.io/anon/pen/qwYoyR
.App p {
margin-bottom: 3px;
}
.App .bg-row {
box-shadow: 0px 0px 5px #bdc3c7;
background-color: #ecf0f1a3;
padding: 20px;
border-radius: 5px;
}
.App .bg-row .repositorios {
transition: all 0.2s ease;
margin-bottom: 20px;
}
.App .bg-row .repositorios:hover .repo-content {
background-color: #d2d6d8;
transition: 500ms ease-in-out;
}
.App .bg-row .repositorios .repo-image {
width: 100%;
height: auto;
box-shadow: 0px 0px 5px #bdc3c7;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
-o-transition: all 0.2s ease;
}
.App .bg-row .repositorios .repo-content {
padding: 10px;
transition: 500ms ease-in-out;
overflow: hidden;
box-shadow: 0px 0px 5px #bdc3c7;
transition: 500ms ease-in-out;
background-color: #ecf0f1;
}
.App .bg-row .repositorios .repo-content p {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.App .bg-row .repositorios .repo-content .read-more {
transition: 500ms ease-in-out;
cursor: pointer;
}
.App .bg-row .repositorios .repo-content .read-more:hover {
white-space: normal;
transition: 500ms ease-in-out;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container App">
<div class="row bg-row">
<div class="col-sm-6 col-md-4 col-lg-3 repositorios">
<div class="repo-content">
<p class="read-more">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</p>
</div>
</div>
</div>
</div>
7
Answers
I don’t think you can animate this with css only. To animate the height you need to set it explicitly. I’d try to get the real height with js, limit the height and then apply the height in js on hover.
CodePen example you can check
You need to fixed width of col-sm-6 col-md-4 col-lg-3 = “100%” this way you achieve your answer
Apply this CSS it will work for you
Or you can do it with Jquery like in this codepen
BTW, I don’t know scss so i converted your scss to css, I apologize for that.
The issue is because
white-space
is not an animate-able property. You can animatemax-height
property – seeupdated codepen
and relevant excerpt:But when
hover
is off, the return animation will not work aswhite-space: nowrap
ruins it.Here is a hacky solution that just uses
max-height
for the transition and the ellipsis is handled by a pseudo element – seecodepen
and snippet below:These are not properties which can be transitioned.
In order to transition to one property to another, CSS requires intermediary values.
There are no intermediary values from nowrap to normal, as there are no intermediary values from heright: 0 to auto, or from display: none to block.
The only way to achieve this with CSS is to use max-height for your element, and transition that on hover. Yet, the white-space/ellipsis themselves would have to go => replace them with a pseudo-element with the same backgroundColor as the parent:
Hacky, improvable, but working with only CSS.
This can be achieve with
height: auto
but CSStransition
required height fixed value. But you can resolve this by using jQuery. I just write a basicmouseenter
,mouseleave
method and some logic to get content expand/collapse height. All mentioned changes applied on below code, you can also review the working fiddle. I hope it’ll help you out. ThanksI’v got here an example using React and CSS, but you can achieve the same in plain JS or a framework of your choice.
The key thing here is to measure the element expanded(hidden, cloned one) height and collapsed height and then handle animation by CSS transition.
CSS:
Checkout this working example Ellipsis collapse transition