Having trouble with image hover functionality on pure CSS?

<div id="headermenu">
    <ul >
        <li id="menu1"><a href="#"><img src="images/menu1.png"/></a></li>
        <li id="menu2"><a href="#"><img src="images/menu2.png"/></a>
            <ul class="submenu2">
                <li><a href="#">submenu2</a></li>
                <li><a href="#">submenu2</a></li>
                <li><a href="#">submenu2</a></li>
                <li><a href="#">submenu2</a></li>
                <li><a href="#">submenu2</a></li>
                <li><a href="#">submenu2</a></li>
                <li><a href="#">submenu2</a></li>
                <li><a href="#">submenu2</a></li>
            </ul>
        </li>           
        <li id="menu3"><a href="#"><img src="images/menu3.png" /></a></li>
        <li id="menu4"><a href="#"><img src="images/menu4.png"/></a></li>
        <li id="menu5"><a href="#"><img src="images/menu5.png"/></a></li>
    </ul>   
</div>

css

#headermenu ul ul {
    display: none;
}
#headermenu ul{
    padding:0; 
    margin:0; 
    white-space:nowrap;
}
#headermenu ul li{
    width: 20%; 
    list-style-type:none; 
    display:inline-block;
    margin-bottom:15px;
    float:left;
    left:0;
}
#menu1:hover{
    background: url('images/menu1hover.png');
}

Hover functionality is not functioning properly, I am curious about how to implement hover effects on a list with images, as well as how to create a sub-list when hovering over an item. Furthermore, I would like to know the process of adding another list within a sub-list using only CSS.

Answer №1

It seems like there might be a problem with the way the <img> element is positioned on top of the background, making it invisible when hovered over. Without all the necessary information, I can only make an educated guess.

Your HTML:

<div id="headermenu">
    <ul>
        <li id="menu1"><a href="#"><img src="http://img.gawkerassets.com/img/19euo1gaaiau9jpg/original.jpg"/></a>
        </li>
    </ul>
</div>

Your CSS:

#headermenu ul ul {
    display: none;
}
#headermenu ul {
    padding:0;
    margin:0;
    white-space:nowrap;
}
#headermenu ul li {
    width: 20%;
    height: 50px;
    list-style-type:none;
    display:inline-block;
    margin-bottom:15px;
    float:left;
    overflow: hidden;
    border: 1px solid;
}
#menu1:hover {
    background: url('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg');
}

In the provided code, the background image changes upon hover but remains unseen due to the presence of the <img> element in front of it.

View Demo Here


If we remove the <img> element from the code, we can see that the hover effect now works properly.

<div id="headermenu">
    <ul>
        <li id="menu1"><a href="#"></a>
        </li>
    </ul>
</div>

View Updated Demo Here

This change confirms that the issue was caused by the <img> element blocking visibility of the background image on hover.


Possible Solutions:

You can assign a background to each li and define a separate background for hover states.

CSS:

#menu1 {
    background: url('http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg');
}
#menu1:hover {
    background: url('http://img.gawkerassets.com/img/19euo1gaaiau9jpg/original.jpg');
}

Try This Solution

Another approach could involve hiding the img element when hovering over the menu item.

CSS:

#menu1 {
    background: url('http://icdn4.digitaltrends.com/image/microsoft_xp_bliss_desktop_image-650x0.jpg');
}
#menu1:hover img {
    display: none;
}

Alternative Solution Here

These are just a couple of ways to address the issue. Experiment with different solutions to find what works best for your specific case.

Answer №2

To effectively display before and after states when hovering over #menu1, consider implementing a background image or using an actual img for both instances. Currently, the background behind the menu1.png image changes upon hover, but the menu1.png itself obstructs it from view.

Answer №3

Check out this code snippet
DEMO

<div> <p>Here is a link to a cool website: <a href="http://www.mysite.com" class="siteLink" title="My site"></a> </p> </div>

.siteLink { width: 180px; height:160px; display:block; background-image: url("http://imgsrc.ru/images/reco/140/windkitten_38083968.jpg"); } .siteLink:hover { background-image: url('http://placehold.it/180x160'); }

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

Is it considered best practice to utilize iframes for the implementation of a header or navbar on a

Is it considered a best practice to utilize iframes for implementing a header/navbar? My website consists of approximately 5 thousand pages, all of which are static HTML (without the use of any content management system, PHP, etc). I am currently in the ...

Invoke a JavaScript function once the div has finished loading

After clicking on a radio button, I am dynamically loading a div element. I want to hide a specific part of the div once it is loaded. $(function($) { $('.div_element').on('load', function() { $('.textbox').hide(); } ...

Update the contents of a neighbor div within the same parent element using jQuery

Attempting to set the range slider value within the slider parent sibling var updateRangeSlider = function() { var slider = $('.range-slider'), range = $('.range- value = $('.range-slider__value'); sli ...

Failing to verify the presence of specific text within a dropdown menu using Selenium

Previously, I successfully implemented this code, however, the HTML/CSS for the dropdown has since changed and now I am unable to get it to function correctly. Below is the structure for the dropdown code, with specific text highlighted that I am trying t ...

Incorporate a fontawesome icon into a dynamically created button using ajax

Is it possible to insert fontawesome icons into a button created using this code snippet? $.each(response, function (i, item) { trHTML += '<tr><td>' + item.name + '</td><td>' + "<input type='button&apo ...

Switch the hover effect to be activated by clicking

Recently, I discovered an incredible plugin created by the talented developer janko. It has been a fantastic addition to my project. The only issue I've encountered is that I do not want the default hover style. Is there a way to transform it into a ...

Prevent Scrolling on Body with W3 CSS

While implementing scroll body lock packages like body-scroll-lock, react-scrolllock, or tua-body-scroll-lock alongside the W3 CSS package, I have encountered an issue where the body remains unlocked even when the designated element is active or open (e. ...

Why Changing the Width of a Flexbox Container Doesn't Impact Its Children?

Attempting to use TweenLite to animate the width of the blue sidebar down to zero, however facing an issue where the content breaks outside the parent's bounds. https://i.stack.imgur.com/4rEVr.png It is unusual for this to happen with Flexbox, given ...

Styling in CSS is being applied to a button element within a React component,

Having trouble with a button styled with the className 'actions' The button displays the CSS styling from '.actions', but not '.actions button'. Both should be applied. This code snippet works for all elements ...

Utilizing a jQuery plugin for input file validation

This code is functioning properly, however the file field is not being validated. All input fields are successfully validated by this plugin except for the file type field. I believe there may be something missing in my implementation, so please help me de ...

The JQuery duplication process did not function as anticipated

This question builds on a previous one related to jQuery cloning and incrementing IDs. The issue is that when adding new elements, the IDs are not being generated in the correct sequence. For example, if the initial ID is expy-1, clicking "add more" should ...

How can I make CSS text-overflow ellipsis trigger a pop-up to display lengthy text when clicked?

I'm encountering an issue with a lengthy sentence in a table cell which I've truncated using text-overflow to display ellipsis. Is there a way to enable users to click on the dots (...) and view the full text in a popup window? table { wi ...

Best practices for making an AJAX call to fetch information from a database

I have a database containing a single table. The table includes columns for Company and Time, among others, with Company and Time being crucial. Users can make appointments by filling out a form. Within the form, there are 2 <select> elements - one ...

Choosing a single radio button value within a nested ng-repeat loop in AngularJS

Help needed with selecting only one radio button value in nested ng-repeat. Please review the source code and suggest any potential mistakes. <form ng-submit="save()"> <div ng-repeat="x in surveyLst"> <div class="row"> < ...

Issue with Bootstrap v3.3.6 Dropdown Functionality

previewCan someone help me figure out why my Bootstrap dropdown menu is not working correctly? I recently downloaded Bootstrap to create a custom design, and while the carousel is functioning properly, when I click on the dropdown button, the dropdown-menu ...

iOS emphasizes special characters by printing them in bold

Some parts of the website I am managing are showing German umlauts in bold (as seen in the screenshot). The font style being used is font-family: Source Sans Pro, Arial, sans-serif; with font-weight: 300. This font is sourced from Google Fonts. When I cha ...

Deactivating the submit button after a single click without compromising the form's functionality

In the past, I have posed this question without receiving a satisfactory solution. On my quiz website, I have four radio buttons for answer options. Upon clicking the submit button, a PHP script determines if the answer is accurate. My goal is to disable t ...

The jQuery scrollTop function seems to be malfunctioning following an ajax request

My ajax call is functioning properly, but I am experiencing an issue with the scrollTo plugin from JQuery. It is not positioning the content where I want it to be located below. //This function reveals the email body when a list item is clicked. jQue ...

Lagging speeds in client-side template rendering using Rivets.js

I have a function that renders an array of around 1000 objects, but the html bindings are causing significant performance issues. It currently takes about 5 seconds to complete rivets.bind(). Does anyone have any recommendations for improving performance? ...

What is causing my Javascript media query for the sidebar to not function properly?

I am working on implementing a sidebar that slides out 250px using JavaScript on the desktop view. However, I want the sidebar to take up 100% width when viewed on mobile devices. Despite my attempts to use Media Queries in JavaScript to achieve this, I am ...