Steps for creating a CSS-only tooltip for a group of paths within an SVG element

Looking for help on adding a CSS-only tooltip to an SVG map on my WordPress website. The tooltip should appear when hovering over countries with the class st0 and display information from the data-info attribute of each country's path in the SVG. Here is the code I have so far: https://codepen.io/ohad-olshevsky/pen/bGbNOVW I attempted to add the tooltip style mentioned here without success: https://www.w3schools.com/css/css_tooltip.asp Since I am using the Elementor pro plugin and inserting the map with the HTML widget, I want to achieve this tooltip effect using only CSS due to limitations with JS and JQ. Thanks for any assistance provided.

I've experimented with jQuery and JavaScript but lack sufficient experience in these areas, and the Elementor plugin does not integrate well with them.

CSS:

#un-map-by-ohad {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
.st0{fill:#ffFFFF;stroke:#262626;}
.st0{
    transition-property: opacity;
    transition-duration: .7s;
    transition-timing-function: ease;
}
.st1{fill:#262626;stroke:#A8D3E8;stroke-width:5.5;}
.st0:hover {
    fill:white;
    opacity: 0.3;
    stroke-width: 0px;
    stroke: #476ebf;
    stroke-opacity: o;
    background-size: 70%;
    transition-property: opacity;
    transition-duration: .7s;
    transition-timing-function: ease;
}

.st2{color:#262626;stroke:#FFFFFF;stroke-width:0.5;}
.st3{fill:#262626;stroke:#FFFFFF;stroke-width:0.5;stroke-opacity:0.9412;}
.st4{color:transparent;}
#info-box 
{
    display: none;
    position: absolute;
    top: 0px;
    left: 0px;
    z-index: 1;
    background-color: #ffffff;
    border: 2px solid #476ebf;
    border-radius: 5px;
    padding: 5px;
    font-color:white;
    font-family: open sans;
}

HTML

<path id="es" data-info="<div>Spain</div><div>2.6%</div>" class="st0" d="M5036.5,4876c10,4.3,21.5,7.2,28.7,12.9c2.9,2.9,4.3,18.6,12.9,17.2c11.5-1.4,21.5-8.6,34.4-8.6"/>

Answer №1

If you wish to showcase prompts, one effective method is by utilizing the SVG <title> ... </title> tag.

To execute prompt output, such as highlighting a specific area on a map, it is necessary to enclose the path responsible for that region within a group tag - <g>.

Furthermore, include a pair of <title> ...</title> tags containing explanatory text inside the above structure.

For instance:

<g>
<title>   "UT" - Utah
      State capital city - Salt Lake City
      Population
      City  186,440
      Estimate Estimate (2018)  200,591
 </title> 
      <path d="M0 0...."/>
</g>      

<style>
.state {
fill:white;
stroke:dodgerblue;
transition:  1s;
}
:hover.state {
 fill:blue;
 stroke:white;
 stroke-width:4px;
 }
</style>
<body>
   <section id="map">

    <svg version="1.1"
       id="svg2" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="467 -284 959 593" xml:space="preserve">
      <g>
  <title>"ID" - IDAHO
State capital city - Boise
Population - 228,790
  </title>
      <path id="ID"  class="state" d="...
        M615.5-107.5l8.8-35.2l1.4-4.2l2.5-5.9l-1.3-2.3l-2.5,0.1l-0.8-1l0.5-1.1l0.3-3.1l4.5-5.5l1.8-0.5l1.1-1.1l0.6-3.2l0.9-0.7l3.9-5.8
        ...
        C710.4-89.3,622.5-106,615.5-107.5z"/> 
</g>
  <g>    
...CONTINUES...
...

Answer №2

.country {
  width: 100px;
  height: 100px;
  background: #33f;
  opacity: 1;
  transition: 0.7s background;
}

.country::after {
  content: attr(data-info);
  background: black;
  color: white;
  border-radius: 0.5em;
  padding: 1em;
  opacity: 0;
  transition: 0.7s opacity;
  
  /* Set position in relation to the nearest ancestor with a non-static position 
     which in this case is the root of the document
  */
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  pointer-events: none;
}

.country:hover {
  background: #99f;
}

.country:hover::after {
  opacity: 1;
}
<div class="country" data-info="France"></div>
<div class="country" data-info="Germany"></div>

Utilize the ::after pseudo-element to present the information stored in the content attribute in .country::after.

If you desire the informational display to be consistent for all countries, pay attention to the specifications following the position property in .country::after.

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

Does anyone know a workaround for addressing the issue of the "n items remaining" problem specifically on Internet Explorer?

When using my ASP.Net app, which heavily relies on javascript and jQuery, along with master pages and .Net Ajax components, I consistently encounter an issue in IE 6 (and sometimes IE 7) where the status bar displays messages like "2 items remaining" or "1 ...

Retrieve the raw text from the input field instead of the date object

Below is the HTML code for an input field: <div class="input-group input-group-md"> <input id="date" name="date" [readOnly]="disabled" type="text" placeholder="M0/d0/0000 Hh:m0:s0" [placeholder]="pl ...

Styling Dropdown Menu Options

Is there a way to format two strings in a select list and display them as shown below? The first string should begin with 'Item1' followed by spaces until 10 spaces are taken up, then add a delimiter '|' and the second string. So all ...

What is the method to programmatically trigger the highlight effect (similar to mouseover) in Javascript or JQuery?

In the process of creating an interface that features a list of large buttons, I am interested in implementing functionality to highlight a button based on the movement of the mouse. Essentially, I would like the button below the currently highlighted butt ...

Creating a Turn.js interactive book similar to the provided example?

I'm in the process of creating a book using turn.js with Jquery and everything is going smoothly so far. However, I could use some assistance. I am trying to achieve the look of a hard-backed journal-type book similar to the example provided here . My ...

Tips for highlighting HTML syntax within JavaScript strings in Sublime Text

Is there a Sublime package available for syntax highlighting HTML within JavaScript strings specifically? (Please note that the inquiry pertains to highlighting HTML within JS strings only, not general syntax highlighting.) Currently, I am developing Ang ...

basic php websocket demonstration

Seeking help for implementing websocket functionality. I have been working on a websocket example, but it seems to be encountering some issues that I can't identify. Any assistance would be greatly appreciated. Thank you in advance. Here are the ex ...

The elegant scroll-bar (whether directive-based or not) seems to be having trouble functioning within the ng-view

I am looking to add a stylish scroll bar to a paragraph that is located within the ng-view. My module code appears as follows: var myweb = angular.module('myweb' , ['ngRoute', 'perfect_scrollbar']); myweb.config(function($ro ...

I am experiencing difficulty with inputting the data into the MySQL database

I have been encountering an error every time I try to input these values into the database. I suspect that the issue lies within this section of the code, but I am unable to pinpoint it. Can someone please assist me in identifying and resolving the error ...

A Troublesome Dilemma: Internet Explorer's Div

I'm currently in the process of designing a vBulletin forum template, and I've encountered an issue with one of the headers that is proving to be quite tricky. It appears that Internet Explorer is expanding the width of one of the div elements by ...

AngularJS field array

Is it possible to create an array that contains references to the fields in a form (such as input, textarea, etc.) and then run a function on them in my code, like addClass()? To clarify my objective - I am looking to add a red border color to any invalid ...

Tips for creating child divs with a width that spans the entire page

When viewing multiple rows of containers on my page, the elements shift inline on smaller screens. I am looking to have the orange bar positioned behind and slightly below the header, spanning the width of the page. To achieve this, I utilized relative pos ...

The communication between a Firefox XUL extension and a webpage

Currently developing a Firefox XUL extension and in need of incorporating interaction between the web page and the extension. For instance, whenever a link is clicked on the page, I would like to trigger a function within the XUL extension. Is there any k ...

Automatically adjust height directive to fill up the remaining space

Is there a way to dynamically adjust the height of an element to fill the remaining space within its container? In my current layout, I have a fixed-height header (or menu) in pixels, a content area that may overflow the window height and require scroll b ...

When I hover my mouse over the items on the Navigation bar, they shift forward

Seeking assistance with a coding issue. When hovering over TOYOTA, the HONDA and CONTACT US sections move forward unexpectedly. This movement is unwanted as I would like only the next list item to display when hovering over TOYOTA and HONDA. How can I corr ...

Use Angular to change the style of multiple element groups at the same time when hovering

When hovering over an element with a common attribute, such as a class name, I want to change the style of all elements that share that attribute. Achieving this effect is simple with jQuery: $(function() { $('.bookId4').hover( function(){ ...

What are some other options besides object-fit?

Currently seeking a replacement for the css object-fit property that functions properly in Firefox and IE. While it works well in Chrome and Opera, it is not compatible with Firefox and IE. Experimented with the imgLiquid Plugin, but encountered issues w ...

What is the best way to pass createdDt and updatedDat values in an Angular form without displaying them on the template?

I am working on a message creation form in Angular where I need to include createdDt and updatedDt as hidden values. These values should not be visible in the template. I want to automatically set the current date and time for both createdDt and updatedD ...

What is the best way to combine XHTML files in Java while handling any potential CSS conflicts that may arise

Looking for a way to concatenate XHTML files in Java and resolve CSS collisions at runtime? The challenge is to merge files with different style definitions without losing any content. JSoup seems promising but falls short on managing style conflicts autom ...

A guide on embedding the flag status within the image tag

I would like to determine the status of the image within the img tag using a flag called "imagestatus" in the provided code: echo '<a href="#" class="swap-menu"><img id="menu_image" src="images/collapsed.gif" hspace = "2"/>'.$B-> ...