Ivan Bacher
Ivan Bacher

Reputation: 6154

Auto width of child to absolutely positioned parent

I would like to make the width of the tooltip (.tooltip-text) automatically adjust it's text content length. At the moment it takes the width of it`s parent and wraps the text accordingly. It's this possible without using any js?

.tooltip {
  position: relative;
  display: inline-block;
  border-bottom: 1px dotted black;
}

.tooltip .tooltiptext {
  visibility: hidden;   
  width: 100%;
  background-color: #555;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  transform: translateX(-50%);
  opacity: 0;
  transition: opacity 0.3s;
}

.tooltip .tooltiptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}

.tooltip:hover .tooltiptext {
  visibility: visible;
  opacity: 1;
}
<h2>Tooltip</h2>
<p>Move the mouse over the text below:</p>

<div class="tooltip">
  <div> Hover me </div> 
    <div class="tooltiptext">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
  </div>
</div>

Upvotes: 2

Views: 44

Answers (1)

Jaswinder Kaur
Jaswinder Kaur

Reputation: 1634

.tooltip .tooltiptext {
    width: max-content;
    max-width: 440px;
}

Upvotes: 3

Related Questions