简体   繁体   English

Tailwind CSS:在图像 hover 上显示文本

[英]Tailwind CSS: display text on image hover

How can I show text on image hover, using Tailwind CSS: display text on image hover?如何使用 Tailwind CSS 在图像 hover 上显示文本:在图像 hover 上显示文本?

Here is my image?这是我的头像? I want text "mammals" to be displayed when user hovers image?我希望在用户悬停图像时显示文本“哺乳动物”?

<img src="/img/cat/categories/mammals.png" alt="mammals" class="max-w-full max-h-full">

I prefer to play around with position relative and absolute because it gives me more options to work with.我更喜欢使用 position 相对和绝对,因为它给了我更多的工作选择。 Check out this code Display text on hover查看此代码在 hover 上显示文本

<div class="w-64 h-64 bg-red-100 relative">
  <div class="absolute inset-0 bg-cover bg-center z-0" style="background-image: url('https://upload.wikimedia.org/wikipedia/en/3/3c/JumanjiTheNextLevelTeaserPoster.jpg')"></div>
  <div class="opacity-0 hover:opacity-100 duration-300 absolute inset-0 z-10 flex justify-center items-center text-6xl text-white font-semibold">Dwayne</div>
</div>

You can use the title atribute in your img tag.您可以在 img 标签中使用标题属性。

<img src="/img/cat/categories/mammals.png" alt="mammals" class="max-w-full max-h-full" title="mammals">

Please use this code works well请使用此代码效果很好

  <div class="relative ">
    <a class="absolute inset-0 z-10 bg-white text-center flex flex-col items-center justify-center opacity-0 hover:opacity-100 bg-opacity-90 duration-300">
      <h1  class=tracking-wider >Title</h1>
      <p  class="mx-auto">Description</p>
      </a>
    <a href="#" class="relative">
        <div class="h-48 flex flex-wrap content-center">
            <img src="/image_url" class="mx-auto  " alt="">
        </div>
    </a>
  </div>

在此处输入图像描述

Another idea is using group-hover class.另一个想法是使用group-hover class。

<div class="w-64 bg-red-100 relative group">
  <img src="https://upload.wikimedia.org/wikipedia/en/3/3c/JumanjiTheNextLevelTeaserPoster.jpg" />
  <div class="opacity-0 group-hover:opacity-100 duration-300 absolute left-0 bottom-0 right-0 z-10 flex justify-center items-end text-xl bg-gray-200 text-black font-semibold">Dwayne</div>
</div>

DEMO演示

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

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