如何在悬停时在链接上方添加图像?
我的页面顶部有一个导航栏,我希望出现翻转效果,当您将鼠标悬停在链接上时,链接上方会出现一个灯泡图标。使用 CSS 实现此目的的最佳方法是什么?
.navbar .nav a, .navbar .nav .a {
font-size: 16px;
}
.navbar .nav a:hover, .navbar .nav .a:hover {
background-image: url('lightbulb.png');
background-repeat: no-repeat;
background-position: center top;
background-size: contain;
display:block;
}
I have a navbar at the top of my page and I would like a rollover effect to occur where when you hover on a link, a light bulb icon appears directly above the link. What is the best way to achieve this with CSS?
.navbar .nav a, .navbar .nav .a {
font-size: 16px;
}
.navbar .nav a:hover, .navbar .nav .a:hover {
background-image: url('lightbulb.png');
background-repeat: no-repeat;
background-position: center top;
background-size: contain;
display:block;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了确保灯泡恰好位于文本上方,我想我会将背景代码放入伪元素中,以便可以将其放置在文本顶部。
这个简单的代码片段使用填充在元素上方留下了一些空间 - 这完全取决于您在实际示例中想要如何做到这一点,当然可能是导航栏的定位?
To be sure of getting the light bulb exactly above the text I think I'd put your background code into a pseudo element so that it can be positioned on top of the text.
This simple snippet leaves some space above the element using padding - it all depends on how you want to do this in the real example of course with the positioning of navbar perhaps?