One of my challenges involves working with an absolutely positioned div element (.tooltip
) containing another div (.text
) that has text with a set max-width
. The issue arises when the left
property of .tooltip
is too large, causing its width to shrink due to the default value of the right
property being 0. My goal is for the .tooltip
to prioritize respecting the width of the content within .text
. Here's what I've attempted:
Setting the width of
tooltip
tofit-content
- this achieves the desired outcome (shown in the snippet) but doesn't work on IE, which needs to be supported. Without this, the width of.tooltip
shrinks.Setting a fixed width for either
tooltip
ortext
isn't feasible as the text intext
can vary in length, resulting in empty space.Conversely, the text can also be quite lengthy, so I cannot use
white-space: nowrap
as recommended in similar queries.
.tooltip {
position: absolute;
border: 1px solid red;
left: 400px;
width: fit-content;
}
.text {
max-width: 200px;
}
<div style="border: 1px solid green;width:500px; height: 500px; position:relative">
<div class="tooltip">
<div class="text">test test test test test test test test test</div>
</div>
</div>