All Posts

Text Tooltips for WordPress

Published: August 7, 2024
Share:
Image
/* Tooltip container */
.cs-tooltip {
  position: relative;
  display: inline-block;
  cursor: pointer;
  text-decoration: underline;
}

/* Tooltip text */
.cs-tooltip .cs-tooltip-text {
  visibility: hidden;
  width: 250px;
  background: red; /* Change the background color */
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 10px;
  position: absolute;
  z-index: 99999999;
  bottom: 125%; /* Position the tooltip above the text */
  left: 50%;
  margin-left: -100px; /* Center the tooltip based on its new width */
  opacity: 0;
  transition: opacity 0.3s;
}

/* Tooltip arrow */
.cs-tooltip .cs-tooltip-text::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: red transparent transparent transparent; /* Match the background color */
}

/* Show the tooltip text when hovering over the tooltip container */
.cs-tooltip:hover .cs-tooltip-text {
  visibility: visible;
  opacity: 1;
}
document.addEventListener('DOMContentLoaded', function() {
  var tooltips = document.querySelectorAll('.cs-tooltip');
  
  tooltips.forEach(function(tooltip) {
    var tooltipText = tooltip.getAttribute('data-tooltip-text');
    if (tooltipText) {
      var span = document.createElement('span');
      span.className = 'cs-tooltip-text';
      span.innerText = tooltipText;
      tooltip.appendChild(span);
    }
  });
});
<p>This is a sentence and this is a tooltip – hover <span class="cs-tooltip" data-tooltip-text="This is my tooltip and I want you to read this sentence after reading the first stentence.">here</span> to see more. You could keep on writing text and adding <span class="cs-tooltip" data-tooltip-text="This is another tooltip">tooltips in as</span> you see fit</p>