如何在HTML中将一个图像放置在另一个图像之上?

HTML CSS

Gil猿

2020-03-24

我是Rails编程的初学者,试图在页面上显示许多图像。有些图像要放在其他图像之上。为简单起见,假设我要一个蓝色正方形,在蓝色正方形的右上角有一个红色正方形(但在角落不紧)。由于性能问题,我试图避免进行合成(使用ImageMagick等)。

我只想相对于彼此放置重叠的图像。

举一个更困难的例子,想象一下将里程表放在更大的图像中。对于六位数字,我将需要合成一百万个不同的图像,或者即时进行处理,其中所需要做的就是将六个图像放置在另一个图像之上。

第3591篇《如何在HTML中将一个图像放置在另一个图像之上?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

10个回答
神乐 2020.03.24

创建一个放置在页面流中的相对div;首先将基础图像作为相对图像放置,以便div知道应该有多大;将叠加层相对于第一张图片的左上角作为绝对值放置。诀窍是使亲戚和绝对人正确。

西里Near 2020.03.24

可能有些晚了,但是您可以这样做:

在此处输入图片说明

的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>

萨斯

// 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;
    }
  }
}
番长樱梅 2020.03.24

@ buti-oxa:不必脚,但您的代码无效。HTML widthheight属性不允许使用单位。您可能会想到CSS width:height:属性。您还应该提供text/css带有<style>标签的content-type(;请参见Espo的代码)

<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>

离开px;widthheight属性可能导致的渲染引擎在所不惜。

古一十三 2020.03.24

内联样式仅是为了此处清晰。使用真实的CSS样式表。

<!-- 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>
十三老丝 2020.03.24

您可以pseudo elements相对于其父元素绝对定位

这为您提供了两个额外的图层供每个元素使用-因此将一个图像放置在另一个图像上变得容易-且具有最少的语义标记(没有空的div等)。

标记:

<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;
}

这是现场演示

飞云 2020.03.24

以下代码可能会给您一些想法:

<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

我怀疑Espo的解决方案可能不方便,因为它要求您绝对放置两个图像。您可能希望第一个在流程中定位自己。

通常,CSS是一种自然的方法。您将position:relative放置在容器元素上,然后将子元素绝对放置在其中。不幸的是,您不能将一个图像放入另一个图像。这就是为什么我需要容器div。请注意,我将其设为浮动对象以使其自动适应其内容。使其显示:从理论上讲,内联块也应该工作,但是那里的浏览器支持很差。

编辑:我从图像中删除了大小属性,以更好地说明我的观点。如果您希望容器图像具有默认大小,并且事先不知道该大小,则不能使用背景技巧如果这样做,则是更好的方法。

猴子村村 2020.03.24

最简单的方法是使用背景图片,然后在该元素中放入<img>。

另一种方法是使用css层。有大量资源可以帮助您解决此问题,只需搜索CSS图层即可

小宇宙 2020.03.24

这是我为使一个图像浮动到另一个图像所做的准系统研究。

img {
  position: absolute;
  top: 25px;
  left: 25px;
}
.imgA1 {
  z-index: 1;
}
.imgB1 {
  z-index: 3;
}
<img class="imgA1" src="https://placehold.it/200/333333">
<img class="imgB1" src="https://placehold.it/100">

资源

小小阿飞 2020.03.24

好的,过了一段时间,这就是我的目标:

.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://placehold.it/50" />
  <img class="image2" src="https://placehold.it/100" />
</div>

作为最简单的解决方案。那是:

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.

猴子 2020.03.24

我注意到可能会导致错误的一个问题是,在rrichter的答案中,以下代码:

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

应该在样式中包含px单位,例如。

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

除此之外,答案很好。谢谢。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android