Definitions that are displayed dynamically when hovering over a particular element

I am seeking a way to implement popup definitions that appear when a div is hovered over. My website showcases detailed camera specifications, and I want users to see a brief definition when they hover over the megapixel class called '.mp'. One of my main goals is to be able to simply add the class '.mp' to any existing div or class, so that a popup will appear above it with the relevant information. This flexibility will allow me to reuse definitions in multiple locations on the site.

I am looking for an efficient solution as my current method is both inefficient and cumbersome.

In my site layout, the specifications are displayed using PHP and fetched from a database. Each specification is wrapped in '.spec_item'. The names of the specs are stored in an array which can be dynamically added to each div using a loop:

$sensor_db = array("mp", "sensor_size", "sensor_type", "light_sensitivity");

For example, this generates divs like:

<div class="spec_item mp">
    12.9
</div>
<div class="spec_item sensor_size">
    0.95X
</div>
<div class="spec_item sensor_type">
    APS-C
</div>

When a user hovers over any div with the class '.mp', I want the corresponding definition to display below it in a small popup, similar to the effect shown in my mockup image. I could achieve this by storing all definitions in another array and toggling between display none/block on hover. However, this approach would not allow me to easily apply the '.mp' class to any div and have the definition automatically show up, which is my desired functionality.

Any guidance or assistance on how I can achieve this would be greatly appreciated.

Answer №1

Perhaps this solution could be of assistance to you. It may require generating the appropriate HTML within your PHP script.

.tooltip-container {
  width: 200px;
  position: absolute;
  display: none;
  margin-top: 12px;
}

.arrow-up {
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 5px 12px 5px;
  border-color: transparent transparent #a6a6a6 transparent;
  position: absolute;
  top: -12px;
  left: 15%;
}

.tooltip {
  border: thin solid darkgray;
  padding: 10px;
}

h5 {
  margin: 0;
  text-align: left;
}

.mp {
  width: 200px;
  height: 50px;
  border-radius: 5%;
  background-color: red;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  position: relative;
}

.mp:hover +.tooltip-container {
  display: block;
}
<div class="mp">
  <p>12.9</p>
</div>
<div class="tooltip-container">
  <span class="arrow-up"></span>
  <div class="tooltip">
    <h5>Hello World</h5>
    <span>Lorem ipsum dolor sit amet</span>
  </div>
</div>

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

CSS - Nesting classes within one another

I'm currently working on customizing a Tumblr theme. Users have the option to choose between a one-column or two-column layout style, and I'm facing some challenges with implementing this in the CSS code. I attempted to add a class based on the u ...

Switch out either even or odd divs within a container

I need help figuring out how to alternate the background colors of divs within a container. I want the first one to have a red background, the second to have blue, the third red again, and so on... Here is the code I currently have: <div>...</di ...

Creating a visually appealing website with Bootstrap 3

Looking to create a unique HTML design using Bootstrap. So far, this is what I've come up with: Plunker Any suggestions or assistance would be greatly appreciated. ...

Generating a dropdown menu in HTML using JSON entities

I am attempting to populate an HTML Select element with data retrieved from JSON. Below is a simplified version of the JSON object: {"Group1": "TestGroup1", "Group2" : "TestGroup2", "TotGroups" : "2"} To achieve this, I am using JQuery and AJAX for fetch ...

I am seeking a solution to this error that occurs whenever I attempt to call a function using a button

Issue " _ctx.hello is not a function TypeError: _ctx.hello is not a function at onClick._cache.<computed>._cache.<computed> (webpack-internal:///./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/di ...

image in responsive css design

Image 1 (Current Layout) Image 2 (Desired Layout) I am trying to achieve a responsive layout similar to the second image. However, I seem to be stuck with the layout from the first image. Here is the CSS code I'm currently using: @media (max-width ...

Properly extending the functionality of an HTML widget

Understanding HTML and CSS layout is still a mystery to me, so any guidance on what I'm trying to achieve would be greatly appreciated. I am currently developing a JavaScript API for our company's "widget" so that users can integrate it into the ...

Using special characters as parameters in Ajax requests

I need assistance with sending data via Ajax to my controller action. Here is the code snippet: $.ajax({ url: "/cotroller/action", data: { fieldsOfStudy: $('[name=fieldsOfStudy]').serialize()} }) The 'fieldsOfStudy' select bo ...

How can I modify the HTML code for rooms in the HTML 5 client of the bigbluebutton application?

Is it possible to customize the HTML code of rooms within the BigBlueButton source code? Additionally, how can I modify the CSS styles of elements in room sessions? For instance, I am interested in hiding the logo of BigBlueButton by setting 'display: ...

Ways to initiate a flip motion

Looking for a way to create a flip image effect when clicked by the user. Here is the code I have so far: let img = document.querySelector('img') let div; let click = 0; img.onclick=function(){ console.log(click) if(click %2 ==0){ d ...

The sorting of objects by Lodash is not accurate

My aim is to arrange objects based on a specific property (price). var arr = [{ name: 'Apple', price: '1.03' }, { name: 'Cherry', price: '0.33' }, { name: &apo ...

Attempting to trigger an action from a Vuex module proves futile as the error message "[vuex] unknown action type" is generated

I've been struggling to successfully register a Vuex module. Below is the code I've been working with: stores/index.js: import Vuex from 'vuex'; import resourcesModule from './resources'; import axios from '@/helpers/ax ...

Convert the MySQL DATE column from numerical month to full month name

I'm having trouble figuring out a simple solution. I'm using the code below to generate a tree grid from a MySQL database. $sql2 = mysql_query("SELECT DISTINCT YEAR(date) FROM blogs ORDER by YEAR(date) desc")or die(mysql_error()); $archive .= "& ...

Arranging xCharts based on the weekday order

Struggling with xCharts, specifically trying to display a bar chart showing numbers with corresponding days of the week in order. Despite my efforts, I can't seem to get them to appear in the correct sequence. Refer to the image below for reference: ...

Creating client side scripts for an ajax call to generate a Json object is simple once you understand the basics

Typically, when I make ajax calls, I request pure HTML. However, for various reasons, I require guidance on constructing a table (let's say of individuals and their information) when receiving a Json object. While I am capable of creating a table in J ...

What is the process for editing or importing CSS within a React project?

I'm a beginner in React and I am encountering an issue with CSS not loading properly. I followed the tutorial for importing it, but it seems like there might be a better way to do it now as the tutorial is outdated. Below is my code, I would appreciat ...

Increasing the div's size to exceed the dimensions of the screen

My issue pertains to the background image on my website. I have set a background image for the body and centered it horizontally. Each page includes a paragraph below the header, each with a different size, causing scrolling. The problem is that the backgr ...

Accessing the system through a pop-up window with the help of AJAX and

As a newcomer to the world of AJAX, I must admit that I may have jumped into this field with too much enthusiasm. For several days now, I have been struggling to integrate AJAX with my PHP script. Before dismissing this question as a duplicate, please unde ...

Creating a horizontal scroll effect using jQuery when the widths of the items are not

I am working on a jQuery gallery that showcases images in a horizontal layout. Below the images, there are "left" and "right" buttons which allow users to scroll through the pictures. There are many tutorials and plugins available for this type of function ...

Google Chrome Content Script: Unable to Trigger Dynamically Added Button

In my project, the custom Google Chrome Extension content script is designed to receive a message from popup.html through the Google Chrome Message Passing API once a user logs in using popup.html. This content script is responsible for dynamically adding ...