Use CSS to apply hover effects to multiple sections of text at the same time

Is there a way in CSS to apply the hover state to one part of HTML text when another part is hovered over, both sharing the same class?

I have a block of text in HTML that is broken down into individual words, each word linked to a specific CSS class. It is possible for multiple words to be linked to the same class.

For example, if I have three words and two classes (classA, classB),

word1, word3 -> classA
word2 -> classB

This would result in the following HTML code :

<span class=classA>word1</span>
<span class=classB>word2</span>
<span class=classA>word3</span>

The issue at hand is changing the appearance of all words with a shared class when any one of them is hovered over.

I attempted the following:

.classA {
    color: red;
  }
.classA:hover {
    color: blue;
  }

Unfortunately, when the mouse hovers over "word1," only "word1" is highlighted, and not "word3" which belongs to the same class ("classA").

Any assistance on this matter would be greatly appreciated!

Answer №1

Simply put, the answer is NO, using only CSS. However, there are alternative solutions that I will share with you below.

You can utilize the adjacent selector to simultaneously apply the :hover effect:

.classA:hover + .classB + .classA {
    color: blue;
}

See Demo Here

Unfortunately, this method will only work when you hover over the first group element. Another approach is to use a wrapper element, but this may be restrictive if you have only two combinations of classes where you wish to style a single type of class.

.wrapper_class:hover .classA {
   color: blue;
}

View Demo 2

Answer №2

.classA:hover {
  color: blue;
}

This particular code affects elements with the class=classA that are being hovered over. Using JavaScript might be a more straightforward approach:

var span = document.getElementsByClassName('classA');
var i = 0;
while(i < span.length){
    span[i].onmouseover = function change(){
        var i = 0;
        while(i < span.length){
            span[i].style.color = 'blue';
            i++;
        }
    }

    span[i].onmouseout = function change(){
        var i = 0;
        while(i < span.length){
            span[i].style.color = 'red';
            i++;
        }
    }

    i++;
}

http://jsfiddle.net/fa7d0/Bt8eN/1/

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

What are the steps to creating a fading effect for an item that appears as it enters the field of view and disappears as it exits the field of view?

Implementing a fade-in effect for a button as the user scrolls down and the button comes into view, along with a fade-out effect as the user continues scrolling down and the button goes out of view. var fadeInButton = $('#fadeInButton'); ...

Guide on Modifying the CSS File of an Installed Plugin on WordPress

I am looking to customize the design of a WordPress plugin by editing its CSS file. When trying to locate the CSS file of the installed plugin through Plugins --> Installed Plugin --> Editor, I could only find .php files instead of the desired CSS f ...

Allowing breaks with the :nth-child() selector

There must be a straightforward explanation for this perplexing issue. However, in my view, it ought to function as intended. We are currently utilizing the Bootstrap 3 CSS framework. The following code is present on a specific page: <div class="promo ...

Include a return or delete from the default IO statement

I am utilizing an intersection observer that alters the header's font-color and background-color based on the content intersecting with it. This change is determined by the presence of data-color and data-background attributes declared on the div/sect ...

Troubleshooting issues with mail subscriptions using Ajax and PHP

My Idea for Implementation I successfully set up a subscription form using PHP initially. However, I wanted to enhance the user experience by implementing an Ajax feature that would display a nice image and a "thank you" message without reloading the pag ...

Getting rid of the outline on <html> while tabbing in Firefox

An odd outline keeps appearing as I tab through elements on a website, but only in Firefox and not in Chrome, Safari, or Opera. To identify the element causing the focus, I used Firebug console by typing: document.activeElement. It showed: >>> d ...

Issues with CSS and JavaScript functionality persist within the Flask framework

Having trouble with CSS and JavaScript on my website hosted via Flask framework on PythonAnywhere. Here is the directory structure: home/dubspher/mysite/ - README.md - app.py - index.html Static/ - css - fonts - images - js - sass Original HTM ...

Switch up the placement of the boxes by moving them in different directions, such as

I've been attempting to recreate the button movement demonstrated in this link , but I'm having trouble achieving it. Using CSS animation, I can make the buttons move in a straight line, here's what I have so far: <div id="box" style=&ap ...

Increasing the size of a card beyond the container using the CSS PRE tag

I am facing an issue with a card on my webpage. It contains a pre tag inside it, which is causing the size of the card to extend beyond its container, resulting in a horizontal scroll bar appearing. How can I make the content within the pre tag scroll hori ...

What is the best way to blend a portion of an image into a div?

Having a bit of trouble with overlapping my image onto another div in my current project. When you take a look at what I've been working on, you'll see there's an image that says "That's me!" and it needs to overlap the left side div. I ...

Add option button

Is there a way to dynamically add radio buttons using jQuery or JavaScript and save the data into a database? I have successfully appended input types such as text, textarea, checkbox, and select with options. Here is my code: <!DOCTYPE html> < ...

I am looking to automatically add a line of HTML text to the notifications list without requiring the user to manually refresh the page

Having trouble articulating the correct term or language I need, making it difficult to search. Q: How can I push a line of text from the server side to the user? I am developing an MVC 4 application using asp.net and c# in .net4. One page will feature a ...

Generating a PDF from a CSS3 multi-column layout with the help of snappy (a wrapper for wkhtmltopdf)

I am attempting to create a PDF with three columns using barryvdh/laravel-snappy and wkhtmltopdf. Since the text that will populate these columns varies in length, I need a method that allows the text to flow freely within the columns. I have attempted to ...

Selenium for handling multiple input fields

Having some trouble figuring out how to input values for selecting a building. Initially, there are two empty input fields for selecting a building. As you enter one, it should add an additional empty input field. I'm struggling with the indexing and ...

Modifying the content of a paragraph by incorporating information from various sources

I need a way to input measurements for my items in text fields, and then with the click of a button, transfer those measurements into a paragraph in corresponding fields. The code I am currently using is shown below: <!DOCTYPE html> <html> & ...

Is jQuery causing the table to exceed the page width?

I have a table in my HTML document that is displaying too many columns in a single row (~25), causing the table to stretch beyond the size of the page and creating unwanted scrollbars. I am seeking a solution to shrink the content of each column while also ...

Define the width of jqgrid

I have been utilizing SmartAdmin jqgrid to showcase data in tabular format. The number of columns in the datatable changes dynamically each time. I am converting the DataTable to a JSON object and assigning it to the jqgrid. However, it appears that jqgrid ...

The image slider script I've built is functioning perfectly in Codepen, but unfortunately, it's not working as

My image slider called Orbit is functioning properly on Codepen.io, however when I try to run the exact same code on Plunker, it doesn't work at all. <ul class="slider" data-orbit> <li> <img src="http://foundation.zurb.com/docs/a ...

Tips for properly showcasing images on a webpage after ensuring all other content has loaded

Is there a way to have an image load last on a webpage after all other content has been loaded? I have an image that is retrieved from a database when a button is pressed, but I would prefer for the entire page to load first and then display the image. C ...

Efficient Django Template: Accurate Conversion of Values through Dictionary Searching

I have an object with an attribute that has specific choices defined in its class: class StashURLJobRequest(models.Model): STATUS_CHOICES = ((0,"Requested"),(1,"Done"),(2,"Failed")) url = models.URLField() created = models.DateTimeField(auto_n ...