Ways to place an image above a link when hovered over

How can I create a rollover effect in my navbar using CSS? I want a light bulb icon to appear above a link when the user hovers over it.

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

Answer №1

To ensure that the light bulb is positioned exactly above the text, one solution could be to place the background code within a pseudo-element so that it can be easily aligned with the text.

This concise code snippet creates some space above the element using padding - the actual implementation may vary based on the specific layout considerations, such as positioning relative to a navbar.

a {
  font-size: 16px;
  padding-top: 20px;
  display: inline-block;
  position: relative;
}

a::before {
  content: '';
  position: relative;
  height: 20px;
  aspect-ratio: 1 / 1;
  left: 50%;
  top: 0;
  transform: translate(-50%, -100%);
  display: inline-block;
}

a:hover::before {
  background-image: url('https://i.sstatic.net/dp2LF.png');
  background-repeat: no-repeat;
  background-position: center top;
  background-size: contain;
}
<a href='#'>Hover</a>

Answer №2

body {
  margin: 3rem;
}

nav {
  display: flex;
  gap: 2.5rem;
}

nav div {
  cursor: pointer;
}

nav li {
  position: relative;
  list-style: none;
}

nav li::before {
  content: '';
  position: absolute;
  width: 30px;
  height: 30px;
  left: calc(50% - 15px);
  top: -30px;
  display: none;
  background-image: url('https://media.istockphoto.com/vectors/light-bulb-icon-vector-light-bulb-ideas-symbol-illustration-vector-id1167459261?k=20&m=1167459261&s=170667a&w=0&h=3KSKkFUNQoPvoS70bzIWeT1fXRuB9rzap6VC1HipCgg=');
  background-size: contain;
}

nav div:hover li::before {
  display: block;
}
<nav>
  <div>
    <li>List Item</li>
  </div>
  <div>
    <li>List Item</li>
  </div>
</nav>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A stateless component in React must always return a valid React element or null

I am a beginner with ReactJS and I am facing an issue. My goal is to showcase Hello world using the code snippet provided below, however, I keep encountering this error message: Can someone guide me on what I might be overlooking? The following is the c ...

Utilize the display: flex property to position a div in the bottom right corner of its containing element

I'm trying to showcase a yellow div "icon" at the bottom right corner of its red container "content". Can someone help me figure out what's incorrect in my code? .root { display: inline-flex; background-color: red; } .content { d ...

I am looking for a way to batch upload several images from an HTML page to a nodejs server, where I can then rename and

Exploring the world of nodejs, I am seeking guidance on updating multiple images from HTML to node js. Specifically, how can I rename each image differently and save them to a directory? Alternatively, is there another solution that would better suit my ne ...

Having trouble getting static files to load in Python Django

I've been working on a Python Django project and I've been attempting to enhance its style using a styles.css file, but I'm facing some challenges. The project I'm working on is named "commerce" and the app is called "auctions". The st ...

How to Align Text at the Center of a Line in Three.js

Exploring What I Possess. https://i.sstatic.net/nAtmp.png Setting My Goals: https://i.sstatic.net/svcxa.png Addressing My Queries: In the realm of three.js, how can I transform position x and y into browser coordinates to perfectly align text in th ...

Just starting out with CSS and coding. Setting up radio buttons and divs for a simple beginner's gallery

One challenge that perplexes me is how to reposition the concealed divs below the radio buttons with labels. Check out my HTML here View my CSS code here /*color palette: abls [lightest to darkest] #eeeeee #eaffc4 #b6c399 ...

The pseudo element 'not' in CSS is currently not functioning as expected when applied to an anchor tag

When working with CSS, I encountered an issue while trying to apply a pseudo element on hover and active states of anchor tags. Unfortunately, my code was not producing the desired outcome. a:not(a[name="nobgcolor"]):hover{color:#ffff35;background-color:# ...

Using select tags in conjunction with JavaScript can add dynamic functionality to your website

I've been working on adding dropdown menus to my website and have been using code similar to this: <select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</opti ...

Looking to integrate the date, month, and year selection tags into the inline format

The Edit User Profile page includes a Birthday field, which is currently displayed vertically. I am looking to change the layout so that the Birthday field appears horizontally. Here is the code I am using: Within visitors/edit.html.erb, <%= simple_f ...

JavaScript / html Error: function body missing closing curly brace

I encountered an error that I'm struggling to resolve: "SyntaxError: missing } after function body". Despite not having a function named 'body', I have attempted changing every instance of 'body' in my script. However, ...

Viewing a Google Charts graph upon the loading of a web page

Utilizing the Google Charts library, I have incorporated a graphic on my web page that is dynamically added using AJAX into a <div> element. To display the graph when the page loads, I have written the following code: <script type="text/ ...

Is there a way to verify the content of a span and automatically guide the user elsewhere if it meets certain criteria?

Currently, I am facing an issue with adding Google authentication to my website as the redirect function is not working properly. As a solution, I plan to implement manual redirection using JavaScript in case the value of the span element is <span id= ...

How can the 'selected' attribute be assigned to 'select/option' tags depending on the option value?

In my code, I have a select input with various options and values. I want to set the 'selected' attribute for the option that corresponds to a specific value X. Here is an example: // If x=3, I want the option with value=3 to be marked as 's ...

Unable to Display Bootstrap 4 Navbar on Hamburger Menu

I am having an issue with my mobile-sized site. Whenever I click the hamburger menu, the Navbar does not appear. I have double-checked and my data-target matches the nav id, so I am not sure why it is not working. <section id="nav"> <na ...

Enhancing User Experience in Bootstrap 4: Making Dropdown Parents Clickable Links

<nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#">Navbar</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbar ...

Activate Materialize Css Tooltip with a click to reveal its content

How can I make the tooltip appear when clicked? Here is my code. Your assistance would be greatly appreciated. <a href="javascript:;" class="tooltipped info_circle" data-position="bottom" data-delay="50" data-tooltip="Numbers should MUST be 5 digits. N ...

Creating a vertical scrollable container that spans the full height of the PDF document

I am currently working on embedding a PDF file into my website. I'm trying to hide the status bar, including download buttons and page numbers, through some clever techniques so that I can control them externally. My goal is to be able to redirect use ...

Switching up the styles in PHP-embedded HTML by tweaking the CSS

I used PHP to create HTML elements by echoing them out, but I ran into an issue when trying to change the CSS for those HTML elements. Can you help me figure out how to do this? The PHP } else { include("steamauth/SteamCo ...

What is the best way to avoid cluttering the URL with a lengthy list of HTML code when adding a navigation bar to a website

I have created a navigation bar with base.html, styles.css, and main.js. Within the navigation bar, I have included links to about.html and contact.html using: {%block content%} ... {%endblock%} However, when I click on the "About" link in the navigatio ...

What is the best way to confirm checkbox selection based on MySQL data?

Writing this question feels challenging, but I have a collection of checkboxes with their data stored as JSON in my PHP database. What I'm trying to achieve now is to dynamically load the JSON data on the page without refreshing it, checking specific ...