Transform the text upon hovering over the image, with the text positioned separate from the image

I'm looking for a solution where hovering over images in a grid area will change the text on the left side of the screen to display a predefined description of that image. I've been working on this problem for hours and would appreciate any help.

The goal is to have the text displayed in the same area. For example, when hovering over a lake image, the description of the lake should be displayed in a designated box. Similarly, if hovering over a duck image, the description of the duck should replace the previous one in the same box.

Is there a way to use onmouseover events in order to dynamically change the text based on what is being hovered over? For instance, when a duck image is hovered over, the text variable "DuckImage = 'A duck is an animal';" should be displayed in a designated box.

Answer №1

If you're looking to create a grid of images with text that appears on hover, you can achieve this using CSS and HTML without the need for JavaScript. Here's how it works:

.grid {
  /* Set padding to create space for descriptions */
  padding-left:50%;
  
  /* Create a basic grid layout for the images */
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 10px;
}

.grid img {
  /* Ensure images stay within their grid cells */
  max-width:100%;
}

.grid a p {
  /* Limit description width to half of the screen */
  width:50%;
  
  /* Fixed position for top left corner */
  position:fixed;
  top:0;
  left:0;
  
  /* Hide description by default */
  display:none;
}

.grid a:hover p {

  /* Show description on hover */
  display:block;
}
<div class="wrapper">

  <div class="grid">
    <a href="#">
      <p>Predefined text for blue image</p>
      <img src="https://via.placeholder.com/350/00f.png" alt="blue" />
    </a>
    <a href="#">
      <p>Predefined text for red image</p>
      <img src="https://via.placeholder.com/350/f00.png" alt="red" />
    </a>
    <a href="#">
      <p>Predefined text for green image</p>
      <img src="https://via.placeholder.com/350/0f0.png" alt="green" />
    </a>
    <a href="#">
      <p>Predefined text for yellow image. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
      <img src="https://via.placeholder.com/350/ff0.png" alt="yellow" />
    </a>
  </div>
</div>

Answer №2

To achieve this, one method could involve placing the grid and message container within a parent container and utilizing the general sibling selector. The example provided below may not be perfect, but it does function. Another approach would be to utilize JavaScript by employing onmouseover for the grid cells and potentially using data attributes to enhance dynamism:

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        ...
        // CSS styling here
        ...
    </head>
    <body>

       <div class="grid-area">
         // Grid items here
       </div>

</body>
</html> 

The following example utilizes JavaScript. This is one potential method that can be implemented. Data attributes have been utilized to allow for customization with values and style adjustments in CSS. Alternatively, you could also use setAttribute('data', "your attribute name", ...) along with other techniques:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    ...
    // Additional CSS styling
    ...
</head>
<body>
    <div class="center">
        // Image elements with data-columns attributes
    </div>
     <div id="outside-box"></div>
<script>
    // JavaScript logic to interact with the images
</script>
</body>
</html>

Answer №3

<img src="Images/duck.png" width="450" height="auto" onmouseover="document.getElementsByName('HoverOverInfo2')[0].innerHTML = 'A Duck'" onmouseout="document.getElementsByName('HoverOverInfo2')[0].innerHTML = ' '" />

    <img src="Images/car.png" width="450" height="auto" onmouseover="document.getElementsByName('HoverOverInfo2')[0].innerHTML = 'A Car'" onmouseout="document.getElementsByName('HoverOverInfo2')[0].innerHTML = ' '" />

Another location

<h4 class="w3-bar-item"><b>Description</b></h4>
    <div style="margin-left:8px">
   <p id="HoverOverInfo" name=></p>

This solution was spot-on for my needs!

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

"Within the node.js framework, the search/query section of the URL appears

I am currently working on a website (client + server) that both operate from the same machine. Despite not encountering any issues in Chrome's developer tools, I am struggling to identify the source of a problem. My dilemma is with trying to POST a s ...

Eliminate spaces between divs without any margins or padding

In my HTML code, I have two divs with different classes - mini-date-holder and mini-event-holder. Even though both of these divs are set to have no margin or padding, they are still displaying with a gap between them. I'm struggling to understand why ...

Pop-up message on card selection using JavaScript, CSS, and PHP

I have a total of 6 cards displayed in my HTML. Each card, when clicked, should trigger a modal window to pop up (with additional information corresponding to that specific card). After spending a day searching for a solution online, I've come here s ...

Is it possible to modify the dropdown menu so that it opens on the right side instead of using a select tag?

Is there a way to make the drop-down from the select tag open to the right side(drop-right)? <label for="ExpLabel">Select Message Expiry:</label> <select name="ExpSelect" id="Expiry"> <option value="ExpiryDate">1 Day</opt ...

What is the correct way to run javascript on a DOM element when ng-show is triggered?

For those who prefer shorter explanations, there's a TLDR at the end. Otherwise, here's a detailed breakdown. I have a form that consists of multiple inputs organized into different "pages" using ng-show. What I aim to achieve is when ng-show r ...

Use custom styles or CSS for the file input element: <input type="file">

Can CSS be used to achieve consistent styling for <input type="file"> in all browsers? ...

Prevent selecting dates on <input type="datetime-local"> fields

How can I restrict date selection? I am working on implementing a date picker for clients to choose appointment dates. To prevent clients from selecting dates that are already booked (stored in a database and using Vue), I want to set limitations. Note: I ...

Adding a uniform header and footer across all pages simultaneously in HTML

I am currently working on a project where I want to apply the same header and footer design from my homepage to all pages. However, I need a method that allows me to update the header and footer in one place, so that any changes I make will automatically r ...

Limit selection choices in select element

Similar Question: Prevent select dropdown from opening in FireFox and Opera In my HTML file, I have a select tag that I want to use to open my own table when clicked. However, the window of the Select tag also opens, which is not desirable. Is there a ...

Is there a way to keep my fixed footer container from moving while zooming in and out on a webpage

Is there a way to prevent the bottom text from shifting when zoomed in or out on the page? The rest of the content remains stable, but due to using position:absolute for this section to stay at the bottom of another div (content), it causes movement. #con ...

Assistance Needed with XPATH and CSS for Selenium Automation

Can anyone assist me in finding the appropriate XPATH/CSS locator to extract text from the structure below? <div class="page-header song-wrap"> <div class="art solo-art"> <div class="meta-info"> <h1 class="page-title"> Zehnaseeb I ...

Transforming a dynamic HTML layout into a non-responsive one: "RESPONSIVE NO MORE"

I'm currently working with an HTML document that has a responsive design. However, I now need to make it non-responsive. Can someone advise me on the most efficient and fastest way to do this? I attempted using min-width for both html and body, but ...

Shifting hues of dots within a grid according to the passing of time

As a newcomer to the world of coding, I have conceptualized an idea for a visually appealing clock. My goal is to visually represent the passage of time throughout the day. To achieve this, I have devised a grid system where each dot on the grid represents ...

Adjust the border color of a TextField in react/material ui based on whether props are required

If a value is required, the TextField border color will be red. Once a value is entered, the red border will disappear. Check out the images by clicking on the links below: Entered Value Without Value with Required field ...

A glitch occurred while attempting to load content dynamically onto an HTML page using Ajax

Attempting to dynamically load content into an HTML div is causing issues for me. Whenever I try to do so, an error pops up: Syntax error HOME. (this is the expected visible content within the div). HTML: Navigation bar: <li><a href="#" onclick= ...

Generate a blank image using the canvas.toDataURL() method

I've been attempting to take a screenshot of this game, but every time I try, the result is just a blank image. var data = document.getElementsByTagName("canvas")[0].toDataURL('image/png'); var out = document.createElement('im ...

Serialize a series of select boxes to optimize for AJAX POST requests

To better explain my issue, let's consider a simple example: Imagine I have a form that collects information about a user: <form action="#" method="post" id="myform"> <input type="text" name="fname" /> <input type="text" name= ...

Adjust the size of the <textarea> to match the height of the table cell

Below is the code I am using to generate a table containing an image along with a <textarea>: <table border="1" style="border-color: #a6a6a6" cellpadding="4" cellspacing="0" width="702">\ <col width="455"> <col width="230"> ...

Drawing on Canvas with Html5, shifting canvas results in significant issues

I've been working on developing an HTML5 drawing app, and while I have all the functionality sorted out, I'm facing challenges during the design phase. My main issue is centered around trying to make everything look visually appealing. Specifical ...

The DataGrids effortlessly expanded to accommodate their parents without the need for a horizontal scrollbar, showcasing their full capacity

After conducting numerous tests on simpler models, I have come to realize that the display of DataGrids Pro has been altered in MUI v6, rather than being a result of something I had broken initially. My workplace utilizes multiple DataGrids with columns h ...