I want to change an HTML element position from static to fixed at top 50% and left 50% of the browser window but the code just leads to background-color change!
div {
width: 100px;
height: 100px;
background-color: red;
position: static;
top: auto;
left: auto;
animation-name: example;
animation-duration: 4s;
}
@keyframes example {
from {
background-color: red;
position: static;
top: auto;
left: auto;
}
to {
background-color: yellow;
position: fixed;
top: 50%;
left: 50%;
}
}
You can use CSS property transition to smoothly move, change size, change position, change color or other attribute of HTML element. To set an animation, you have to include the transition property into the specific style and set the attributes.
You can't change css position value with css animation. example is with hover, you can do according to your choice. i khow how to do it with jQuery but i want use CSS.
CSS allows animation of HTML elements without using JavaScript or Flash! The numbers in the table specify the first browser version that fully supports the property. What are CSS Animations? An animation lets an element gradually change from one style to another.
Short answer: You can't animate position. Instead of applying position: fixed through keyframes try to add a class fixed or something via javascript. Alternative you could move the element with transform: translate but it won't be fixed. how set {transform: translate} by percentage for browser dimention ?
Short answer: You can't animate position.
Instead of applying position: fixed
through keyframes try to add a class fixed
or something via javascript.
An example: https://jsfiddle.net/6Ldqhoce/
Alternative you could move the element with transform: translate
but it won't be fixed.
I found the answer via 'Vangel Tzo' help :
div {
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
}
@keyframes example {
from {
background-color: red;
}
to {
background-color: yellow;
transform: translate(50vw, 50vh);
}
}
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