I'm working on creating a page with some CSS-animations (due to some of my customers dont want JavaScript heavy site). So I have one element I want to animate, but in 3 different ways based on which button is clicked:
Example:
<a href="#element">One animation</a>
<a href="#element">Second animation</a>
<a href="#element">Third animation</a>
<div id="element"></div>
The way I select the element is with :target. But how can I animate the same element in three different ways using :target?
This is the CSS I have by now for testing purposes:
#element{
height: 10px;
width: 10px;
border-radius: 0px;
background-color: red;
position: absolute;
left: 440px;
top: 300px;
-webkit-transition-property: height, width, background-color,;
-webkit-transition-duration: 5s;
-webkit-transition-timing-function:ease-out;
}
#element:target{
height: 300px;
width: 500px;
border-radius: 0px;
background-color: blue;
position: absolute;
left: 200px;
top: 200px;
}
So I basically want to have 3 different :target styles on the same element. Is this possible?
You can do it by using css adjacent(+) selector like this:
HTML:
<a id='first' href="#first">One animation</a>
<a id='second' href="#second">Second animation</a>
<a id='third' href="#third">Third animation</a>
<div id="element" class="force-repaint"></div>
CSS:
#element{
height: 300px;
width: 300px;
border-radius: 0px;
background-color: red;
position: absolute;
left: 300px;
top: 50px;
-webkit-transition-property: height, width, background-color,;
-webkit-transition-duration: .2s;
-webkit-transition-timing-function:ease-out;
}
#first:target + #second + #third + #element {
background: yellow;
}
#second:target + #third + #element{
background: green;
}
#third:target + #element{
background: purple;
}
.force-repaint { -webkit-animation: bugfix infinite 1s; }
@-webkit-keyframes bugfix { from { fill: 0; } to { fill: 0; } }
Working DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With