Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make element stay in the same position using css?

Tags:

html

css

I've made a "Blinking Eye" effect using CSS and HTML, it worked fine on my usual resolution, but when I resized the screen it went to the side, check the images below to better understand.

enter image description here enter image description here

How can I make that element stay in the right place, even when resizing the screen?

/* BLINKING EYES EFFECT LOGO */

@keyframes blink {
  /**
     * At the start of the animation the dot
     * has an opacity of .2
     */
  0% {
    opacity: .2;
  }
  /**
     * At 20% the dot is fully visible and
     * then fades out slowly
     */
  20% {
    opacity: 1;
  }
  /**
     * Until it reaches an opacity of .2 and
     * the animation can start again
     */
  100% {
    opacity: .2;
  }
}

.saving blinkeyes {
  animation-name: blink;
  animation-duration: 1.4s;
  animation-iteration-count: infinite;
  animation-fill-mode: both;
  font-size: 450%;
  color: red;
  position: absolute;
  left: 255px;
  top: 45px;
  z-index: 5;
}

.saving blinkeyes2 {
  animation-name: blink;
  animation-duration: 1.4s;
  animation-iteration-count: infinite;
  animation-fill-mode: both;
  font-size: 450%;
  color: red;
  position: absolute;
  left: 302px;
  top: 45px;
  z-index: 5;
}
<p class="saving">
  <blinkeyes>.</blinkeyes>
</p>
<p class="saving">
  <blinkeyes2>.</blinkeyes2>
</p>
<center><img src="http://i.imgur.com/nPvZsjB.png"></center>

You can run it here: https://jsfiddle.net/m8hak5q3/

like image 401
Ashley Ivy Avatar asked Mar 29 '16 05:03

Ashley Ivy


2 Answers

  1. As many others have said, you need to wrap your logo and eyes in a div who's position is relative and then it is just a matter of tweaking the position of the eyes.
  2. I have also simplified your code a little by setting the justify on the blinkeyes so you only need one, and can just tweak the width to specify how far apart they are.
  3. You will need to have a common font across all browsers for this to be consistent. I have added font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
  4. Your sample images had larger pupils than the demo, (Probably because of the aforementioned font issue) so I changed your periods into large black circles &#9679;
  5. Custom Elements are not exactly a standard yet, so I changed blinkeyes into a div with a class instead
  6. The font size of 450% could be dangerous given different environments, so I set it to a fixed size.

/* BLINKING EYES EFFECT LOGO */
@keyframes blink {
    /**
     * At the start of the animation the dot
     * has an opacity of .2
     */
    0% {
      opacity: .2;
    }
    /**
     * At 20% the dot is fully visible and
     * then fades out slowly
     */
    20% {
      opacity: 1;
    }
    /**
     * Until it reaches an opacity of .2 and
     * the animation can start again
     */
    100% {
      opacity: .2;
    }
}
#logo_wrapper{
  margin:0 auto;
  overflow:hidden;
  width:513px;
  position:relative;
}
.blinkeyes {
    font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
    text-align:justify;
    text-justify:inter-word;
    width:68px;
    position:absolute;
    top:67px;
    left:37px;
    animation-name: blink;
    animation-duration: 1.4s;
    animation-iteration-count: infinite;
    animation-fill-mode: both;
    font-size: 40px;
    color: red;
    z-index: 5;
}
<div id='logo_wrapper'>
  <img src="http://i.imgur.com/nPvZsjB.png">
  <div class='blinkeyes'>
    &#9679; &#9679; &nbsp;&nbsp;&nbsp;
  </div>
</div>

JSFiddle : https://jsfiddle.net/m8hak5q3/8/

like image 131
trex005 Avatar answered Oct 22 '22 10:10

trex005


From what you have described in the question, the image of the skull has different position from the blink eyes.

So to keep them moving together, they need to be put in one container like a div and set the div as position:relative. Then apply position: absolute to the skull picture and the two red eyes.

For Example, Html might be(assuming the skull image is 100px width and 100px height):

<div id="container">
    <p class="saving"><blinkeyes>.</blinkeyes></p>
    <p class="saving"><blinkeyes2>.</blinkeyes2></p>
    <img src="skull-with-crown.jpg" width="100" height="100" />
</div>

Then the css part, other than the red eye css(you might need to change a bit of the top and left of blinkeyes and blinkeyes2):

#container{
    position:relative;
    width: 100px;
    height: 100px;
}
#container img{
    position: absolute;
    left: 0;
    top: 0;
}

This might be just one of approaches. There might be something else that you would prefer. Hope this can help.

like image 22
lhrec_106 Avatar answered Oct 22 '22 12:10

lhrec_106