This question already has answers here:
How to remove margin space around body or clear default css styles
(7 answers)
Closed 6 years ago.
I'm working on a website with animated elements using CSS3. One problem I have is the vertical scrolling bar. So there's an element that is constantly sliding from left to right and back:
#keyframes waves {
0% {left:-50px;}
50% {left: 0px;}
100% {left: -50px;}
}
So that this works, the object is bigger than the screen size. Now of course the vertical scrolling bar appears. How can I hide that?
I put the element in a container, where I defined overflow-x: hidden but somehow it didn't work. Maybe I just did a mistake? What method should I use?
Here's the fiddle:
https://jsfiddle.net/hsdoy3t4/
Thank you very much for your help.
Because it's taking default margin of body tag.
Set margin:0 for body tag.
body{
margin:0;
}
#home {
width: 100vw;
overflow-x: hidden;
}
#object {
background-image: url('http://www.1000skies.com/800trim.jpg');
width: calc(100% + 50px);
height: 158px;
position: relative;
-webkit-animation: waves 2s infinite;
-moz-animation: waves 2s infinite;
animation: waves 2s infinite;
}
#-webkit-keyframes waves {
0% {left:-50px;}
50% {left: 0px;}
100% {left: -50px;}
}
#-moz-keyframes waves {
0% {left:-50px;}
50% {left: 0px;}
100% {left: -50px;}
}
#keyframes waves {
0% {left:-50px;}
50% {left: 0px;}
100% {left: -50px;}
}
<section id="home">
<div id="object"></div>
</section>
Related
I'm working on a simple CSS animation and came across a curious problem.
When animating several small divs, if I zoom in or out on Chrome/Firefox the heights of the divs becomes inconsistent - despite them all sharing the same size styles.
Is there any way to address this using CSS? I want the bars to maintain a consistent height without regard to the zoom level. I realize this is something of an edge case, but want to cover as many bases as possible!
Example is here.
HTML
<div class='animation-box animate'>
<div class="animation-bar"></div>
<div class="animation-bar"></div>
<div class="animation-bar"></div>
<div class="animation-bar"></div>
</div>
CSS
.animation-box {
width: 100px;
}
.animation-bar {
animation-duration: 3s;
animation-iteration-count: infinite;
animation-name: bargraph;
animation-timing-function: linear;
background-color: #0d97c1;
height: 3px;
margin: 2px;
}
#keyframes bargraph {
0% {
width: 100%;
}
50% {
width: 10%;
}
100% {
width: 100%;
}
}
What about a simplification with only one element and less of code:
.animation-bar {
animation: bargraph 1.5s infinite linear alternate;
width: 100px;
height: 25px;
background-image: linear-gradient(#0d97c1 50%, transparent 50%);
background-position:0 0;
background-size: 100% 5px;
background-repeat: repeat-y;
}
#keyframes bargraph {
0% {
background-size: 100% 5px;
}
100% {
background-size: 10% 5px;
}
}
<div class="animation-bar"></div>
This is a problem of subpixel rendering. The issue here is that when you are zooming, the space between two bars will have to snap to a given pixel on your screen.
If you have a 3px margin # 150% zoom, the computed space is 4,5px. Meaning that the zoomed space on screen will be inconsistently rendered at 4 or 5px.
On a regular CPU computed dom, your bar will be placed at the closest value, producing those weird gaps.
What you could do to minimize the effect is applying some GPU rendered CSS (opposed to the regular CPU rendering) which is way better at rendering subpixel graphics.
One way of doing that is applying transforms.
.animation-box {
width: 55px;
margin: 0 15px 0 -5px;
}
.animation-bar {
animation: bargraph 1s infinite linear;
transform-origin: top left;
height: 3px;
background-color: #0d97c1;
margin-bottom: 3px;
}
#keyframes bargraph {
0% {
transform: scaleX(1);
}
25% {
transform: scaleX(.8);
}
50% {
transform: scaleX(.6);
}
75% {
transform: scaleX(.8);
}
100% {
transform: scaleX(1);
}
}
<div class='animation-box animate'>
<div class="animation-bar"></div>
<div class="animation-bar"></div>
<div class="animation-bar"></div>
<div class="animation-bar"></div>
</div>
I recommend an excellent article on Smashing Magazine on that topic : CSS GPU Animation: Doing It Right
I am trying to create a ken-burns like effect for image panning - and have seen multiple examples of how to do so. but my resultant code is not working on iOS and is choppy on windows larger screens.
I am using background position - and would like to use transfrom instead - which seems to work better on iOS browsers.
#-webkit-keyframes pan {
0% { background-position: 0% 0%; }
25% { background-position: 100% 100%; }
50% { background-position: 100% 0%; }
75% { background-position: 100% 100%; }
100% { background-position: 0 0; }
}
#keyframes pan {
0% { background-position: 0% 0%; }
25% { background-position: 100% 100%; }
50% { background-position: 100% 0%; }
75% { background-position: 100% 100%; }
100% { background-position: 0 0; }
}
.back {
background: url(big.png) no-repeat;
-webkit-animation: pan 80s ease-in 1s infinite;
animation: pan 80s ease-in 1s infinite;
}
The above works in general browser but is choppy. I have tried to increase the intervals etc. - but it also does not quite work on iOS.
how would I convert the above code to use transform or translate css-property instead ? I understand it is better/faster for performance and smoothness.
thanks
I am using an img tag wrapped by a div to display a svg spinner. The div has a rotate animation on it. Do you know why its not spinning on the origin and why are the inline-spinners overlapping?
I cannot use SVG animationTransform because IE does not support it. Since its a simple spinner, I was hoping to rotate just the div and get the loading effect.
Here's the plunkr demo:
https://plnkr.co/edit/jiuI4rlETsNHN1yLbG2k?p=preview
<div class="spinner">
<img src="spinner.svg" alt="">
</div>
<div class="spinner">
<img src="spinner.svg" alt="">
</div>
<h4>Inline Spinners</h4>
<div class="spinner-inline">
<img src="spinner.svg" alt="">
</div>
<div class="spinner-inline">
<img src="spinner.svg" alt="">
</div>
/* Styles go here */
body{
background: #444;
color: white;
}
.spinner{
display: block;
height: 50px;
width: 50px;
animation: spin 2s linear infinite;
transform-origin: 50% 50%;
}
.spinner-inline{
display: inline-block;
height: 10px;
width: 10px;
animation: spin 2s linear infinite;
transform-origin: 50% 50%;
}
#keyframes spin{
0% {transform: rotate(0deg);}
100% {transform: rotate(360deg);}
}
Update:
.spinner *{
width: 100%;
height: 100%;
}
.spinner-inline *{
width: 100%;
height: 100%;
}
Adding above CSS for child elements helped a little but the inline spinners still dont spin on origin.
The reason your inline-spinners have an offset in the transform origin is because the default line-height (28px) takes over your height, so resetting that you solve the origin problem:
.spinner-inline{
display: inline-block;
height: 10px;
line-height:0;/*****/
width: 10px;
animation: spin 2s linear infinite;
transform-origin: 50% 50%;
}
https://plnkr.co/edit/mW7BamvPST89ufqNyE8N?p=preview
I looked at your plunkr, although I don't believe I can answer the origin question, the inline spinners is an easy fix.
You have the width and height set to 20px (which is only half of their actual size).
Set your spinner-inline css to:
.spinner-inline{
display: inline-block;
height: 40px;
width: 40px;
animation: spin 2s linear infinite;
transform-origin: 50% 50%;
}
And they will no longer overlap.
*Alternatively, it looks like setting this does make the spinners spin on origin as well.
I want to move an image from right to left, the image should enters from the right side of the screen and exits from left side of the screen, for that I have written this css animation code but it is not working please help.
.CSS file
.imageAnimation {
position: 100% 0px;
-webkit-animation: moveToLeft 10s linear infinite;
animation: moveToLeft 10s linear infinite;
}
#-webkit-keyframes moveToLeft {
from {position: 100% 0px;}
to {position: 0% 0px;}
}
#keyframes moveToLeft {
from {position: 100% 0px;}
to {position: 0% 0px;}
}
.HTML file
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<link rel="stylesheet" type="text/css" href="animation.css"/>
</head>
<body>
<img class="imageAnimation" style="position:absolute; top:0px; left:100%;" src="enemy.png" width="300" height="100"/>
</body>
</html>
Try this
body {
position: relative;
overflow-x: hidden;
}
img {
position: absolute;
animation: moveImage 3s linear infinite;
left: -350px;
}
#keyframes moveImage {
100% {
transform: translateX(calc(100vw + 350px));
}
}
<img src="http://placehold.it/350x150">
350px in translateX(calc(100vw + 350px)); is width of image so image will go to end of window + width of image. So you will have to change that for width of your image, same for left: -350px.
You're actually really close...just using position incorrectly. Use top and left instead.
.imageAnimation {
position: absolute;
left: 100%;
top: 0px;
-webkit-animation: moveToLeft 10s linear infinite;
animation: moveToLeft 10s linear infinite;
}
#-webkit-keyframes moveToLeft {
from {left: 100%;}
to {left: 0%;}
}
#keyframes moveToLeft {
from {left: 100%;}
to {left: 0%;}
}
My answer uses calc and percentages so the box will start outside of the right side of the screen, across the view port area and out of the left side of the screen. You can read about calc browser support here. Heres the code:
.wrap {
background: pink;
height: 200px;
overflow: hidden;
position: relative;
width: 100%;
}
.box {
animation-name: moveToLeft;
animation-duration: 5s;
background-color: red;
height:100px;
left:calc(0% - 100px);
position:absolute;
width:100px;
}
#keyframes moveToLeft {
0% {left:calc(100% + 100px);}
100% {left:calc(0% - 100px);}
}
<div class="wrap">
<div class="box"></div>
</div>
I use a parent container with an overflow: hidden so the scroll bar will not show when the box is out of the frame. Also the 100px you see in the calc will need to be the same width as your element. Here is a js.fiddle with the code. Hope that helps.
I have a div with a background image that I would like to inflate using CSS3 webkit-keyframes.
I tried animating using background-size, but it looks like CSS3 (or at least Safari webkit) doesn't animate background-size.
How can I rewrite this code to get the same effect (i.e. I would like the image to inflate from the center of the div)?
Some simple vanilla javascript would also be okay, but I'd prefer a pure-CSS solution.
HTML:
<div id="image"></div>
CSS:
div#image
{
background: url('../img/image.png');
background-repeat: no-repeat;
background-position: center;
width: 125px;
height: 252px;
position: absolute;
left: 170px;
top: 260px;
-webkit-animation-delay: 0s;
-webkit-animation-timing-function: cubic-bezier(0, 0, 0.35, 1.0);
-webkit-transform: translateZ(0);
-webkit-animation-duration: 4s;
-webkit-animation-name: pop_image;
}
#-webkit-keyframes pop_image
{
0% {background-size: 0%;}
25% {background-size: 0%;}
48% {background-size: 100%;}
100% {background-size: 100%;}
}
Edit:
I also tried animating on -webkit-transform: scale(), but that didn't work either.
Edit 2:
So, animating on -webkit-transform: scale() worked, I just needed to refresh my browser.
Here is the CSS3 keyframe:
#-webkit-keyframes pop_keys
{
0% {-webkit-transform: scale(0,0);}
25% {-webkit-transform: scale(0,0);}
48% {-webkit-transform: scale(1,1);}
100% {-webkit-transform: scale(1,1);}
}
You can use css. scale and animate the images to scale up:
http://jsfiddle.net/DvVug/1/
EDIT:
Updated so the image doesn't 'jump' back to original size