How can I make a hidden element unhidden and transition?
Example:
I want to animate a <div> that has display: none; height: 0px; transition: height 600ms;.
So on click I add a class with display: block; height: 100px;.
The height does not animate.
CodePen
I would prefer a solution that uses transition, but if none is available I can use animation. I am not looking for any answers that use javascript.
You're not going to be able to animate it with display. If you give your .submenu class an overflow: hidden; and remove the display: none;, it will animate as desired since you're already animating the height from 0.
CSS
.submenu {
height: 0;
overflow: hidden; /* <-- Add This */
background: blue;
transition: height 600ms ease 0ms;
}
CodePen
$('.menu').click(function(){
$('.submenu').toggleClass('open');
});
.menu {
background: red;
position: fixed;
top: 0;
left: 0;
right: 0;
transition: all .2s ease-in-out;
}
.submenu {
height: 0;
overflow: hidden;
background: blue;
transition: height 600ms ease 0ms;
}
.submenu.open {
display: block;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="menu">
<h1>Click me to expand a submenu</h1>
<div class="submenu">
<p>test 1</p>
<p>test 2</p>
</div>
</div>
Related
I've been struggling to hover to work. All this should do is have a red container div and when you hover it, a black inner div drops down from the top to block the container. I must be doing something basic wrong here.
HTML:
<div class="container">
<div class="inner" />
</div>
CSS:
.container {
position: relative;
width: 200px;
height: 200px;
background: red;
}
.inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
max-height: 0;
background: black;
transition: max-height 2s ease-out;
overflow: hidden;
}
.container:hover .inner {
max-height: 200px;
}
As mentioned by Temani Afif, this was nothing more than missing a height.
I found a strange issue while hovering over a div that took me some time to troubleshoot. I'm curious to why this happened to help myself and others in the future.
<div>
<img src="http://placehold.it/300x300" alt="" />
<h3>Smoooth</h3>
</div>
<div>
<h3>Janky</h3>
<img src="http://placehold.it/300x300" alt="" />
</div>
Essentially, hover over the first one i get the expected result. But when the h3 is before the image, it acts all janky.
codepen attached.
See the Pen zqZOPN by Mark Szymanski
Actually the bug is that the h3 is visible in the second version. It should be hidden behind the img since it comes before it in the DOM.
For some weird reason, if you change the opacity of the img to a value less that 1, the h3 disappears, as it should.
img {
display: block;
opacity: .99;
margin: 0;
-webkit-backface-visibility: hidden;
/* Chrome, Safari, Opera */
backface-visibility: hidden;
transition: opacity 200ms ease-in-out;
}
* {
box-sizing: border-box;
}
section {
width: 600px;
margin: 3em auto;
}
div {
float: left;
position: relative;
width: 48%;
margin: 1%;
background-color: dodgerblue;
overflow: hidden;
cursor: pointer;
}
img {
display: block;
opacity: .99;
margin: 0;
-webkit-backface-visibility: hidden;
/* Chrome, Safari, Opera */
backface-visibility: hidden;
transition: opacity 200ms ease-in-out;
}
h3 {
position: absolute;
z-index: 0;
top: 50%;
left: 15%;
text-align: center;
width: 70%;
background-color: dodgerblue;
color: #fff;
font-weight: 300;
padding: 1em;
transform: translateY(-75%);
-webkit-backface-visibility: hidden;
/* Chrome, Safari, Opera */
backface-visibility: hidden;
transition: background-color 200ms ease-in-out;
}
div:hover h3 {
background-color: transparent;
}
div:hover img {
opacity: 0.25;
}
<section>
<h4>Why does the h3 jump on hover when it's placed before the image in the mark-up?</h4>
<div>
<img src="http://placehold.it/300x300" alt="" />
<h3>Smoooth</h3>
</div>
<div>
<h3>Janky</h3>
<img src="http://placehold.it/300x300" alt="" />
</div>
</section>
And that is the reason of the flickering, because as soon as the img opacity changes to a value lower than 1, the h3 hides behind the img.
To solve your problem, you have to add z-index:1 to your h3 to force it to come in frond of the img
I'm trying to emulate the hover effect you can see here:
http://www.timeout.com/newyork (When you hover on the articles.)
I understand how to make a div move on :hover, what I don't understand is how they've hidden the "read more" button until the div is hovered over.
Essentially I would like to know how to hide a div until mouse over, then have it slide out from under another.
Here is a pure CSS solution I quickly hacked up: CSS Hover Effect
h1, h2, h3, h4, h5{
margin:0px;
}
.tile{
overflow: hidden;
width: 400px;
height:350px;
}
.tile:hover > .body{
transition: all 0.5s ease;
top: -3em;
}
.body{
transition: all 0.5s ease;
background-color: #333;
margin:0px;
color: #fafafa;
padding: 1em;
position:relative;
top: -1em;
}
<div class="tile">
<img src="http://lorempixel.com/400/300">
<div class="body">
<h2>Test Header</h2>
<p>Info to display</p>
</div>
</div>
Basically, I just change the position of the text div when I hover over the main div and add a transition animation to it.
They coukd change the maxHeight ...
.read_more {
maxHeight: 2px;
overflow:hidden;
}
.read_more:hover {
maxHeight: 30px;
}
See if this simple example helps:
.main{
width: 300px;
height: 300px;
position: relative;
background:yellow;
overflow:hidden;
}
.hovered{
width: 100%;
height: 64px;
background: gray;
position: absolute;
bottom: -28px;
}
.hovered span{
background: red;
color: #fff;
display:block;
width: 100%;
padding: 5px 0;
}
.main:hover .hovered{
bottom: 0;
}
https://jsfiddle.net/4zak8bfp/
You can do it using some jQuery addClass() and removeClass() methods.
Here is an example:
HTML:
<div class="wrapper">
<div class="caption">
<H1>This is a title</H1>
<p>
This is sample contents...
</p>
<div class="read-more-wrapper">
Read More
</div>
</div>
</div>
CSS:
.wrapper{
position: relative;
width: 450px;
height: 250px;
background-color: #2f89ce;
overflow: hidden;
}
.caption{
display: block;
position: absolute;
bottom: -30px;
width: 100%;
height: auto;
background-color: #fff;
transition: all ease-in-out 0.3s;
}
.read-more-wrapper{
background-color: #d03134;
height: 30px;
}
.slidein{
bottom: 0;
transition: all ease-in-out 0.3s;
}
JQuery:
$('.wrapper').on('mouseenter', function(){
$(this).find('.caption').addClass("slidein");
}).on('mouseleave', function(){
$(this).find('.caption').removeClass('slidein');
});
Here is the fiddle:
https://jsfiddle.net/bk9x3ceo/2/
Hope that helps.
I have a hoverable CSS-only tooltip that works well in most instances, but I want to add a scrollbar when it exceeds a max-height. If I add max-height: 50px; overflow-y: auto, my pseudo elements :before and :after will disappear due to the overflow property.
See: http://jsfiddle.net/accelerate/24xwru1n/
Is there a way to add a scrollbar to my tooltip while maintaining my pseudo elements? Or will I have to live without my pseudo elements to make it work?
I'm afraid you have to add a wrapper when you want a scroll in hover and apply to this the css as in tooltip-text:
HTML
<div class="tooltip tooltip-scroll">Hover over me for scrollbar
<div class="wrapper">
<span class="tooltip-text">Hello there<br/>abc<br/>def<br/>ghi<br/>jkl<br/></span>
</div>
</div>
.wrapper{
position:relative;
}
.tooltip {
transform: none;
margin: 50px;
}
.tooltip:hover > .tooltip-text, .tooltip:hover > .wrapper {
pointer-events: auto;
opacity: 1.0;
}
.tooltip > .tooltip-text, .tooltip >.wrapper {
display: block;
position: absolute;
z-index: 6000;
overflow: visible;
padding: 5px 8px;
margin-top: 10px;
line-height: 16px;
border-radius: 4px;
text-align: left;
color: #fff;
background: #000;
pointer-events: none;
opacity: 0.0;
-o-transition: all 0.3s ease-out;
-ms-transition: all 0.3s ease-out;
-moz-transition: all 0.3s ease-out;
-webkit-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
/* Arrow */
.tooltip > .tooltip-text:before, .tooltip > .wrapper:before {
display: inline;
top: -5px;
content: "";
position: absolute;
border: solid;
border-color: rgba(0, 0, 0, 1) transparent;
border-width: 0 .5em .5em .5em;
z-index: 6000;
left: 20px;
}
/* Invisible area so you can hover over tooltip */
.tooltip > .tooltip-text:after, .tooltip > .wrapper:after {
top: -20px;
content: " ";
display: block;
height: 20px;
position: absolute;
width: 60px;
left: 20px;
}
.wrapper > .tooltip-text {
overflow-y: auto;
max-height: 40px;
display: block;
}
<div class="tooltip">Hover over me for no scrollbar<span class="tooltip-text">Hello there<p/>abc<br/>def<br/>ghi<br/>jkl<br/></span></div>
<p/><p/><p/>
<div class="tooltip tooltip-scroll">Hover over me for scrollbar
<div class="wrapper">
<span class="tooltip-text">Hello there<br/>abc<br/>def<br/>ghi<br/>jkl<br/></span>
</div>
</div>
First of all, I'm sorry if this question was already answered but I couldn't find it anywhere.
I'm trying to get a smooth transition when hovering over a div. On top of this div is another div, which gets visible when hovering. The transition didn't seem to do the trick, so how can I pull this off? (jquery instead of CSS, or other/better code?)
Maybe there's another way of doing this, I hope you guys can help me.
HTML:
<ul>
<li>
<div class="hover">
<a href="#">
<p>Test</p>
</a>
</div>
<img src="#" alt="img" />
<div class="text">
<p>Test</p>
</div>
</li>
</ul>
CSS:
body { background-color: #eee;
}
ul { margin: 0;
padding: 0;
list-style: none;}
ul li { width: 30.33%;
height: auto;
margin-right: 3%;
padding: 5px;
background-color: #fff;
float: left;
position: relative;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;}
ul li img { width: 100%;
max-height: 100%;
z-index: 1;
background-color: #ddd;
border: 0;}
.text { width: 100%;
padding-top: 7%;}
.text p { margin: 0 10px;
padding-bottom: 7%;}
.hover { height: 100%;
background-color: #333;
position: absolute;
top: 0;
left: 0;
right: 0;
display: none;}
.hover a { width:100%;
height: 100%;
margin: 0;
padding: 25px;
display: block;
color: #fff;
text-decoration: none;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;}
ul li:hover > .hover { display: block;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
transition: all 0.2s ease;}
Here's the code: http://jsfiddle.net/HAFEx/
Thanks in advance!
A couple things need to be changed. Firstly, you can't animate the display property, so you should toggle opacity on hover instead.
Secondly, you should apply the transition on .hover, not just on parent:hover .hover so that it transitions both ways.
Using both of those improvements (and some formatting of your code) you get this result
.hover {
transition: all 0.2s ease;
opacity:0;
... Other properties ...
}
display:block can't be afected by a transition.
Instead use opacity.
.hover { height: 100%;
background-color: #333;
position: absolute;
top: 0;
left: 0;
right: 0;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
opacity:0;
}
ul li:hover > .hover{
display: block;
opacity:1;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
transition: all 0.2s ease;
}
http://jsfiddle.net/Rjq3y/
You cannot transition css display. Try opacity.
http://jsfiddle.net/HAFEx/2/
ul li:hover .hover {
opacity:1;
-webkit-transition: all 0.2s ease;
-moz-transition: all 0.2s ease;
transition: all 0.2s ease;
}
You can also jquery to animate the display:none property.
$("li").mouseover(function(){
$(".hover").fadeIn(1000);
});
$(".hover").mouseout(function(){
$(".hover").fadeOut(1000);
});
Check out this codepen.
http://codepen.io/anon/pen/rqozA
The display property is a number or hexadecimal value, so it cannot be tweened. In this circumstance, you can't simply have the hover state opacity: 0.
Two work arounds are Javascript (messy) and using CSS Keyframe animations. http://hschwarz77.wordpress.com/2013/10/16/css-transition-from-display-none-to-display-block-on-hover/
To the best of my knowledge, these are the only solutions to have the div completely hidden (and unusable...visibility:hidden occupies the space) and fade in.