CSS class malfunction - various classes with identical functions

I'm new to the world of web development and design, embarking on a journey to learn all I can in these fields.

Recently, I attempted to create unique hover styles for each menu button within my CSS classes-based menu list. However, I encountered an issue as nothing happens when I hover over the buttons.

In my initial attempt, I achieved the desired hover effect without using classes by simply writing `li:hover {...}` directly in my CSS stylesheet. Here is a snippet of my code:

<div id="menu">
<ul style="padding-top:80px;" >
<li class="class0"> <p style="padding-left:17px; padding-top:10px;">Main</p></li>
<li class="class1"> <p style="padding-left:19px; padding-top:10px;">About</p></li>
<li class="class2"> <p style="padding-left:22px; padding-top:10px;">Minigames</p></li>
<li class="class3"> <p style="padding-left:25px; padding-top:10px;">Exit</p></li>
</ul>
</div>

Below is the snippet from my CSS code:

li {
    color:white;
    font-weight:normal;
    font-size:x-large;
    height:60px;
}

.class0 li:hover {
    background-repeat:no-repeat;
    color:black;
    font-weight:800;
    background-image: url(./img/menu_hover_0.png);
}
.class1 li:hover {
    background-repeat:no-repeat;
    color:black;
    font-weight:800;
    background-image: url(./img/menu_hover_1.png);
}
.class2 li:hover{
    background-repeat:no-repeat;
    color:black;
    font-weight:800;
    background-image: url(./img/menu_hover_2.png);
}
.class3 li:hover{
    background-repeat:no-repeat;
    color:black;
    font-weight:800;
    background-image: url(./img/menu_hover_0.png);

Your assistance with this issue would be greatly appreciated. Thank you in advance!

Answer №1

Follow this structure for your classes:

li.classX:hover{

Be sure to go over the CSS selector documentation thoroughly.

Answer №2

li.class0:hover is a better choice than .class0 li:hover. The former targets the specific element with class class0, while the latter would require the li to be nested inside an element with that class.

I made some optimizations to your CSS:


li {
    color: white;
    font-weight: normal;
    font-size: x-large;
    height: 60px;
}
li:hover {
    background-repeat: no-repeat;
    color: black;
    font-weight: 800;
}
li.class0:hover, li.class3:hover {
    background-image: url(./img/menu_hover_0.png); /* I combined these two classes since they had the same image */
}
li.class1:hover {
    background-image: url(./img/menu_hover_1.png);
}
li.class2:hover {
    background-image: url(./img/menu_hover_2.png);
}

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

How can I use XPATH to target a div after choosing a nested element?

I had the task of creating a universal identifier to locate a dropdown menu following a label. There are 10 different forms, each with a unique structure but consistent format, which means I cannot rely on specific IDs or names. Instead, my approach was to ...

Choose the first and last div element in the selection

Is there a way to target the first and fourth div elements using the :nth-child selector for styling purposes? .main{ width:460px; height:240px; } .box{ width:100px; height:100px; background:red; float:left; margin:10px; } /*I tried */ . ...

The troubleshooting of compatibility issues between TailwindCSS and HTML Input and Button tags

Recently, I decided to switch from using plain CSS to TailwindCSS for styling the navbar component that I obtained from the Bootstrap website. While TailwindCSS has helped me style most parts of the navbar successfully, I have encountered a challenge with ...

Ways to ensure a form label is accessible via keyboard navigation

My HTML5 form collects user information and processes it upon submission. With the help of the jQuery Validation Plugin, input validation is performed. In case of errors, an error-message-container div is displayed at the top of the screen, listing all err ...

What is the best way to retrieve the unique identifier of dynamically created divs and showcase a message based on that identifier?

Is it possible to retrieve the id of a dynamically generated div and show a unique message when each div is clicked in the code snippet found at this link? function get_random_color() { var letters = '0123456789ABCDEF'.split(''); ...

Unable to implement Bootstrap 5 Hamburger Animation - Looking for solutions

SOLVED I successfully resolved the issue :) The challenge was: $(function () { $('.navbar-toggler-button').on('click', function () { $('.animated-hamburger').toggleClass('open'); //Chrome expert mode ...

Loading images in advance using JavaScript

I have been searching through multiple forums and cannot seem to figure out the issue. The problem I am encountering is related to a website where there are three images with a hover effect. When the page first loads, there is a delay in loading the backgr ...

Declaration of Character Encoding in CSS3

Do I need to include a character encoding declaration in CSS3? When I add the @charset "utf-8"; declaration, Visual Studio's CSS3 validation displays a warning. Can anyone offer some guidance on this issue? ...

Changing the CSS property of a single table cell's innerHTML

I have a question that may seem silly, but I'm going to ask it anyway. Currently, I am iterating through a list of strings that follow the format "1H 20MIN" and adding them to table cells using the innerHTML property like so: for (i = 0; i < list ...

Can Rollup be utilized solely for processing CSS files?

I am curious about whether Rollup can be used solely for processing CSS files, such as css, scss, less, etc. For example, if I have a file called index.css in my src folder (the entry folder) and want Rollup to process it in the dist folder (the output fol ...

How to choose an option from a dropdown menu using React

In a React application, I am creating a straightforward autocomplete feature. The code is outlined below. index.js import React from 'react'; import { render } from 'react-dom'; import Autocomplete from './Autocomplete'; co ...

List the acceptable favicon formats for Internet Explorer version 10 and above

When I say type, I'm referring to the file extension. My favicon displays correctly in Firefox, Chrome, and Safari, but someone suggested that the issue may be related to the favicon type. Is there a reliable online resource where I can find informa ...

Ensuring the checkbox is disabled prior to editing

Check out my table below: https://i.stack.imgur.com/7byIa.png Whenever I click the edit button, I can modify the status field and action field. This feature works correctly. However, the issue is that I am able to change the values of status and action e ...

Excessive content spilling over into the footer area due to an integrated forum feature within

We have integrated a Muut forum into our website, and it is working well when embedded in a coding block within the page's body. However, there is an issue where the sidebar of the forum gains extra features when logged in as an admin. This causes the ...

Adjusting Window Size Causes White Space to Appear Above Div

I currently have two inline-block div in my post page that showcase my latest book reviews. Typically, these sections align perfectly and occupy the same amount of space on the screen. However, for certain window sizes, one of the inline-block elements end ...

Creating Responsive Headers Using CSS for Mobile Devices

I'm looking to make my header font size of 60px more responsive for mobile devices so that the text doesn't get cut off. Any suggestions on how to achieve this? Thank you! Here's the code snippet: h1 { font-size: 4.286em; /* 60px */ margi ...

What is the best method to update numerous low-resolution image sources with higher resolution images once they have finished loading?

I'm currently developing a website that implements a strategy of loading low-resolution images first, and then swapping them for high-resolution versions once they are fully loaded. By doing this, we aim to speed up the initial loading time by display ...

What is the best way to locate and extract the content of an HTML element using XPath in Selenium with VBA?

I am currently seeking a solution to extract the content of an element with the name "data-testid" from a website. This specific element appears approximately 35 times within various contexts in the HTML code. The particular element I am interested in look ...

The HTML output from converting Tiny MCE text content is not rendering properly on the React app's front-end

I'm currently working on a blog project using React and Material UI. In order to enable users to add posts, I've integrated a TinyMCE rich text editor onto the page. The issue arises when trying to display a specific blog post, as the content app ...

Flask caches JSON files automatically

I am currently working on a visualization app using js and python. The functionality of my app is as follows: There is a textbox in the browser where I input an URL The URL is then sent to Python via Flask In Python, the URL is processed to create ...