简体   繁体   English

CSS工具提示位置不固定

[英]css tooltip position is not fixed

I have created a tooltip using CSS. 我已经使用CSS创建了工具提示。 When I hover on the image gets displayed but the position is not fixed; 当我将鼠标悬停在图像上时,显示该图像,但位置不固定; it depends on the size of the image. 这取决于图像的大小。

Here is the HTML: 这是HTML:

   <section class="col col-2">
                  <input type="file" id="imgupload" accept="image/*" onchange="angular.element(this).scope().uploadFile(this)" style="display:none"
                  />
                  <div class="pointer  imagewh">

                      <img type="image" data-ng-src="{{currIcon}}" ng-click="icon()" class="responsive" />

                    <span class="tooltiptext">Upload Icon</span>
                  </div>
                </section>

Here is the CSS: 这是CSS:

 .pointer img {
        cursor: pointer;
        position: relative;
        display: inline-block;
        margin-bottom: 7px;
    }

    .pointer .tooltiptext {
        visibility: hidden;
        width: 120px;
        background-color: black;
        color: #fff;
        text-align: center;
        border-radius: 6px;
        padding: 5px 0;
        position: absolute;
        z-index: 1;
    }

    .pointer:hover .tooltiptext {
        visibility: visible;
    }

.responsive {
        max-width: 100%;
        height: auto;
    }    

.imagewh{
text-align:center;
width: 110px;
height: 110px;
}
.imagewh img{
position:relative;
top:50%;
transform: translateY(-50%);
}

在此处输入图片说明 在此处输入图片说明

When I made the image responsive, the tooltip position keeps changing with each respective size of the images. 当我使图像响应时,工具提示位置会随着图像的各个大小而不断变化。 How to make the position fixed? 如何确定职位? Please help me with this. 请帮我解决一下这个。

Here is my solution, Its not perfect, but it will give you a good staring point. 这是我的解决方案,虽然它不是完美的,但是它将给您一个很好的凝视点。

Basically I made the pointer div position:relative , and the tooltip position:absolute and also changed visibility:none to display:none . 基本上,我将pointer div position:relative和工具提示的position:absolute更改,并将visibility:none更改为display:none

Visiblilty just hides the div, but the element still occupies space in the DOM. Visiblilty只是隐藏了div,但是元素仍然在DOM中占据空间。 display:none completely hides the div along with the space display:none完全隐藏div和空格

And when you hover over the pointer div, I make the tooltip display:block 当您将鼠标悬停在pointer div上时,我将display:block tooltip display:block

I removed the 我删除了

 *{box-sizing:border-box;} body{padding:100px;} .pointer{ position:relative;} .pointer img { cursor: pointer; position: relative; display: inline-block; margin-bottom: 7px; width: 100%; height: 100%; } .pointer .tooltiptext { display:none; width: 120px; background-color: black; color: #fff; text-align: center; border-radius: 6px; padding: 5px 0; position: absolute; z-index: 1; top:-30px; left:50%; transform:translateX(-50%); } .pointer:hover .tooltiptext { display:block; } .responsive { max-width: 100%; height: auto; } .imagewh { text-align: center; width: 110px; height: 110px; border:1px solid black;/*for testing purpose*/ } .bigimage{ width: 210px; height: 210px; } 
 <div class="pointer imagewh"> <img type="image" data-ng-src="https://placehold.it/100x100" class="responsive" /> <span class="tooltiptext">Upload Icon</span> </div> <br/><br/> <div class="pointer imagewh bigimage"> <img type="image" data-ng-src="https://placehold.it/200x200" class="responsive" /> <span class="tooltiptext">Upload Icon</span> </div> 

 .pointer img { cursor: pointer; position: relative; display: inline-block; margin-bottom: 7px; } .pointer .img-box { position:relative; } .pointer .tooltiptext { visibility: hidden; width: 120px; background-color: black; color: #fff; text-align: center; border-radius: 6px; padding: 5px 0; position: absolute; z-index: 1; } .img-box:hover .tooltiptext { visibility: visible; right: calc( 0% - 60px); top: -30px; } .responsive { max-width: 100%; height: auto; } .imagewh{ text-align:center; width: 110px; height: 110px; } .imagewh img{ position:relative; } 
 <br> <br> <div class="pointer imagewh"> <div class="img-box"> <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSLsza5efhXDC69Ka0TuAMa1bfm2tbtUHXKVSKn3yzgVoiXbu6A" class="responsive" /> <span class="tooltiptext">Upload Icon</span> </div> </div> 

Try this. 尝试这个。 It works fine. 工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM