How to change the hover effect of a span with CSS

I am currently attempting to modify the hover effects of my spans. Due to a specific requirement, I find myself needing to assign the same id's to all <li> elements that contain my spans. This results in all of the spans having the same parent id:

<li id="li_id">
    <span><a href="/some/link.html">Link title</a></span>
</li>
<li id="li_id">
    <span class="anotherlink"><a href="/another/link.html">Another Link title</a></span>
</li>

I have researched methods to override id's using classes, such as this one: Can I override a #id ul li behavior with a class definition , but I have been unsuccessful in making them work.

CSS:

#primary_nav #home li#li_id>span:hover {
    background-image: url(this_image.png);
}

//This is for my first link
#primary_nav #home li#li_id .anotherlink >span:hover {
    background-image:url(another_image.png);
}

//This is for the other link.

Am I using the correct syntax? The styles do not seem to apply correctly at the moment, and I am unsure about the CSS for the other link.

NOTE: I understand that this may seem unconventional, as typically classes should be used with different id's. However, I am working on an existing site and need to implement temporary solutions like this until a new site is deployed. Therefore, I just need to ensure that this site is updated until the replacement site is live.

Answer №1

A common mistake is using two CSS IDs, which should be unique. To apply styling to multiple elements, it's best to use classes. Classes are designed for repeated usage, while IDs should be reserved for single, unique elements. Always keep this distinction in mind when working with CSS.

Answer №2

Changing the IDs of all the spans was a tough decision, but I managed to make quick adjustments using CSS and grouped the new IDs under a single selector.

 #primary_nav #home li#li_id-1 , #primary_nav #home li #li_id-2 {...}

Although everything is functioning correctly now, I realized that my approach was not the most efficient. Instead of using multiple IDs in a single selector, I could have simply grouped them under a single class.

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

Analyzing the performance differences between HTML5 and Flash applications

Recently, I created both an HTML5 app and a Flash app for research purposes. Now, I am looking for a reliable method, possibly in the form of software, to efficiently test and compare the performance of these systems. ...

Ensure that the div spans the entire width of the page, prioritize maintaining the aspect ratio, but allow for variable

I have a dilemma with a div containing multiple elements. My goal is to make the div 100% in width, while also attempting to preserve the aspect ratio if feasible. Should the enclosed elements exceed the div's boundaries, then the height will adjust a ...

"Enhance Your Website with Creative Image Hover Effects using HTML

I'm looking to add a simple hover effect to the images AboutUsLink.png and AboutUsColourLink.png, but I'm unsure of the process. Here is the HTML code: <section class="link1"> <a href="#SecondLink"><img src="images/LogoAndLin ...

Similar to the :has() in jQuery, the equivalent in CSS

Consider the code snippet below: <div class="section"> <div class="row">...</div> <div class="row"> <!-- bottom margin here needs to be 0 --> <div class="section"> &l ...

Combining header and data elements within the same row by using the set_template function in the CodeIgniter

Utilizing the HTML Table Class from CodeIgniter, I am working on creating a table template. The example provided in the documentation is as follows: $tmpl = array ( 'table_open' => '<table border="0" cellpaddi ...

Displayed in the xmlhttp.responseText are the tags

Why do tags appear when I input xmlhttp.responseText into my textbox? It displays <!DOCTYPE html><html><body></body></html> along with the desired content. Is there a way to prevent the tags from being displayed? Here is the ...

How to Deactivate Horizontal Scrolling Using CSS in WordPress

Hey there, I'm currently working on a WordPress blog and facing an issue. Here's the link to my problem: When I resize the browser (X-Axis) to a minimum, like when using a mobile device, I noticed that I can scroll to the right in the Content se ...

The Bootstrap 4 alignment class for centering rows is not functioning as expected

<div class="row align-items-center remember"> <input type="checkbox" class="form-control" checked>Remember Me </div> <div class="row align-items-center"> <input type=& ...

Tags that adhere to W3C compliance for struts

I'm facing an issue with a web page created using struts tag libraries. When attempting to validate the page on w3c.org, all the struts tags are showing up as undefined. For example, <html:button> appears as undefined. The DOCTYPE I used is: &l ...

Why are my Chrome Div regions failing to expand to their full width?

I have been searching but couldn't find an answer to my issue, so I apologize if this question has been asked before. I am working on a simple site layout and noticed that two of the div regions do not expand fully in Chrome, however, they do in Firef ...

Step-by-step guide to creating a pop-up div that appears on hover and remains visible when clicked

I'm trying to figure out how to make a popup appear when the mouse hovers over it and then stay visible when clicked on. The issue I'm facing is that currently, the popup disappears when the mouse moves away from it. How can I modify the code so ...

Issue: Incorrectly calling a hook. Hooks can only be used within the body of a function component. Assistance needed to resolve this issue

import React, { useState } from "react"; const RegistrationForm = () => { const [name, setName] = useState(""); const [password, setPassword] = useState(""); const [email, setEmail] = useState(" ...

Using AJAX to dynamically edit and update PHP data

The information displayed on my index.php page is fetched from a database using the mysqli_fetch_array function. The data is presented in a table format with fields like Name, Age, Email, and Update. An edit button allows users to modify the data. Here is ...

Display the chosen alternative in a Bootstrap dropdown menu

I am currently facing an issue with the dropdown list in my bootstrap menu. <li class="dropdown"> <a aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">Chose option<span class="c ...

What is the best way to define file paths in a webpage to ensure that the same file works seamlessly on both server and

Currently, I am working on developing a website locally with the intention of later transferring it via FTP to my server. In my index.php file, there is a line that reads: <?php include($_SERVER['DOCUMENT_ROOT'] . "/includes/header.php");?&g ...

Achieving a consistent user experience across both mobile and desktop devices without using a separate mobile version or media queries

Is it possible to have the same URL website.com display completely different content depending on whether it is accessed from a mobile device or not? I am looking for a solution that does not involve using CSS media queries or creating a separate subdomai ...

Icons on the top banner that adjust to different screen sizes

I am still getting acquainted with Wordpress, so please bear with me if I use incorrect terminology or ask silly questions. I am eager to learn and improve. My website is focused on jewelry (using Kallyas premium), and I have a row of banner icons between ...

Achieving a consistent display of three cards lined up in a row

Is there a way to ensure that 3 cards are always displayed in a row on all device sizes? Currently, the layout breaks into another row when the screen is minimized. Thank you! function DisplayCards() { const [items, setItems] = useState([{}, {}, {}, {}, {} ...

Is there a way to have the menu displayed on a single line and consistently centered?

I am working on a menu that needs to stay centered at the top of the screen, with a consistent appearance and just one line, regardless of the screen width. However, I'm encountering an issue where the menu mysteriously transitions into a two-line for ...

Tips for implementing and utilizing onclick functions in EJS

My goal is to develop a trivia game with interactive features. I aim to enable users to click on an answer, which will trigger a border effect and increase the points variable. Below is the layout of the entire page: <% include ../partials/boilerp ...