简体   繁体   English

如何将图像放入另一个图像

[英]How to put image into another image

I have an empty-white image1 with size specified with CSS and I want to put another image2 inside it which has no styling.我有一个用 CSS 指定大小的空白色图像 1,我想在其中放置另一个没有样式的图像 2。 So my problem is when the second image is smaller or bigger than image1, I guess my question is how to adjust image2 to fully fit with image1.所以我的问题是当第二个图像小于或大于 image1 时,我想我的问题是如何调整 image2 以完全适合 image1。

I should use CSS or javascript(if possible) only.我应该只使用 CSS 或 javascript(如果可能)。

Any help appreciated.任何帮助表示赞赏。

 <div style="position: relative; left: 0; top: 0;">
  <img src="a.jpg" style="position: relative; top: 0; left: 0;"/>
  <img src="b.jpg" style="position: absolute; top: 30px; left: 70px;"/>
</div>
<style type="text/css"> 
.imgA1 { position:absolute; z-index: 1; } 
.imgB1 { position:absolute; z-index: 3; } 
</style>

<img class=imgA1 src="imageA.jpg">
<img class=imgB1 src="imageB.jpg"> 

Use max-width and max-height for both and wrap them in a div!两者都使用max-widthmax-height并将它们包装在一个 div 中! In my example, whatever the size, (first, child is smaller than the parent, second, child is larger than the parent), both fit accordingly.在我的示例中,无论大小(首先,孩子比父母小,其次,孩子比父母大),两者都相应地适合。 And it is in pure CSS.它是纯 CSS 的。

 img {max-width: 100%; max-height: 100%; display: block;} .parent, .child {position: absolute;} #p1 {width: 350px; height: 150px; left: 0; top: 0;} #c1 {width: 350px; height: 150px; left: 0; top: 0;} #p2 {width: 350px; height: 150px; left: 0; top: 200px;} #c2 {width: 350px; height: 150px; left: 0; top: 200px;}
 <div id="p1" class="parent"><img src="http://placehold.it/350x150" alt=""></div> <div id="c1" class="child"><img src="http://placehold.it/150x150" alt=""></div> <div id="p2" class="parent"><img src="http://placehold.it/350x150" alt=""></div> <div id="c2" class="child"><img src="http://placehold.it/450x450" alt=""></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM