How do I position one image on top of another in HTML?

HtmlCssOptimizationGraphics

Html Problem Overview


I'm a beginner at rails programming, attempting to show many images on a page. Some images are to lay on top of others. To make it simple, say I want a blue square, with a red square in the upper right corner of the blue square (but not tight in the corner). I am trying to avoid compositing (with ImageMagick and similar) due to performance issues.

I just want to position overlapping images relative to one another.

As a more difficult example, imagine an odometer placed inside a larger image. For six digits, I would need to composite a million different images, or do it all on the fly, where all that is needed is to place the six images on top of the other one.

Html Solutions


Solution 1 - Html

Ok, after some time, here's what I landed on:

.parent {
  position: relative;
  top: 0;
  left: 0;
}
.image1 {
  position: relative;
  top: 0;
  left: 0;
  border: 1px red solid;
}
.image2 {
  position: absolute;
  top: 30px;
  left: 30px;
  border: 1px green solid;
}

<div class="parent">
  <img class="image1" src="https://via.placeholder.com/50" />
  <img class="image2" src="https://via.placeholder.com/100" />
</div>

As the simplest solution. That is:

Create a relative div that is placed in the flow of the page; place the base image first as relative so that the div knows how big it should be; place the overlays as absolutes relative to the upper left of the first image. The trick is to get the relatives and absolutes correct.

Solution 2 - Html

This is a barebones look at what I've done to float one image over another.

img {
  position: absolute;
  top: 25px;
  left: 25px;
}
.imgA1 {
  z-index: 1;
}
.imgB1 {
  z-index: 3;
}

<img class="imgA1" src="https://via.placeholder.com/200/333333">
<img class="imgB1" src="https://via.placeholder.com/100">

Source

Solution 3 - Html

Here's code that may give you ideas:

<style>
.containerdiv { float: left; position: relative; } 
.cornerimage { position: absolute; top: 0; right: 0; } 
</style>

<div class="containerdiv">
	<img border="0" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" alt=""">
	<img class="cornerimage" border="0" src="http://www.gravatar.com/avatar/" alt="">
<div>

JSFiddle

I suspect that Espo's solution may be inconvenient because it requires you to position both images absolutely. You may want the first one to position itself in the flow.

Usually, there is a natural way to do that is CSS. You put position: relative on the container element, and then absolutely position children inside it. Unfortunately, you cannot put one image inside another. That's why I needed container div. Notice that I made it a float to make it autofit to its contents. Making it display: inline-block should theoretically work as well, but browser support is poor there.

EDIT: I deleted size attributes from the images to illustrate my point better. If you want the container image to have its default sizes and you don't know the size beforehand, you cannot use the background trick. If you do, it is a better way to go.

Solution 4 - Html

One issue I noticed that could cause errors is that in rrichter's answer, the code below:

<img src="b.jpg" style="position: absolute; top: 30; left: 70;"/>

should include the px units within the style eg.

<img src="b.jpg" style="position: absolute; top: 30px; left: 70px;"/>

Other than that, the answer worked fine. Thanks.

Solution 5 - Html

You can absolutely position pseudo elements relative to their parent element.

This gives you two extra layers to play with for every element - so positioning one image on top of another becomes easy - with minimal and semantic markup (no empty divs etc).

markup:

<div class="overlap"></div>

css:

.overlap
{
    width: 100px;
    height: 100px;
    position: relative;
    background-color: blue;
}
.overlap:after
{
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    top: 5px;
    left: 5px;
    background-color: red;
}

Here's a LIVE DEMO

Solution 6 - Html

It may be a little late but for this you can do:

enter image description here

HTML

<!-- html -->
<div class="images-wrapper">
  <img src="images/1" alt="image 1" />
  <img src="images/2" alt="image 2" />
  <img src="images/3" alt="image 3" />
  <img src="images/4" alt="image 4" />
</div>

SASS

// In _extra.scss
$maxImagesNumber: 5;

.images-wrapper {
  img {
    position: absolute;
    padding: 5px;
    border: solid black 1px;
  }

  @for $i from $maxImagesNumber through 1 {
    :nth-child(#{ $i }) {
      z-index: #{ $maxImagesNumber - ($i - 1) };
      left: #{ ($i - 1) * 30 }px;
    }
  }
}

Solution 7 - Html

Inline style only for clarity here. Use a real CSS stylesheet.

<!-- First, your background image is a DIV with a background 
     image style applied, not a IMG tag. -->
<div style="background-image:url(YourBackgroundImage);">
	<!-- Second, create a placeholder div to assist in positioning 
	     the other images. This is relative to the background div. -->
	<div style="position: relative; left: 0; top: 0;">
		<!-- Now you can place your IMG tags, and position them relative 
		     to the container we just made -->	 
		<img src="YourForegroundImage" style="position: relative; top: 0; left: 0;"/>
	</div>
</div>

Solution 8 - Html

The easy way to do it is to use background-image then just put an <img> in that element.

The other way to do is using css layers. There is a ton a resources available to help you with this, just search for css layers.

Solution 9 - Html

Set background-size cover. Then wrap your div with another div now set max-width on parent div.

<div style="max-width:100px">
  <div style="background-image:url('/image.png'); background-size: cover; height:100px; width:100px; "></div>
</div>

Solution 10 - Html

You could use CSS-Grid, which is a very convenient solution if you want to stack, overlap content. First you need to define your grid. Inside that grid, you "tell" your img-tags where to be places within that grid. If you define them to be at the same start of the grid, they will be overlapped. In the following example two images are overlapped, one is below them.

<div style="display: grid; grid-template-columns: [first-col] 100%; grid-template-rows: [first-row] 300px">
  <img src="https://fakeimg.pl/300/" style="grid-column-start: first-col; grid-row-start: first-row">
  <img src="https://fakeimg.pl/300/" style="grid-column-start: first-col; grid-row-start: first-row">
  <img src="https://fakeimg.pl/300/">
</div>

You can find a very good explanation of CSS-Grid here.

Solution 11 - Html

@buti-oxa: Not to be pedantic, but your code is invalid. The HTML width and height attributes do not allow for units; you're likely thinking of the CSS width: and height: properties. You should also provide a content-type (text/css; see Espo's code) with the <style> tag.

<style type="text/css"> 
.containerdiv { float: left; position: relative; } 
.cornerimage { position: absolute; top: 0; right: 0; } 
</style>

<div class="containerdiv">
    <img border="0" src="http://www.gravatar.com/avatar/" alt="" width="100" height="100">
    <img class="cornerimage" border="0" src="http://www.gravatar.com/avatar/" alt="" width="40" height="40">
<div>

Leaving px; in the width and height attributes might cause a rendering engine to balk.

Solution 12 - Html

Create a relative div that is placed in the flow of the page; place the base image first as relative so that the div knows how big it should be; place the overlays as absolutes relative to the upper left of the first image. The trick is to get the relatives and absolutes correct.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrrichterView Question on Stackoverflow
Solution 1 - HtmlrrichterView Answer on Stackoverflow
Solution 2 - HtmlEspoView Answer on Stackoverflow
Solution 3 - Htmlbuti-oxaView Answer on Stackoverflow
Solution 4 - HtmlWezView Answer on Stackoverflow
Solution 5 - HtmlDanieldView Answer on Stackoverflow
Solution 6 - HtmlElkin AnguloView Answer on Stackoverflow
Solution 7 - HtmlFlySwatView Answer on Stackoverflow
Solution 8 - HtmlChris BartowView Answer on Stackoverflow
Solution 9 - HtmlHowdyView Answer on Stackoverflow
Solution 10 - HtmlFzumView Answer on Stackoverflow
Solution 11 - HtmlSören KuklauView Answer on Stackoverflow
Solution 12 - HtmlPallavi SinghView Answer on Stackoverflow