工具提示框显示为向上而不是底部

时间:2020-09-05 11:24:17

标签: html css

这是我的代码

.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>

如何定位它使其在底部打开。同样因为它在顶部打开,并且如果我在跨度中添加了太多数据,那么它会超出页面并且无法滚动,因此某些部分不可见。工具提示中是否对此有解决方法?

6 个答案:

答案 0 :(得分:5)

尝试一下,

更改此样式,

.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>

答案 1 :(得分:0)

尝试一下

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

答案 2 :(得分:0)

您可以在bottom: 125%;中将top: 125%;更改为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>

答案 3 :(得分:0)

您也可以使用data属性(此处没有箭头)。

.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>

答案 4 :(得分:0)

这是您可以做的:

代替以下内容:

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

您可以使用:

/* 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 */

然后,用箭头代替:

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

要做:

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

考虑以下工作示例:

.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>

答案 5 :(得分:0)

在此示例中,考虑div父级为120px:

<!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>

箭头位置是:

/*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%;