使用Tailwind CSS来精确叠加两个相同大小的图像并使它们按相同比例缩放。

huangapple go评论54阅读模式
英文:

TailwindCSS to exactly overlay two same-sized images and have them scale the same

问题

我有两个尺寸完全相同的图像,我希望将它们叠加在彼此上方(在z轴方向),并使它们实现以下效果:

  1. 适应包含的div(如果有意义的话,例如页眉),以及
  2. 当它们包含的div缩放时,以完全相同的方式缩放。

有点像在页眉中放置一个徽标,并且随着页面变窄,它应该缩放,只是现在有两个图像。

如果我的术语不准确,请原谅。

我有类似以下的东西:

<div class="inline-block">
   <img src="/i1.png" class="XXX" />
   <img src="/i2.png" class="XXX absolute top-0 left-0" />
</div>

但我不知道要在XXX中放什么。我尝试过h-full、w-full、w-fit等等,以及许多组合,但都没有成功。

我基本上只是希望它们像一个图像一样运行...

我想在TailwindCSS中实现它,请。

提前感谢您,
Ashley。

英文:

I have two images that are exactly the same size that I wish to overlay one on top of the other (in the z-direction) and have them:

  1. Fit inside the containing divs (if that makes sense, e.g. a header), and
  2. scale exactly the same way when their containing divs scales.

Something like having a logo in a header and as the page narrows it should scale, except that now there are two images.

Apologies if my terminology is not right there.

I have something like:

&lt;div class=&quot;inline-block&quot;&gt;
   &lt;img src=&quot;/i1.png&quot; class=&quot;XXX&quot; /&gt;
    &lt;img src=&quot;/i2.png&quot; class=&quot;XXX absolute top-0 left-0&quot; /&gt;
&lt;/div&gt; 

but don't know what to put in the XXX. I have tried h-full, w-full, w-fit,... and many combinations without success.

I basically just want them to behave as one image...

I would like to do it in TailwindCSS, please.

Thanks in advance,
Ashley.

答案1

得分: 1

你不需要为&lt;img&gt;元素添加任何其他类,但你需要在&lt;div&gt;父元素上应用position: relative,以便top: 0left: 0相对于父容器:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->
<div class="inline-block relative">
  <img src="https://picsum.photos/1920/1080" />
  <img src="https://picsum.photos/1920/1080?0" class="absolute top-0 left-0" />
</div> 

<!-- end snippet -->
英文:

You shouldn't need any other classes for the &lt;img&gt; elements, but you would need to apply position: relative on the &lt;div&gt; parent so that top: 0 and left: 0 will be relative to the parent container:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;

&lt;div class=&quot;inline-block relative&quot;&gt;
  &lt;img src=&quot;https://picsum.photos/1920/1080&quot; /&gt;
  &lt;img src=&quot;https://picsum.photos/1920/1080?0&quot; class=&quot;absolute top-0 left-0&quot; /&gt;
&lt;/div&gt; 

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月25日 22:05:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定