Using position
:
You can define the images with absolute position. This way, the positioning of each one will be defined with the properties top
, bottom
, left
and/or right
. In this case, it is better to have an element that involves your images, to define the position relative to the element and not (directly) to the body
.
Useful links:
Position: Mozilla Developer Network
CSS3 - Positioning schemes: W3C
Sample Code:
.wrapper {
border: 2px solid #ccc; /* só para exibir o bloco onde as imagens estão */
position: relative;
height: 200px;
width: 500px
}
.dixie, .diddy {
position: absolute
}
.dixie {
top: 15px;
right: 150px;
max-width: 115px
}
.diddy {
left: 150px;
max-width: 130px
}
<div class='wrapper'>
<img class='dixie' src='http://i.stack.imgur.com/A06Ly.png' alt=''/>
<img class='diddy' src='http://i.stack.imgur.com/ZN4KS.png' alt=''/>
</div>
Using transform
:
I believe the first alternative is easier, however there is another way to use the property translate
(translateY
and translateX
). You can do as in the previous example, use position:absolute
center both images, one superimposing the other and use transform
to change the position of the image vertically/horizontally in relation to its upper left point.
Useful links (both deal with transform
in general, not only translate
):
- Transform: Mozilla Developer Network
- Transform: W3C
I hope the picture makes it clearer...
A picture (#vermelha
) and another image (#azul
).
And finally the sample code:
.wrapper {
border: 2px solid #ccc;
position: relative;
height: 200px;
width: 500px
}
.dixie, .diddy {
position: absolute;
top: 10px;
left: 180px
}
.dixie {
transform: translateX(40px);
max-width: 120px
}
.diddy {
transform: translateX(-40px);
max-width: 130px
}
<div class='wrapper'>
<img class='dixie' src='http://i.stack.imgur.com/A06Ly.png' alt=''/>
<img class='diddy' src='http://i.stack.imgur.com/ZN4KS.png' alt=''/>
</div>
What is the best way to list images on top of each other! This is both in HTML and CSS!
– Wesley Redfield