Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let an HTML image overlap another

Tags:

html

css

<div class="mainRunner">
    <img src="../App_Themes/Default/images/a.gif" />
    <img src="../App_Themes/Default/images/b.gif" />
</div>

I want b.gif to overlap a.gif rather than go on a new line - how can I achieve this?

like image 392
zomb Avatar asked Mar 03 '11 14:03

zomb


People also ask

How do you overlap images in HTML?

We're applying a negative right margin on a floated left element that allows content to overlap. -100% is equal to the width of the container so it shifts the image to the left and allows the bottom image to render beneath it as if it's not in the DOM. Codepen here: HTML.

How do I overlap one image to another?

Open your base image in Photoshop, and add your secondary images to another layer in the same project. Resize, drag, and drop your images into position. Choose a new name and location for the file. Click Export or Save.

How do you overlap something in HTML?

You can apply the CSS position:relative; and then specify an offset such as top:-50px;left:-20px; which would shift the element 20 pixels to the left and 50 pixels up. You can also specify right and bottom and use positive or negative values.

How do you put two pictures on top of each other in HTML?

Wrap the images in a <div> with the overlay image first and the actual image second, and can set the css of the div to position: relative . The two images can then be given the css {position: absolute; top: 0; left: 0;} . If you really want to be safe, you can set the z-index of each image. Save this answer.


2 Answers

You would have to use positioning and z-index to get this to work, with changing the images to block level elements and adding a class:

.mainRunner {
  border: 1px solid #000;
}

.img1 {
  border: 1px solid #f00;
  position: relative;
  z-index: 2;
}

.img2 {
  border: 1px solid #0f0;
  position: relative;
  z-index: 1;
  top: -12px;
  left: -12px;
}
<div class="mainRunner">
  <img class="img1" src="http://t0.gstatic.com/images?q=tbn:ANd9GcTG4mTuuZmylqn_qqviXFh5EPLD_DTsXMIjXT-4XJM0QPtJxw7lXw&t=1" />
  <img class="img2" src="http://t0.gstatic.com/images?q=tbn:ANd9GcTG4mTuuZmylqn_qqviXFh5EPLD_DTsXMIjXT-4XJM0QPtJxw7lXw&t=1" />
</div>
like image 157
Kyle Avatar answered Oct 03 '22 17:10

Kyle


Make sure the the containing element (wrapping div) has relative positioning applied.

div.mainRunner { position:relative;}

After this you can do one of the following.

  1. Apply a class name to each image so you can map to it with absolute positioning.

    div.mainRunner img.classname { position:absolute; top:__; left:__;}

Lastly apply z-index to the image class.

div.mainRunner img.classname { position:absolute; top:__; left:__; z-index:50;}

And for the second image;

div.mainRunner img.classname { position:absolute; top:__; left:__; z-index:51;}

If you have no control over applying classes to the images then do this (on the assumption that only 2 images will be in this div;

div.mainRunner img.classname:first-child { position:absolute; top:__; left:__; z-index:50;}
div.mainRunner img.classname { position:absolute; top:__; left:__; z-index:51;}
like image 31
Desi Avatar answered Oct 03 '22 15:10

Desi