Animate :target element on target event in CSS3 - css

I am building a lightbox based on the CSS3 selector :target which selects an element based on the hash in the url. I want to animate the target element on the :target event, but this doesn't seem to work.
Let's say we have a div #banana which is shown when a link to #banana is pressed.
#banana {display: none;}
#banana:target {display: block;}
This works fine. But when trying to animate the element, that doesn't work. See this fiddle.
div#banana {
display: none;
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
opacity: 0;
transition: opacity 5s linear 1s;
}
div#banana:target {
display: block;
opacity: 1;
}
The element won't fade in. It is as if the browser skips the animation and immediately triggers the end result.

The problem is that you are changing the display property. The display property can't be transitioned since you can't really animate an element turning from nothing into a block.
The display property can be left out altogether. You will however need to give your element visibility: hidden so that it will not prevent the link from being clicked, then transition it to visibility: visible:
div#banana {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
opacity: 0;
transition: opacity 5s linear 1s;
visibility: hidden;
}
div#banana:target {
opacity: 1;
visibility: visible;
}
Updated fiddle

It's not possible to animate display property. There is simply no gradual stages between none and block.
In your case you can "hide" element by using huge negative top position and revert it back to 0 on target event. Actual transition will be handled by changing opacity.
div#banana {
position: fixed;
left: 0;
top: -1000px;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
opacity: 0;
transition: opacity 1s linear;
}
div#banana:target {
top: 0;
opacity: 1;
}
<div id="banana">
close
</div>
Do you want a banana? Click me!

Related

CSS transition to fade in image isn't working

Please can you help troubleshoot the transition in this CSS? My browser can see the code in the inspector but no transition is taking place. I have tried operating the transition on different properties including width and position but nothing works.
#header-image {
position: absolute;
top: 30px;
right: 30px;
background: transparent;
width: 250px;
margin-left: 10px;
opacity: 1;
transition: opacity 2s linear 1s;
}
I know I'm probably being thick so apologies in advance.
In order for the transition to work.. the property value should change. only then it will trigger the transition.
i.e) lets say #header-image initially has opacity: 0; width: 50px;.
but when you hover it you want to increase the opacity and width opacity: 1; width: 250px;
so your css will look like..
#header-image {
position: absolute;
top: 30px;
left: 30px;
background: blue;
width: 100px;
height: 100px;
margin-left: 10px;
animation: fadeIn 2s linear;
}
#keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
<div id="header-image"></div>
Then your transition will work. So basically transition will work only when there is a change in the value. But in your case you are setting the opacity:1 initially by default.
If you want to add this effect on page load then you have to use css animation or javascript. Below I have given an example snippet on how it can be achieved using css animation.
However if you are planning to use many animations then I recommend to use some popular libraries like Animista, Animate.css, wow.js

Text transform from behind the box

I have a div element containing a paragraph tag of text. When I hover this div, I want the text to come out the div (from behind). I tried negative z-index but it didn't work.
.box p {
display: block;
position: absolute;
top: 0;
right: 0;
opacity: 0;
z-index: -1;
visibility: hidden;
transition: top .3s, opacity .3s, visibility .3s, z-index .3s;
}
.box:hover p {
top: -28px;
opacity: 1;
z-index: 1;
visibility: visible;
}
Two problems. First, you have visibility:hidden in both cases so the element will never show.
The second problem is, the div has no height when the <p> isn't visible or hovered over so you can't hover over it at all.
To fix this to help get you on your way:
div {height:100px;}
.box p {
display: block;
height:300px;
position: absolute;
top: 0;
right: 0;
opacity: 0;
z-index: -1;
transition: top .3s, opacity .3s, visibility .3s, z-index .3s;
}
.box:hover p {
top: -28px;
opacity: 1;
z-index: 1;
}
But that might be the ideal way for you and you'll need to adjust it.
Note that, on hover, you have top: -28px; so it's scrolled slightly off screen on the right hand side.
EDIT:
Another thing I want you to be aware of. z-index is not a transitional property. That is, you cannot transition from zero to 0.1 to 0.2 on your way to a rounded 1.0. z-index is an actual layer and you can only go in whole units from one level to the next.

CSS animation-fill-mode not working for display:none

I have the following code to show/hide buttons on a carousel based on css classes. Everything works as intended, except on hideButton, the display is not set to "none" after the animation is done.
The effect is that although the button fades, it's still on top of underlying items and prevents clicks from hitting them.
.pills-tab-carousel__button {
position: absolute;
top: 0;
height: 100%;
line-height: 100%;
width: 64px;
animation: showButton .5s forwards;
}
.pills-tab-carousel__button--hidden {
animation: hideButton .5s forwards;
}
#keyframes hideButton {
0% {
opacity: 1;
display: block;
}
100% {
opacity: 0;
display: none;
}
}
#keyframes showButton {
0% {
opacity: 0;
display: none;
}
1% {
opacity: 0.01;
display: block;
}
100% {
opacity: 1;
display: block;
}
}
Why not to use visibility: hidden; since your element is already position absolute so you don't need to worry about layout.
Check out the fiddle I just created:
https://jsfiddle.net/uvxeqaLn/
You cannot animate the display property.
As an alternative you can set the property pointer-events: none; which will allow clicks to pass through the hidden element.
Just be aware that this is not supported in any IE versions prior to IE11.

Unclaimed space - dropdown menu CSS in Wordpress

I have a problem with obtaining the effect of the dropdown menu to Wordpress template. I tried already do for many hours and it is unchanged. My knowledge of CSS is not too big, so I ask you for help.
I added to the template function dropdown menu that works on the theme of the child. I would like to get rid of gaps that are unnecessary for items from the sub-menu. Example, I'm interested in the effect as in the case of the "Test 1" and "dropmenu."
Child code:
.sub-menu {
visibility: hidden; /* hides sub-menu */
opacity: 0;
top: 100%;
left: -20px;
height: 0px;
width: 100%;
transform: translateY(-2em);
z-index: -1;
transition: all 0.3s ease-in-out 0s, visibility 0s linear 0.3s, z-index 0s linear 0.01s;
}
.menu-item:hover .sub-menu {
visibility: visible; /* shows sub-menu */
opacity: 1;
display: block;
height: 64px;
z-index: 1;
transform: translateY(0%);
transition-delay: 0s, 0s, 0.3s; /* this removes the transition delay so the menu will be visible while the other styles transition */
}
.menu-item {
position: relative;
display: inline-block;
}
Image: explanation
Website: here link
The space is coming from .sub-menu. You have height: 0; on .sub-menu, which is good, but the margin-top on the li's is overflowing and creating that space.
Adding overflow: hidden; to .sub-menu seems to fix it.

Does Webkit CSS-Animate pseudo-elements?

I'm working on a new overlay screen technique for a site that I'm working on. I want to leverage animation to CSS because it is easier, and faster than JavaScript animations. I'm doing something simple, but I'm having trouble with webkit-based browsers like Chrome and Safari.
This is the code I'm using:
body:after {
content: '';
width: 100%;
height: 100%;
background-color: #000;
position: fixed;
top: 0;
left: 0;
z-index: -1;
transition-duration: .5s;
-webkit-transition-duration: .5s;
opacity: 0;
}
body.dimmed:after {
z-index: 9999;
opacity: .7;
}
AS you can see, it uses the after pseudo-element, and based on the body class it animates it to a show it or hide it. It works well on Firefox, but not on Chrome or safari. On these browsers the animation does not happen, and the change is instantaneous, which is not what I want. If you apply the same CSS to the body, rather than the pseudo-element, the animation happens:
body {
content: '';
width: 100%;
height: 100%;
background-color: #000;
position: fixed;
top: 0;
left: 0;
z-index: -1;
transition-duration: .5s;
-webkit-transition-duration: .5s;
opacity: 0;
}
body.dimmed {
z-index: 9999;
opacity: .7;
}
This makes me think that transitions do not apply to pseudo-elements on Chrome. Should this be reported as a bug?
It's a known bug, known for years already:
https://bugs.webkit.org/show_bug.cgi?id=23209
http://code.google.com/p/chromium/issues/detail?id=54699
BTW, at the moment you could try to use this technique: http://kizu.ru/en/pseudos/ — by triggering the transition on the element itself and then inheriting the value to the pseudo-element. That won't work for every property (for opacity for example), but you could weork around with that using some imagination.

Resources