MickeyMouseFL
MickeyMouseFL

Reputation: 109

How to make tooltip stretch to fit text (without pre-defined width)

So, I have a tooltip and I want it to stretch to fit all of the text inside of it. I have about 30 tooltips actually and I don't want to manually change them (I tried it and it didn't work so I'm not sure how I would anyway). Here is my code:

.programButton {
    width: 1000px;
    height: 100px;
    background: #EAEAEA;
    border: 1px solid black;
    text-align: left;
}
#programButtonText, .programButtonExtra {
    font-family: 'Open Sans', sans-serif;
    font-size: 34px;
    margin-left: 8px;
}
.extraBox {
    width: 180px;
    background: white;
    border: 2px solid #999999;
    top: -55px;
    left: 380px;
    margin-bottom: -92px;
    /*margin-top: -80px;
    margin-left: 1000px;*/
    position: relative;
    display: block;
}
.extraBox1 {
    font-family: 'Open Sans', sans-serif;
    font-size: 25px;
    color: #47BC47;
    background: white;
    margin: 5px;
    margin-bottom: 10px;
}
.extraBox1:hover {
    cursor: pointer;
    background: #47BC47;
    color: white;
}
.extraBox2:hover {
    cursor: pointer;
    background: #D41B1B;
    color: white;
}
.extraBox2 {
    font-family: 'Open Sans', sans-serif;
    font-size: 25px;
    color: #D41B1B;
    background: white;
    margin: 5px;
    margin-top: 0px;
}
#programButtonText {
    line-height: 65px;
}
.hoverText {
    cursor: pointer;
}
.lastVisited {
    font-family: 'Open Sans', sans-serif;
    font-size: 22px;
    margin-top: -35px;
    margin-left: -793px;
}
.entireProgramButton {
    display: block;
    cursor: default;
}
.programButtonExtra {
    margin-top: -41px;
    margin-left: 980px;
}
span.tooltips {
  position: relative;
  display: inline;
}
span.tooltips span {
  font-family: 'Open Sans', sans-serif;
  font-size: 22px;
  position: absolute;
  color: #FFFFFF;
  background: #000000;
  height: 40px;
  line-height: 40px;
  text-align: center;
  display: none;
  visibility: hidden;
  border-radius: 9px;
}
span.tooltips span:after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  width: 0; height: 0;
  border-top: 8px solid #000000;
  border-right: 8px solid transparent;
  border-left: 8px solid transparent;
}
span:hover.tooltips span {
  visibility: visible;
  display: block;
  opacity: 1;
  bottom: 30px;
  right: -378px;
  top: -35px;
  margin-left: -76px;
}
<br><br><br>
<center>
    <div id = "entireProgramButton9" class = "entireProgramButton">
        <div id = "programButton9" class = "programButton"><div id = "programButtonText"><span id = "programName9" class = "hoverText">Tic-Tac-Toe&nbsp;<span class = "tooltips" id = "information">ⓘ<span>The Tic-Tac-Toe game lets you play against AI or another player.</span></span></span></div><div class = "programButtonExtra" id = "programButtonExtra9"><span class = "hoverText">⋮</span></div></div>
        <div class = "lastVisited">Last visited: Never</div>
        <div class = "extraBox" id = "extraBox9">
            <div id = "extraBox19" class = "extraBox1">Save for later</div>
            <div id = "extraBox29" class = "extraBox2">Report a bug</div>
        </div>
    </div>
</center>

So, as you can see (best results on the full-page results), when you hover over the ⓘ, it shows a little tooltip telling you about it. This works, but you'll notice that there is a lot of blank space on both sides of the text. This is because when I was making it, that 800px of width worked on the tooltip. Since I have like 30 tooltips, how can I have it so that it stretches the width approximately to fit the text?

Edit: It turns out that on this specific box, the tooltip does not look bad (besides the moving) if you take out the width. I replaced the program with another that I would have.

I also took out the width to show you what happens without it.

Upvotes: 2

Views: 2604

Answers (1)

Barbara Laird
Barbara Laird

Reputation: 12717

Just take the width out of span.tooltips span. The span will resize to the content. I also added a little right and left padding to that class to make it look neater.

.programButton {
    width: 1000px;
    height: 100px;
    background: #EAEAEA;
    border: 1px solid black;
    text-align: left;
}
#programButtonText, .programButtonExtra {
    font-family: 'Open Sans', sans-serif;
    font-size: 34px;
    margin-left: 8px;
}
.extraBox {
    width: 180px;
    background: white;
    border: 2px solid #999999;
    top: -55px;
    left: 380px;
    margin-bottom: -92px;
    /*margin-top: -80px;
    margin-left: 1000px;*/
    position: relative;
    display: block;
}
.extraBox1 {
    font-family: 'Open Sans', sans-serif;
    font-size: 25px;
    color: #47BC47;
    background: white;
    margin: 5px;
    margin-bottom: 10px;
}
.extraBox1:hover {
    cursor: pointer;
    background: #47BC47;
    color: white;
}
.extraBox2:hover {
    cursor: pointer;
    background: #D41B1B;
    color: white;
}
.extraBox2 {
    font-family: 'Open Sans', sans-serif;
    font-size: 25px;
    color: #D41B1B;
    background: white;
    margin: 5px;
    margin-top: 0px;
}
#programButtonText {
    line-height: 65px;
}
.hoverText {
    cursor: pointer;
}
.lastVisited {
    font-family: 'Open Sans', sans-serif;
    font-size: 22px;
    margin-top: -35px;
    margin-left: -793px;
}
.entireProgramButton {
    display: block;
    cursor: default;
}
.programButtonExtra {
    margin-top: -41px;
    margin-left: 980px;
}
span.tooltips {
  position: relative;
  display: inline;
}
span.tooltips span {
  font-family: 'Open Sans', sans-serif;
  font-size: 22px;
  position: absolute;
  color: #FFFFFF;
  background: #000000;
  height: 40px;
  line-height: 40px;
  text-align: center;
  display: none;
  visibility: hidden;
  border-radius: 9px;
  padding-left:15px;
  padding-right:15px;
}
span.tooltips span:after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  width: 0; height: 0;
  border-top: 8px solid #000000;
  border-right: 8px solid transparent;
  border-left: 8px solid transparent;
}
span:hover.tooltips span {
  visibility: visible;
  display: block;
  opacity: 1;
  bottom: 30px;
  right: -378px;
  top: -35px;
  margin-left: -76px;
}
<br><br><br>
<center>
    <div id = "entireProgramButton19" class = "entireProgramButton">
        <div id = "programButton19" class = "programButton"><div id = "programButtonText"><span id = "programName19" class = "hoverText">Clock&nbsp;<span class = "tooltips" id = "information">ⓘ<span>The Clock program tries to get the time.</span></span></span></div><div class = "programButtonExtra" id = "programButtonExtra9a"><span class = "hoverText">⋮</span></div></div>
        <div class = "lastVisited">Last visited: Never</div>
        <div class = "extraBox" id = "extraBox9a">
            <div id = "extraBox119" class = "extraBox1">Save for later</div>
            <div id = "extraBox219" class = "extraBox2">Report a bug</div>
        </div>
    </div>
</center>

Upvotes: 1

Related Questions