Regrettably, achieving optimal tooltip positioning purely with CSS poses a challenge.
Suggested Script Solutions
However, given that you are already utilizing some scripts, I recommend the following solution:
- Utilize
position: absolute
to accurately position the .ktooltiptext
relative to the .ref
,
- Leverage the
.getBoundingClientRect()
method for seamless retrieval of tooltip dimensions and positioning,
- Implement corrections if the tooltip extends beyond the window boundaries,
- Make necessary adjustments in the CSS styling.
Snippet using only native JavaScript
var ktooltips = document.querySelectorAll(".ktooltip");
ktooltips.forEach(function(ktooltip, index){
ktooltip.addEventListener("mouseover", position_tooltip);
})
function position_tooltip(){
var tooltip = this.parentNode.querySelector(".ktooltiptext");
var ktooltip_rect = this.getBoundingClientRect();
var tipX = ktooltip_rect.width + 5;
var tipY = -40;
// Position tooltip
tooltip.style.top = tipY + 'px';
tooltip.style.left = tipX + 'px';
var tooltip_rect = tooltip.getBoundingClientRect();
if ((tooltip_rect.x + tooltip_rect.width) > window.innerWidth)
tipX = -tooltip_rect.width - 5;
if (tooltip_rect.y < 0)
tipY = tipY - tooltip_rect.y;
tooltip.style.top = tipY + 'px';
tooltip.style.left = tipX + 'px';
}
.ref,
.refs {
position: relative;
}
.ktooltip {
display: inline-block;
text-indent: 0em;
}
.ref .ktooltiptext,
.refs .ktooltiptext {
visibility: hidden;
width: 200px;
background: #fff;
border-radius: 6px;
padding: 5px;
top: -999px;
left: -999px;
border: 2px solid grey;
line-height: normal;
position: absolute;
z-index: 1;
}
.ref:hover .ktooltiptext,
.refs:hover .ktooltiptext {
visibility: visible;
}
<p>
<span id="edtxt.trans1" class="tei l">My hope is in a bishop,
<!--link to a reference -->
<sup class="ref expl">
<a href="#edtxt-explnote1" id="reference-to-edtxt-explnote1" class="ktooltip">1</a>
<!-- the reference in a tooltip -->
<span class="ktooltiptext">According to tradition <span style="name">Nicholas</span> was bishop of Myra in Lycia (south-west Turkey today).</span>
</sup>
</span><br>
<span id="trans2" class="tei l">and in almighty God and his never-ending miracles.</span><br>
<span id="trans3" class="tei l">Generous Saint Nicholas holds an office,</span><br>
<span id="trans4" class="tei l">there is a gold symbol in his sign.
<!-- link to ref -->
<sup class="ref expl">
<a href="#edtxt-explnote2" id="reference-to-edtxt-explnote2" class="ktooltip">20</a>
<!-- the tooltip -->
<span class="ktooltiptext"> One of <span style="">Nicholas’s</span> symbols was three <sup>bags</sup> of gold <span style="font-variant: small-caps;">which</span> were the <span style="color: red;">dowry</span> he provided <span style="color: blue;">for</span> three <span style="color: green;">girls</span>
</span>
</sup>
</span><br>
</p>
jQuery Implementation
$(".ktooltip").mouseover(function(e) {
var tooltip = $(this).siblings('.ktooltiptext');
var tipX = $(this).outerWidth() + 5;
var tipY = -40;
tooltip.css({ top: tipY, left: tipX });
var tooltip_rect = tooltip[0].getBoundingClientRect();
if ((tooltip_rect.x + tooltip_rect.width) > $(window).width())
tipX = -tooltip_rect.width - 5;
if (tooltip_rect.y < 0)
tipY = tipY - tooltip_rect.y;
tooltip.css({ top: tipY, left: tipX });
});
.ref,
.refs {
position: relative;
}
.ktooltip {
display: inline-block;
text-indent: 0em;
}
.ref .ktooltiptext,
.refs .ktooltiptext {
visibility: hidden;
width: 200px;
background: #fff;
border-radius: 6px;
padding: 5px;
top: -999px;
left: -999px;
border: 2px solid grey;
line-height: normal;
position: absolute;
z-index: 1;
}
.ref:hover .ktooltiptext,
.refs:hover .ktooltiptext {
visibility: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>
<span id="edtxt.trans1" class="tei l">My hope is in a bishop,
<!--link to a reference -->
<sup class="ref expl">
<a href="#edtxt-explnote1" id="reference-to-edtxt-explnote1" class="ktooltip">1</a>
<!-- the reference in a tooltip -->
<span class="ktooltiptext">According to tradition <span style="name">Nicholas</span> was bishop of Myra in Lycia (south-west Turkey today).</span>
</sup>
</span><br>
<span id="trans2" class="tei l">and in almighty God and his never-ending miracles.</span><br>
<span id="trans3" class="tei l">Generous Saint Nicholas holds an office,</span><br>
<span id="trans4" class="tei l">there is a gold symbol in his sign.
<!-- link to ref -->
<sup class="ref expl">
<a href="#edtxt-explnote2" id="reference-to-edtxt-explnote2" class="ktooltip">20</a>
<!-- the tooltip -->
<span class="ktooltiptext"> One of <span style="">Nicholas’s</span> symbols was three <sup>bags</sup> of gold <span style="font-variant: small-caps;">which</span> were the <span style="color: red;">dowry</span> he provided <span style="color: blue;">for</span> three <span style="color: green;">girls</span>
</span>
</sup>
</span><br>
</p>
You can experiment with any of the provided code snippets to ensure that the tooltip remains well-positioned within the window bounds!
⋅
⋅
⋅
An Alternative CSS-Based Approach
If precise tooltip alignment is not a priority, consider this CSS-only solution where no HTML modifications are required:
- Apply
position: relative;
to the span
elements to serve as reference points,
- Use
position: absolute
on the .ktooltiptext
,
- Define
top
and left
properties to control the placement of the .ktooltiptext
.
This approach maintains the tooltip's visual style while ensuring it aligns consistently below and to the left of the respective span
element, preventing overflow issues.
p span { /* TAKIT: Changed here */
position: relative;
}
.ktooltip {
display: inline-block;
text-indent: 0em;
}
.ref .ktooltiptext,
.refs .ktooltiptext {
visibility: hidden;
width: 200px;
background: #fff;
border-radius: 6px;
padding: 5px;
border: 2px solid grey;
line-height: normal;
position: absolute;
top: 20px;
left: 0;
z-index: 1;
}
.ref:hover .ktooltiptext,
.refs:hover .ktooltiptext {
visibility: visible;
}
<p>
<span id="edtxt.trans1" class="tei l">My hope is in a bishop,
<!--link to a reference -->
<sup class="ref expl">
<a href="#edtxt-explnote1" id="reference-to-edtxt-explnote1" class="ktooltip">1</a>
<!-- the reference in a tooltip -->
<span class="ktooltiptext">According to tradition <span style="name">Nicholas</span> was bishop of Myra in Lycia (south-west Turkey today).</span>
</sup>
</span><br>
<span id="trans2" class="tei l">and in almighty God and his never-ending miracles.</span><br>
<span id="trans3" class="tei l">Generous Saint Nicholas holds an office,</span><br>
<span id="trans4" class="tei l">there is a gold symbol in his sign.
<!-- link to ref -->
<sup class="ref expl">
<a href="#edtxt-explnote2" id="reference-to-edtxt-explnote2" class="ktooltip">20</a>
<!-- the tooltip -->
<span class="ktooltiptext"> One of <span style="">Nicholas’s</span> symbols was three <sup>bags</sup> of gold <span style="font-variant: small-caps;">which</span> were the <span style="color: red;">dowry</span> he provided <span style="color: blue;">for</span> three <span style="color: green;">girls</span>
</span>
</sup>
</span><br>
</p>