The initial solution was to put a container to limit the action of the object:
@keyframes anima{
0%{ left: 0; }
50%{ left: 100%;}
100%{ left: 0; }
}
#container {
width:100%;
box-sizing:border-box;
position:relative;
padding-right:40px;
border:1px solid green;
}
#circle{
width: 40px;
height: 40px;
border-radius: 50%;
background-color: red;
position: relative;
top: 0;
left: 0;
animation: anima 8s infinite;
}
<body>
<div id="container">
<div id="circle"></div>
</div>
</body>
I took advantage and adjusted the CSS. When you set a value zero, you should not specify units.
The explanation is as follows: the way you did the animation, what is being animated is the upper left corner of the object, so 100% means that the left margin will be at the edge of the screen (consequently the object will be out).
Putting a padding on the right side, the same width of the object, we are compensating for the size of this.
The green edge is just so you can view the animation working properly. The margins that remain around the container are of body
, I didn’t take it in the example CSS, but just add a rule to remove it.
To conclude, the box-sizing:border-box;
makes our container 100% wide consider 100% including the padding we insert in the measure (and up to the green edge).
Evolving the idea
After a conversation in the comments, in which the author asked about the possibility of having a solution where it is not necessary to know beforehand the size of the padding, I reached a cleaner conclusion, which does not depend on container, and fits the size of the object:
@keyframes anima{
0%{ left:0;transform:translateX(0); }
50%{ left:100%;transform:translateX(-100%); }
100%{ left:0;transform:translateX(0); }
}
#ball1, #ball2 {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: red;
position: relative;
left:0;
transform:translateX(0);
animation: anima 5s infinite ease-in-out;
}
#ball2 {
width:100px;
height:100px;
background-color:blue;
}
body, html {
margin:0;
padding:0;
}
<body>
<div id="ball1"></div>
<div id="ball2"></div>
</body>
The trick here was to move the animation pivot, which goes from the top left to the top right, using translate()
, so that when arriving at the right margin the object does not leave the screen.
I took the opportunity to change the measurements to simulate the full screen without edge, with the element actually touching the margins.
ok got it. Now answer me one thing, which I was initially thinking about and didn’t put in the question. And if the size of the red ball were dynamic, that is, if the width and height were variable (controlled by javascript or on the server), is there any solution where I don’t need to define the padding-right:40px? That would be interesting because it would have a more generic solution.
– Savrige
This demand is quite different from the original question proposal. I will give a thought here. Although if you are going to control by JS or the server, just change the properties in both places, no? (both the object and the container)
– Bacco
the size of the ball would be programmatically changed via JS. Then you would have to programmatically change the padding-right as well... I think it works. But it would be more "clean" if there was some way to make it pure CSS.
– Savrige
I thought about using the css’s Calc() function, but I don’t know if it would be possible in that context
– Savrige
@Maigret I think I’ve solved.
– Bacco
show! now it’s top.
– Savrige