Ensuring proper positioning with relative and absolute properties:
It is important to note that all the answers provided above overlook the necessity of positioning a parent element with a property other than static. Failure to do so will result in the images being positioned absolutely in relation to the browser window, which may not be desired.
By using position: absolute
, you can determine the position within the closest parent element that has some form of positioning. Therefore, giving the parent element a position:relative;
without specifying top or bottom values ensures that it remains unchanged from its original position, while still having the position
property declared.
<div id="container">
<img src="data:image/png;base64,R0lGODlhAQABAPAAAC+byy+byywAAAAAAQABAEAIBAABBAQAOw==" style="height:125px; width:125px;">
<img class="hide" src="data:image/png;base64,R0lGODlhAQABAPAAADCQIzCQIywAAAAAAQABAEAIBAABBAQAOw==" style="height:125px; width:125px;">
</div>
#container{
position:relative;
}
#container img{
position:absolute;
top:0;
left:0;
}
.hide:hover{
opacity:0;
}
http://jsfiddle.net/BLbhJ/1/
Note: Added functionality for hiding elements on hover.