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

Convert JavaScript code to Google Appscript

Looking for guidance on incorporating Javascript code into my Google Appscript. Here is the code snippet: I have a separate Stylesheet.html file saved. Can someone advise me on how to invoke the functions within the html file? <script> //google. ...

Utilizing property validation in your codebase

How can I set up my code to display a warning in the console if the value of the Prop is not a string? The player-name prop should always be a string. If this: <greet :player-name="5"></greet> contains a number, I want my code below to generat ...

Why is a question mark added to the URL when the login button is clicked

I've encountered an issue where clicking this code for the first time redirects me to localhost:8080//? instead of localhost:8080//admin.html. Any idea why this is happening? $("#admin-login").click(function(){ var data = {"username": $("#admin ...

Guide to placing the Drawer component beneath the AppBar in Material-UI

Exploring the capabilities of the Material UI drawer component, I'm looking to position the drawer below the appbar. My goal is to have the hamburger icon toggle the drawer open and closed when clicked: opening the drawer downwards without moving the ...

Using Vue and vue-multiselect to create interactive options based on the selected language

I'm currently developing a Vue website with multilingual support. The chosen language is stored in a Vuex store and accessed through the computed property lang, like so: lang(){ return this.$store.state.lang } I use this lang property in v-if cond ...

Retain the chosen values even after submitting the form

Consider the following form: <form method="get" action=""> <select name="name"> <option value="a">a</option> <option value="b">b</option> </select> <select name="location"> <opt ...

AngularJS has the ability to handle the value of one input based on the value of another input

How can I ensure that if one input has a value, the other must also be filled in AngularJS? I'm looking for the best approach to handle this requirement in an angular way. Any suggestions? Below is my code: <div class="form-group recipe_item" ng ...

Is Amazon altering the names of their CSS selectors and HTML elements on the fly?

For my Amazon.es web scraper built with Selenium, I am using a CSS selector to determine the total number of pages it will iterate through. However, the selector name seems to change dynamically and I must update it daily. As someone not well-versed in H ...

Filling a table cell with a div that spans 100% height

I am currently working with Bootstrap and I am attempting to create three columns filled with text overlaying a rectangle. The squares within the columns should have equal height and there needs to be spacing between them. Through the use of display:table ...

What is causing Firefox to display an additional line in this section?

After much effort, I've been attempting to eliminate the extra spacing on this page in Firefox: Do you see the menu? If you compare it to Chrome, you'll notice that the menu is positioned too low beneath the background, giving the layout a broke ...

Issue with $_SERVER['PHP_SELF'] not functioning properly

Apologies if this question has been asked before, but after searching extensively I couldn't find a solution. Therefore, I am posting here... The issue is that I'm trying to submit values on the same page (using jQuery Mobile UI) and I have used ...

"Enhance your website with a unique Bootstrap 5 carousel featuring multiple

As a beginner in Bootstrap, I am currently working on an ecommerce template to learn about Bootstrap 5. I am interested in creating a carousel that displays multiple slides at once, like a products slider with left and right arrows. Is this possible in Bo ...

Showing a confirmation message to the user upon clicking outside of a specific section

On my webpage, I have a section for creating content, as well as a top bar and sidebar with internal links (both controlled by ng controllers). I am looking to implement a confirmation message that will appear if the user tries to leave the page while in t ...

Is it possible for ng-model to verify its own value?

Recently, I've been experimenting with Angular and found myself facing a dilemma. Is it possible to apply a different CSS style based on whether the value of ng-model is greater than 0? `<span ng-model="users2" ng-hide="!myVar" ng-class="{'te ...

What is the best way to implement a CSS transition for styles that are dynamically created by React?

I have a situation where I am using a button component that is styled based on a theme provided by a context: The code in Button.js looks like: () => { const theme = useContext(themeContext); // { primaryColor: "blue" } return <button className ...

Overflow due to cascading style sheets

THE ANSWER TO THIS QUESTION HAS BEEN PROVIDED In regards to this particular div that contains two unordered lists, I am seeking a solution where these lists can be positioned side by side instead of above or below each other. I have attempted various met ...

`CSS Animation fails to run in Internet Explorer and Microsoft Edge browsers`

My issue with the CSS animation is that while the opacity animates properly, the translate effect does not seem to work as expected. You can see the problem here: https://jsfiddle.net/bLxb8k3s/ It appears that the reason for this issue is because IE and ...

Is it possible in Javascript to trace the origins of a particular element's property inheritance for debugging purposes?

I'm currently dealing with an issue where the computed style font-size of a particular element is "16px". I've been attempting to pinpoint where in the CSS or JavaScript this font size setting is coming from, specifically within one of its parent ...

How to place a Div element in the middle of a webpage

My webpage background features an image of old Tbilisi, and here is the code I am using: <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <style> #links { margin: 0 auto; ...

Angular causes HTML Dropdown to vanish once a null value is assigned

In my scenario, I have two variables named power and mainPower. Both of these variables essentially represent the same concept, with mainPower storing an ID of type Long in the backend, while power contains all attributes of this data transfer object. The ...