0

I have a title tag inside a rect:

<rect width="37" height="38" rx="10" stroke="none">
     <title>
ADM-04
Ana Fretta
Optiplex 3070
     </title>
</rect>

Which looks like this when you hover over the rect:

enter image description here

Is there any way to style the tooltip?

Mathias Hillmann
  • 1,537
  • 2
  • 13
  • 25

1 Answers1

1

with a reference to Timmmm's answer, here's a working demo of adding tooltip to sag element

const tooltips = document.querySelector('.tooltips');

function add_tooltip(ele) {
  const el = document.querySelector(ele);

  let tp = document.createElement('div');
  tp.setAttribute('class', 'tooltip');
  tooltips.appendChild(tp);
  tp.innerHTML = el.querySelector('title').innerHTML;

  el.addEventListener('mousemove', e => {
    tp.style.left = e.clientX + 10 + "px";
    tp.style.top = e.clientY + 10 + "px";
    tp.style.display = 'flex';
  });

  el.addEventListener('mouseleave', () => {
    tp.style.display = "none";
  });
}

add_tooltip('rect');
.tooltips{
  position: fixed;
  width: 100vw;
  height: 100vh;
  top: 0;
  left: 0;
  z-index: -1000;
}

.tooltip {
  background: cornsilk;
  border: 1px solid black;
  border-radius: 5px;
  padding: 5px;
  position: fixed;
  display: none;
  width: 100px;
  height: 100px;
  display: none;
  justify-content: center;
  align-items: center;
}
<div class="tooltips">
</div>

<svg>
  <rect width="37" height="38" rx="10" stroke="black" fill="white">
    <title>
      ADM-04
      Ana Fretta
      Optiplex 3070
    </title>
  </rect>
</svg>
Weilory
  • 2,621
  • 19
  • 35