DrComputerScience DrComputerScience - 17 days ago 4
CSS Question

How do i make my image move across the page even when the resolution of screen changes

I have an image which goes from one side off the screen to other. However, when I open the HTML on a different sized computer/laptop, it does not fit and looks out of place. How do I fix this?

CODE:



body {
text-align: center;
}
div.container {
text-align: left;
width: 710px;
margin: 0 auto;
border: 12px solid black;
border-radius: 10px;
}
div.content {
width: 700px;
min-height: 400px;
background-color: white;
padding: 5px;
}
@-webkit-keyframes mini {
from {
left: 410px;
}
}
.mini {
position: absolute;
top: 280px;
left: 950px;
width: 166px;
height: 70px;
z-index: 10000;
-webkit-animation: mini 3s;
animation: mini 8s;
}

<div class="container">
<div class="content">
<img src="Media/buscartoon.jpg" class="mini" />
</div>
</div>




Answer

Try the following css classes that i have ammended. I have kept the top at 5px which makes room for the 5px padding within the content div. Also the 50% transformation formal includes the left 100% - (width of the image + right-padding).

You can now adjust the top to make it as you see fit.

CSS changes:

 div.content {
    width: 700px; min-height: 400px;
    background-color: white; padding: 5px; 
    position: relative;
 }

    /* Chrome, Safari, Opera */
    @-webkit-keyframes myfirst
    {
       0%   {left:0%; top:5px;}
        50%  {left: calc(100% - 105px);}
        100% {left:0%; top:5px;}
    }

    /* Standard syntax */
    @keyframes myfirst
    {
         0%   { left:0%; top:5px;}
        50%  {left: calc(100% - 105px);}
        100% {left:0%; top:5px;}
    }

Sample: http://codepen.io/Nasir_T/pen/ZBpjpw Hope this helps.

[Edit - Code changed in question]

I think in both scenarios you will need to set the content div with position:relative to keep the image contained within it as the image itself is position:absolute. Along with that you need to use percentage values for the left and top in order for the animation and the position to be in the right place regardless of the size of the screen.

For the updated code in question please check the following code sample:

http://codepen.io/Nasir_T/pen/ObRwmO

Just adjust the key frame left percentage according to your need.

Comments