工具提示框向上显示而不是向下,该如何解决?

5

这是我的代码

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

我该如何将它定位在底部打开?由于它正在顶部打开,如果我在span中添加太多数据,则会超出页面并且无法滚动,因此部分内容不可见。是否也可以在工具提示中修复这个问题?

tooltip, tooltiptextchanges to tooltip1, tooltiptext1 - Rayees AC
6个回答

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>


在这个工具提示的左侧也被隐藏了,这取决于框的高度。考虑一下,如果您的div高度为500px,那么您的工具提示将从750px开始显示! - Chandra Prakash Ajmera

0

您可以在 tooltip1 .tooltiptext1 中将 bottom: 125%; 更改为 top: 125%;

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


哦,给我一分钟。 - Friday Ameh

0

你也可以使用数据属性(这里没有箭头)。

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


0

试试这个

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

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>


0

在这个例子中,考虑 div 父元素的宽度为 120 像素:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接