Applying a text fade-in hover effect to an image within a table: What you need to know!

How can I implement a text fade in effect when hovering over images within a table? I've managed to achieve this for individual images, but when attempting to apply it to the table itself, the effect does not work. There seems to be an error in my arrangement. Is it not possible to accomplish this within a table?

Below is the code snippet:

table {
  text-align: center;
  margin-left: auto;
  margin-right: auto;
  border-spacing: 50px;
  border-collapse: separate;
}

td {
  max-width: 150px;
  max-height: 150px;
  text-align: center;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: #008CBA;
}

.container:hover .overlay {
  opacity: 1;
}

.text {
  color: black;
  font-size: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}
<table>
  <tr>
    <th>Cat 1</th>
    <th>Cat 2</th>
    <th>Cat 3</th>
  </tr>
  <tr>
    <div class="container">
      <td class="cat1"><img src="https://d17fnq9dkz9hgj.cloudfront.net/uploads/2012/11/91615172-find-a-lump-on-cats-skin-632x475.jpg" height="150px" width="150px"></td>
      <div class="overlay">
        <div class="text">Overlay Text</div>
      </div>
    </div>
    <td class="cat2"><img src="https://static1.squarespace.com/static/53a096cce4b00d7644776a0b/562b9546e4b016f977a96bd7/562b975fe4b01024eb5d7267/1445697377179/Shake_11_Lil+Bub.jpg?format=1500w" height="150px" width="150px"></td>
    <td class="cat3"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg" height="150px" width="150px"></td>
  </tr>
</table>

Fiddle: https://jsfiddle.net/megmun/f7esrn0t/

Answer №1

One major issue I encountered was incorrectly placing divs around TD elements, causing layout problems. Fortunately, the CSS was mostly correct but here's a helpful tip:

Position: absolute elements are contained within other positioned elements (relative, other absolute, and fixed), so for your TD to contain the absolute overlay it should be set to relative.

.grid {
  text-align: center;
  margin-left: auto;
  margin-right: auto;
  border-spacing: 50px;
  border-collapse: separate;
}

.grid td {
  position: relative;
  max-width: 150px;
  max-height: 150px;
  text-align: center;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: opacity .5s ease;
  -webkit-transition: opacity .5s ease;
  -moz-transition: opacity .5s ease;
  -o-transition: opacity .5s ease;
  background-color: #008CBA;
  z-index: 1000;
}

.grid td:hover .overlay {
  opacity: 1;
}

.text {
  color: black;
  font-size: 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);  
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}
<table class="grid">
  <tr>
    <th>Cat 1</th>
    <th>Cat 2</th>
    <th>Cat 3</th>
  </tr>
  <tr>

    <td>

      <img src="https://d17fnq9dkz9hgj.cloudfront.net/uploads/2012/11/91615172-find-a-lump-on-cats-skin-632x475.jpg" height="150px" width="150px" />

      <div class="overlay">
        <div class="text">Overlay Text</div>
      </div>
    </td>

    <td><img src="https://static1.squarespace.com/static/53a096cce4b00d7644776a0b/562b9546e4b016f977a96bd7/562b975fe4b01024eb5d7267/1445697377179/Shake_11_Lil+Bub.jpg?format=1500w" height="150px" width="150px" /></td>
    <td><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg" height="150px" width="150px" /></td>
  </tr>
</table>

Additional note:

Don't forget to handle vendor-specific properties like transition and transform in your CSS code as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Keeping the scroll in place on a Bootstrap4 modal while scrolling through content that is already at the top

On my Bootstrap 4 modal, I have encountered an issue with scrollable content. When I am at the top of the content and try to scroll downwards, nothing happens because I am already at the top. However, if I continue to hold the touch without releasing it, t ...

Having trouble logging in with the script, even though I have already set up the correct username and password in my

After purchasing the "Jqcm - Premium Responsive Quiz Engine" script, the author has disappeared and has not been responding to any questions for months. When attempting to log in to the backend with the provided username and password, a circling bar appea ...

How to assign a value to a textarea element in an HTML form?

I am struggling to populate a text area with the value using this code snippet... <?php $message = $_REQUEST['message']; ?> <br/><b>Description</b><br/> <TEXTAREA NAME="message" COLS=40 ROWS=6 value="<?=$messa ...

Adding a dynamic click event in HTML using IONIC 4

I've created a function using Regex to detect URL links and replace them with a span tag. The replacement process is working fine, but I'm facing an issue where when I include (click)="myFunction()" in the span, it doesn't recognize the cli ...

Height specification in Tailwind Grid

Hi there! I'm currently working on implementing a grid system in NextJS using Tailwind CSS. However, I've hit a roadblock when it comes to automatically sizing the grids to fit the parent element. Let me illustrate my issue with two images below ...

Steps for writing code to append and execute code snippets using values from a JSON dataset

My attempt to fetch the result through this code is I am looking to display 'Y' or 'N' in the columns Mon, Tue, Wed, Thu, Fri, Sat, Sun I am facing issues fetching the 'run' value from the json data. I have used user.days to ...

When using jQuery's `.click()` method on an element to collapse it with the 'show' parameter set to false, the disabling action will only take effect after the document

When you first run the program and click anywhere on the body, it activates the collapse element. I want it to only collapse the accordion on click, not show it immediately. Currently, it will deactivate only after it is hidden once. HTML <!DOCTYPE ht ...

Submitting a form using AJAX only works with the click event and not the submit event. However, when using the click event, the form fields are not validated

I am facing an issue with my classic form submission using Ajax. Currently, it only works with $("button").click(function(), which results in the fields not being validated properly. <script type="text/javascript"> $(document).ready(function(){ ...

How to Conceal File Extensions with .htaccess

Can anyone provide guidance on the best way to hide .php extensions using HTAccess, even for sub-folders? I have attempted various solutions from previous answers but none seem to work. ...

Adjust the button's hue upon clicking it

The current function is operational. Upon pressing the button, it toggles between displaying "click me" and "click me again". However, I desire the button to appear blue when showing "click me" and red when displaying "click me again". <!DOCTYPE html ...

Guide to removing text color from a background with css or javascript

Is there a way to achieve the effect shown in this image on my HTML page: http://prntscr.com/b912c0? I've attempted using mix-blending modes, but haven't been successful in recreating the desired result. Any tips or tricks for accomplishing this ...

Issue with Bootstrap cards overlapping while searching

On my Bootstrap 4 page, I've incorporated cards that can be searched, displaying only those with matching titles. However, I am facing two main issues. Firstly, when two matching cards are on the same row, they overlap. Secondly, if a matching card is ...

Is it possible to send an email with an attachment that was generated as a blob within the browser?

Is there a way to attach a file created in the browser as a blob to an email, similar to embedding its direct path in the url for a local file? The file is generated as part of some javascript code that I am running. Thank you in advance! ...

Stretch element to reach the edge of the container

I need help with positioning an input and a textarea inside a container that has a height of 52.5vh. I want the textarea to start right below the input and expand all the way to the end of the container. How can I achieve this layout? Both elements should ...

Ways to require text entry only when specific radio buttons are selected in jQuery

Currently, I am working on a project using JSP and have created an HTML form with a Process button at the top. When this button is clicked, a form is displayed containing two radio buttons - TestClient and TestServer. The form also includes a Submit butto ...

When I try to access a view using the controller, the CSS does not seem to

Currently, I am developing an asp.net mvc application and the index.cshtml is set as the default page. When I run the application, the startup page displays with the applied CSS without any issues. However, when I access this page from the controller "/h ...

The styling of divIcons in React Leaflet Material UI is not applied as expected

When using divIcon in React Leaflet to render a custom Material UI marker with a background color prop, I noticed that the background style is not being applied correctly when the marker is displayed in Leaflet. Below you can find the code for the project ...

Tips for positioning a modal in the center specifically for zoomed-in mobile devices

I created a modal that uses the following function to center itself: center: function() { var top=Math.max($window.height() - $modal.outerHeight(),0) / 2; var left=Math.max($window.width() - $modal.outerWidth(),0) / 2; $modal.css({ ...

Issue with Safari: Unable to trigger CSS :hover when tapping SVG element

On an iOS device in Safari, tapping this SVG element does not trigger the transition animation to fade in the replacement <g id="training-sub-menu">. The animation is confirmed to work, as there is a strange behavior where long-pressing whe ...

"Revamp the appearance of the div element upon clicking elsewhere on the

function replace( hide, show ) { document.getElementById(hide).style.display="none"; document.getElementById(show).style.display="flex"; } @import url('https://fonts.googleapis.com/css2?family=Allerta+Stenci ...