css - 使图像出现在链接悬停 css 上

标签 css image hyperlink hover

我正在使用这个可能很简单的解决方案,但我在 stackoverflow 或互联网上找不到与此特定问题相关的任何帮助。

我有一个菜单,我想让一个图像出现在链接下方或当您将鼠标悬停在它上面时靠近它。我希望这可以在 CSS 中完成。到目前为止,这是我的代码。

HTML

<html>
    <head>
        <title></title>

        <link rel="stylesheet" type="text/css" href="startpage.css">


        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
     <div id="wrapper">


         <img id="baggrund" width="166" height="327" alt="back image" src="images/laerkeryg.jpg"/>
         <img id="cirkler" width="319" height="249" alt="circles" src="images/circles.PNG"/>
         <div id="header">/jacob gerlif pilegaard/</div>
         <div id="underheader">portfolio</div>   
          <ul>
            <li><a href="index.html">home</a></li>
            <li><a href="about.html">about</a></li>
            <li><a href="gallery.html">gallery</a></li>
            <li><a href="contact.html">contact</a></li>
            </ul> 
         </div>
     <img id="menucircle" width="50" height="50" alt="menu circle" src="images/circle.PNG"/>
          </body>
</html>

这是目前为止的 CSS:

a:hover
{
    background-image: url('image/circle.PNG');
    background-repeat: no-repeat;
}

希望大家能帮帮我!

最佳答案

干净的处理方式是使用:after伪元素

a:hover:after {
    content: url(image/circle.PNG); /* no need for qoutes */
    display: block;
}

您甚至不需要图像出现在 DOM 中。
还要记住,图像的路径是相对于 CSS 文件的,而不是 HTML 文件。

当然,图像将下推 anchor 下方的内容。为避免这种情况,您可以尝试:

a:hover {
    position: relative;
}
a:hover:after {
    content: url(image/circle.PNG); /* no need for qoutes */
    display: block;
    position: absolute;
    left: 123px; /* change this value to one that suits you */
    top: 56px; /* change this value to one that suits you */
}

关于css - 使图像出现在链接悬停 css 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19270594/

相关文章:

html - 响应时间轴

html - 将登录表单 float 到导航右侧

css - Wordpress:子页面的内容似乎被隐藏了

facebook - 如何获取群组墙上单个帖子的链接?

javascript - 将现有单选按钮更改为 <a href>

javascript - 幻灯片显示不正确的问题

Javascript 拖放代码适用于 div,但不适用于 img

ios - 为什么 Google Chrome 模拟器以 375x667 分辨率显示 iPhone 6?

ios - 上传到 Firebase 时的图片方向问题

css - 无法使用内联 CSS 和电子邮件签名删除超链接下划线