KingPolygon
KingPolygon

Reputation: 4755

Centering Image with Z-Index in a Resizable Window?

My images were centering perfectly until I decided to place an image behind another and use z-index. I was using relative positioning, but after moving to absolute, it aligns to the left.

How can I center it, even if window is resized?

Heres my code in a jsfiddle:http://jsfiddle.net/WJPhz/

HTML

<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript">  </script> 
<center>
    <div id="sign" style="z-index:1">
        <img src="http://s9.postimg.org/bql0ikclb/dope.png" alt"">
    </div>
    <div id="numbers" style="z-index:0">
        <img src="http://s9.postimg.org/z3j212sov/image.png" alt"">
    </div>
</center>

CSS

#sign {
    cursor: pointer;
    max-width: 241px;
    position:absolute;
}

#numbers {
    cursor: pointer;
    max-width: 241px;
    position:absolute;
}

Javascript

$(document).ready(function() {
     $('#sign').hide().delay(1000).fadeIn(3000);
     $('#numbers').hide().delay(2000).fadeIn(3000);

     $("#sign").click(function() {
            $('#sign').fadeOut(3000);
    });
});

Upvotes: 0

Views: 1184

Answers (3)

Gangadhar
Gangadhar

Reputation: 1749

#sign {
    cursor: pointer;
    max-width: 241px;
    position:absolute;
    margin-left:calc(50% - 120px);
}

#numbers {
    cursor: pointer;
    max-width: 241px;
    position:absolute;
    margin-left:calc(50% - 120px);
}

This should work.

Upvotes: 0

Pete
Pete

Reputation: 58442

A workaround would be to put your images in another div and center that div:

html

<div class="center">
    <div id="sign">
        <img src="http://s9.postimg.org/bql0ikclb/dope.png" alt"" />
    </div>
    <div id="numbers">
        <img src="http://s9.postimg.org/z3j212sov/image.png" alt"" />
    </div>
</div>

css

.center {
    position:relative; 
    max-width: 241px;
    margin:0 auto;
}

#sign {
    cursor: pointer;
    max-width: 241px;
    position:absolute;
    top:0; 
    left:0;
    z-index:2;
}

#numbers {
    cursor: pointer;
    max-width: 241px;
    position:absolute;
    top:0; 
    left:0;
    z-index:1;
}

http://jsfiddle.net/peteng/WJPhz/2/

Upvotes: 2

Markipe
Markipe

Reputation: 606

Try this one maybe this can help http://jsfiddle.net/markipe/WJPhz/1/

CSS

left:50%;
margin-left: -120px; /*div width divide by 2*/

Upvotes: 1

Related Questions