php - 文本显示在图像悬停上

标签 php html css

<style>
    /* CSS for the span */
    .hover {
        visibility: hidden;
        width: 120px;
        background-color: black;
        color: #fff;
        text-align: center;
        border-radius: 6px;
        padding: 5px 0;
        position: absolute;
        z-index: 1;
        bottom: 100%;
        left: 96%;
        margin-left: -60px;
    }

    /* CSS for the text containing the span */
    .imagetest {
        position: relative;
    }

    /* CSS for text on hover */
    .imagetest:hover .hover {
        visibility: visible;
        display:inline-block;                
    }
</style>

PHP/HTML

<?php
    echo "<div class='hoverText' align=right><img class='imagetest' src='Files/infoplaceholder.png' height=20 width=20><span class='hover'> See About Page for more info </span></div>";
    echo "<BR>";
?>

当我将交换名称放在 css 部分时,imagetest for hoverText 工作得很好。该方法的缺点是 div 横跨整个页面,鼠标光标经过 div 的任何地方都会弹出悬停。悬停只是不喜欢我存储在图像上的类。我想知道这是为什么。

最佳答案

您的 CSS 正在尝试选择“.imagetest”的子元素,该元素具有“悬停”类。由于 span.hover 标签不是 .imagetest 的子标签,css 不会选择它。

尝试这样的事情

    <div class='hoverText' align=right>
        <span class='imagetest'>
            <img src='Files/infoplaceholder.png' height=20 width=20 />
            <span class='hover' > See About Page for more info </span>
        </span>
    </div>

这将帮助 CSS 找到内部跨度作为 .imagetest 的子元素

另一种选择是使用 CSS 选择器来选择 sibling

.imagetest:hover + .hover {
    visibility: visible;
    display: inline-block;
}

See a list of CSS selectors at W3Schools

关于php - 文本显示在图像悬停上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50686043/

相关文章:

php - Twig 赋值运算符

javascript - jQuery 的 div 调整大小问题

css - 如何在垂直居中对齐图标字体?

html - 通过减小 td 的大小来删除表中两个 td 之间的空白

jquery - tr 显示后不取全长 :none

php - 正则表达式匹配用户名的复杂条件

php - 从 mysql 获取主类别的子类别

php - 使用 PHP for 和 foreach 循环从 JSON 数据中检索视频信息时遇到问题 - YouTube API 3

php - 撤出帖子后将标签放在 block 引号上

javascript - 在 jQuery 的嵌套列表中选择没有子项的列表项父项