I have a problem with the width when the tooltip is on the right edge of the screen. My tooltip has the following properties: max-width: 320px and position: fixed. And I don't understand why does tooltip shrink on the edge. I want to the tooltip come out from the right edge without shrinking. My tooltip can have one word or more, and therefore it should have adaptive width.
And I want the tooltip to be adaptive like this:
I want the long tooltip text has full max-width without shrinking and small text has a small width corresponding to the text.
html:
<div class="element">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat</div>
<div class="tooltip">
<span class="tooltip-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat</span>
</div>
css:
body {
background-color: #a3d5d3;
}
.tooltip {
max-width: 320px;
width: auto;
position: fixed;
background: #000;
color: #fff;
padding: 5px 10px;
border-radius: 5px;
pointer-events: none;
}
.tooltip-text {
display: inline-flex;
}
JavaScript:
var tooltip = document.querySelector('.tooltip');
tooltip.style.left = 0;
tooltip.style.top = 0;
var elWithTooltip = document.querySelector('.element');
function changePosition(e) {
const { clientX, clientY } = e;
tooltip.style.left = clientX + "px";
tooltip.style.top = clientY + "px";
}
elWithTooltip.addEventListener('mouseover', changePosition);
elWithTooltip.addEventListener('mousemove',changePosition);