rohit bumrah
rohit bumrah

Reputation: 236

Tooltip box showing upwards instead of bottom

This is my code

.tooltip1 {
  position: relative;
  display: inline-block;
}


/* Tooltip text */

.tooltip1 .tooltiptext1 {
  visibility: hidden;
  width: 270px;
  background-color: #555;
  color: #fff;
  text-align: center;
  padding: 5px 0;
  border-radius: 6px;
  /* Position the tooltip text */
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  margin-left: -60px;
  /* Fade in tooltip */
  opacity: 0;
  transition: opacity 0.3s;
}


/* Tooltip arrow */

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


/* Show the tooltip text when you mouse over the tooltip container */

.tooltip1:hover .tooltiptext1 {
  visibility: visible;
  opacity: 1;
}
<div class="tooltip">Hover over me
  <span class="tooltiptext">Tooltip text</span>
</div>

How do I position it such that it open in bottom. Also as it is opening on top and if I add too much data in span then it goes above page and it cant be scrolled so some part is not visible.Is there a fix to this also in tooltip?

Upvotes: 5

Views: 1554

Answers (6)

Chandra Prakash Ajmera
Chandra Prakash Ajmera

Reputation: 304

Here is what you can do:

Instead of the following:

 /* Position the tooltip text */
  position: absolute;
  z-index: 1;
  top: 125%;
  left: 50%;
  margin-left: -60px;

You can use:

/* Position the tooltip text */
position: absolute;
z-index: 1;
top: calc(100% + 5px); /* start from bottom (100%) adding 5px. */
left:0; /* start from the left */

And, for the arrow, instead of:

 top: 100%;
 left: 50%;
 margin-left: -5px;
 border-color: #555 transparent transparent transparent;

Do:

 top: -10px; /* position of arrow 5px+5px */
 left: 50%;
 border-color: transparent transparent #555 transparent; /* direction of the arrow */

Consider the following working example:

.tooltip {
    position: relative;
    display: inline-block;
}

/* Tooltip text */
.tooltip .tooltiptext {
    /* visibility: hidden; */
    width: 100%;
    background-color: #555;
    color: #fff;
    text-align: center;
    padding: 5px 0;
    border-radius: 6px;

    /* Position the tooltip text */
    position: absolute;
    z-index: 1;
    top: calc(100% + 5px);
    left:0;

    /* Fade in tooltip */
    opacity: 0;
    transition: opacity 0.3s;
}

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

/* Show the tooltip text when you mouse over the tooltip container */
.tooltip:hover .tooltiptext {
    visibility: visible;
    opacity: 1;
}
<div class="tooltip">Hover over me
   <span class="tooltiptext">Tooltip text</span>
</div>

Upvotes: 0

Friday Ameh
Friday Ameh

Reputation: 1684

You can change bottom: 125%; to top: 125%; in tooltip1 .tooltiptext1.

.tooltip1 {
    position: relative;
    display: inline-block;
}

/* Tooltip text */
.tooltip1 .tooltiptext1 {
    visibility: hidden;
    width: 270px;
    background-color: #555;
    color: #fff;
    text-align: center;
    padding: 5px 0;
    border-radius: 6px;

    /* Position the tooltip text */
    position: absolute;
    z-index: 1;
    top: 125%;
    left: 50%;
    margin-left: -60px;

    /* Fade in tooltip */
    opacity: 0;
    transition: opacity 0.3s;
}

/* Tooltip arrow */
.tooltip1 .tooltiptext1::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

/* Show the tooltip text when you mouse over the tooltip container */
.tooltip1:hover .tooltiptext1 {
    visibility: visible;
    opacity: 1;
}
   <div class="tooltip1">Hover over me
  <span class="tooltiptext1">Tooltip text. 
    Lorem, ipsum dolor sit amet, consectetur adipisicing elit. Tempore a  assumenda laudantium magnam facere!Eaque perspiciatis eos accusamus inventore  aut!</span>
</div>

Upvotes: 0

Renzo CJ
Renzo CJ

Reputation: 43

Consider that the div parent is 120px in this example:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Testing tooltips</title>
<style type="text/css">
    .wrapper {
        position: relative;
        width: 1200px;
        height: 800px;
        background-color: #f4f4f4;
        margin: 0 auto;
        padding-top: 50px;
    }
    .tooltip {
        position: relative;
        display: inline-block;
        margin-left: 20px;
    }
    .tooltip .tooltiptext {
        visibility: hidden;
        width: 120px;
        background-color: black;
        color: #fff;
        text-align: center;
        padding: 5px 0;
        border-radius: 6px;
        position: absolute;
        z-index: 1;
    }
    .tooltip-position {
        top: 125%;
        left: 50%;
        margin-left: -60px;
    }
    .tooltip:hover .tooltiptext {
        visibility: visible;
        opacity: 1;
    }
    .tooltip .tooltiptext::after {
        content: "";
        position: absolute;
        bottom: 100%;
        left: 50%;
        margin-left: -5px;
        border-width: 5px;
        border-style: solid;
        border-color: transparent transparent #000 transparent;
    }
    
</style>
</head>

<body>
    <div class="wrapper">
        <div class="tooltip">Hover over me!
            <span class="tooltiptext tooltip-position">Tooltip text</span>
        </div>
    </div>
</body>
</html>

The arrow positions are:

/*Consider 120px parent div as an example*/

//Top
Bottom: 125px;
left: 50%;
margn-left: -60px;

//Bottom
top: 125px;
left: 50%;
margin-left: -60px;

//right
top: -5px;
left: 125%

//Left
top: -5px;
bottom: auto;
right: 128%;

Upvotes: 0

AbsoluteBeginner
AbsoluteBeginner

Reputation: 2255

You can use data attribute as well (here without arrow).

.tooltip {
     position: relative;
}
 .tooltip-bottom:hover::after {
     content: attr(data-tooltip);
     padding: 4px 8px;
     position: absolute;
     left: 0;
     top: 110%;
     white-space: nowrap;
     z-index: 1;
     background-color: #000;
     color: #fff;
     border-radius: 3px;
}
<span class="tooltip tooltip-bottom" data-tooltip="Tooltip text">Hover me</span>

Upvotes: 0

Rayees AC
Rayees AC

Reputation: 4659

Try this,

change this styles,

.tooltip .tooltiptext {
  top: 150%; // bottom: 125%; given already
}

.tooltip .tooltiptext::after {
  bottom: 100%; //top: 100%; given already
}

.tooltip {
  position: relative;
  display: inline-block;
}

.tooltip .tooltiptext {
  visibility: hidden;
  width: 120px;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 5px 0;
  position: absolute;
  z-index: 1;
  top: 150%;
  left: 50%;
  margin-left: -60px;
}

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

.tooltip:hover .tooltiptext {
  visibility: visible;
}
<div class="tooltip">Hover over me
  <span class="tooltiptext">Tooltip text</span>
</div>

Upvotes: 5

Thisara Senanayake
Thisara Senanayake

Reputation: 26

Try this

  .tooltip1 .tooltiptext1::after {
    content: "";
    position: absolute;
    bottom: 100%;
    left: 50%;
    border-width: 5px;
    border-style: solid;
    border-color: transparent transparent  #555 transparent;
  }
  

Upvotes: 0

Related Questions