Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:08:22+00:00 2026-06-12T13:08:22+00:00

I have many elements with the same class on a web page. I would

  • 0

I have many elements with the same class on a web page. I would like to highlight all these elements when I hover one of them. How can I do that in CSS?

Right now, I have this CSS:

p.un:hover { background-color:yellow;}

And my HTML:

<div class="book">
  <div class="page left">
    <p class="un">Karen…</p>
  </div>
   <div class="page right">
     <p class="un">Karen ne se retourne pas. Mme Tilford reste là, apparemment confuse et abattue.</p>
   </div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:08:23+00:00Added an answer on June 12, 2026 at 1:08 pm

    The best you can do using pure CSS is this:

    .classname:hover ~ .classname {
        background-color: yellow;
    }
    

    But this only highlights all siblings with a class classname after the hovered element.

    You have to use JavaScript to highlight all elements;

    var elms = document.getElementsByClassName("classname");
    var n = elms.length;
    function changeColor(color) {
        for(var i = 0; i < n; i ++) {
            elms[i].style.backgroundColor = color;
        }
    }
    for(var i = 0; i < n; i ++) {
        elms[i].onmouseover = function() {
            changeColor("yellow");
        };
        elms[i].onmouseout = function() {
            changeColor("white");
        };
    }
    

    If you have multiple classes and want to generalize this, use something like this:

    var classes = ["one", "two", "three"]; //list of your classes
    var elms = {};
    var n = {}, nclasses = classes.length;
    function changeColor(classname, color) {
        var curN = n[classname];
        for(var i = 0; i < curN; i ++) {
            elms[classname][i].style.backgroundColor = color;
        }
    }
    for(var k = 0; k < nclasses; k ++) {
        var curClass = classes[k];
        elms[curClass] = document.getElementsByClassName(curClass);
        n[curClass] = elms[curClass].length;
        var curN = n[curClass];
        for(var i = 0; i < curN; i ++) {
            elms[curClass][i].onmouseover = function() {
                changeColor(this.className, "yellow");
            };
            elms[curClass][i].onmouseout = function() {
                changeColor(this.className, "white");
            };
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page with many elements that have the same class attached to
I have two lists based on the same class. I would like one list
i have a json file with many elements like these: { code : hfuiew89,
What I'm doing is adding a class to all elements who have the same
how can i do. i have many html elements on page. after clicking on
I have a page consisting of many elements with alphanumeric ids such as this:
I have a bunch of elements like the following: <div class=droppableP id=s-NSW style=width:78px; height:63px;
Let's say I have many items with click event (all have same element name
I have models with many to many relationships like this: class Contact(models.Model): name =
I have many variable-sized lists containing instances of the same class with attribute foo,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.