Modify the attribute of numerous elements when hovering by implementing CSS transformations

I have a pair of adjacent div elements and I want to modify the background-color attribute of both when the user hovers over one of them.

Initially, both divs should have a background-color set to #d8d8d8, and this color should change to #cacaca on both divs when hovering over either one.

I was able to accomplish this using jQuery:

$("#FOO").hover
(
  function()
  {
      $(this).css("background-color","#CACACA");
      $("#BAR").css("background-color","#CACACA");
  },
  function()
  {
     $(this).css("background-color","#D8D8D8");
     $("#BAR").css("background-color","#D8D8D8");
  }
)

$("#BAR").hover
(
  function()
  {
      $(this).css("background-color","#CACACA");
      $("#FOO").css("background-color","#CACACA");
  },
  function()
  {
     $(this).css("background-color","#D8D8D8");
     $("#FOO").css("background-color","#D8D8D8");
  }
)
.buttons {
  border:1px solid black; 
  background-color: #D8D8D8;

  height: 100px;

  font-family: play;
  font-size: 30px;

  text-align:center;
  vertical-align: middle;
  line-height: 100px; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<div class="col-xs-10 buttons" id="FOO" style="border-right: 0px">
  <span style="padding-left:100px">FOO</span>
</div>
<div class="col-xs-2 buttons" id="BAR" style="border-left: 0px">
  <span>BAR</span>
</div>

Is there an alternative method to achieve this effect? Perhaps using only CSS?

Answer №1

If you want to style columns with a hover effect, you can wrap them in div and apply :hover on it:

.buttons {
  border:1px solid black; 
  background-color: #D8D8D8;

  height: 100px;

  font-family: play;
  font-size: 30px;

  text-align:center;
  vertical-align: middle;
  line-height: 100px; 
}

.row:hover > .buttons {
    background-color: #CACACA;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<div class='row'>
  <div class="col-xs-10 buttons" id="FOO" style="border-right: 0px">
    <span style="padding-left:100px">FOO</span>
  </div>
  <div class="col-xs-2 buttons" id="BAR" style="border-left: 0px">
    <span>BAR</span>
  </div>
</div>

Answer №2

To enhance the styling of both divs, it is recommended to assign them a common CSS class as shown below:

<div class="col-xs-10 buttons buttonset1" id="FOO" style="border-right: 0px">
  <span style="padding-left:100px">FOO</span>
</div>
<div class="col-xs-2 buttons buttonset1" id="BAR" style="border-left: 0px">
  <span>BAR</span>
</div>

Subsequently, utilize jQuery to define the following function:

$(".buttonset1").hover
(
  function()
  {
      $(".buttonset1").css("background-color","#CACACA");
  },
  function()
  {
      $(".buttonset1").css("background-color","#D8D8D8");
  }
)

This approach offers greater flexibility in managing the styles effectively.

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

Problem encountered when using the Mongoose find() method with the exec() function

Could you please clarify why the code snippet below is returning audiences instead of an empty array? return Audience.find() .exec((err, audiences) => { if (err) return errorHandler.handle('audienceService', err); return Promise.re ...

Enhancing the performance of jquery selection speed

I am currently working on a code that manipulates the HTML code of an asp.net treeview. Since this code runs frequently, I need to ensure that it executes with maximum speed. I am interested in expanding my knowledge on jQuery selectors and optimizing th ...

Manipulating active classes on different divisions with JQuery

I've completed this code so far: <div class="yearly-archive"> <p> YEAR - <span class="archive-year year-active"> 2014 </span> / <span class="archive-year"> 2013 </span> / <span class="archi ...

Update the central information while keeping the entire webpage intact

Hey there, I could really use some assistance! I'm working on a software documentation website that is similar to the mongodb documentation page. It has a header, sidebar (navbar menu), and main content area. Within the sidebar navbar menu, I have dr ...

increase the count by one every second using setInterval

I need my sHand to increase by 6 every second, but currently it only increases once. When I try something like this.sHand++, it works fine and increases by 1 degree per second. However, I want it to increase by 6 instead of 1. Any solutions? data:{ ...

Challenges with personalized music player

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/CSS"> #custom{ font-family: monospace; font-size: 16px; max-width: 650px; ...

Display a list of records retrieved from a Firebase query using ngFor to iterate through each instance

I'm currently using firebase and angular to work on a billing list project. The list contains data showing information for a specific month, you can refer to the imagehttps://i.stack.imgur.com/ZR4BE.png While querying the list was smooth, I encounte ...

The http url on an iPad in the src attribute of an HTML 5 video tag is malfunctioning

Having trouble loading a video on iPad using an http URL in the src attribute of an HTML5 video tag. Both static and dynamic approaches have been attempted, but it works fine on web browsers. Here is an example of the code: Static: <!DOCTYPE html ...

Setting up types for variables in Angular 2 componentsHere is an

I have a model that consists of multiple properties, and I aim to set all these properties with a default value of either empty or null. Here is an example of my Model: export class MyModel { name: string; jerseyNumber: number; etc... } In m ...

Troubleshooting Cross-Origin Resource Sharing Problem in AngularJS

I've come across several discussions regarding this issue, but none have been able to resolve my problem so far. In my small web app project, I am attempting to access the freckle API from letsfreckle.com. However, I am encountering difficulties. It ...

Extend JavaScript capabilities for window.print() function to automatically include backgrounds

I am looking to add a special magical property to my Print this Page button. This property will automatically enable the default unset option (shown in the picture) which is to print the backgrounds of div colors and background images. <a href="#" oncl ...

Leveraging JQuery for form submission

My webpage contains a form with the following structure: <form action="/" method="post"> <select id="SelectedMonth" name="SelectedMonth"> <option>7/1/2017</option> <option>6/1/2017</option> </select> </form> ...

Activate the text area message by clicking on the href link

I am currently working on customizing the intercom plugin for a WordPress site. My goal is to have a message triggered when a user clicks on a hyperlink, essentially simulating the enter key press in a textarea. Here is the code snippet that I am using: ...

"Divs of the same type automatically adjust size to fit within the

I am currently exploring the possibility of creating a versatile component that can be customized based on the needs of its parent element, whether through bootstrap or traditional flexbox / CSS grid. I want to determine the level of reusability this compo ...

Are Django form widgets like datepicker and tabindex being snubbed?

I have been working on creating a stylish form and managed to find some interesting CSS to enhance it. The form is looking good, however, a couple of fields are not displaying correctly; particularly one that is associated with a choice field as the model ...

Stumped on the reason PHP isn't receiving POST data from $.ajax request

Although I've been through this process before, I'm baffled as to why the POST data in my PHP script is empty. Here's what I've discovered: I double-checked that the $.ajax call's "data" parameter has a value (both alerts in th ...

Having trouble with the error "Cannot GET /" in your Angular2 and Express

I've been working on customizing this GitHub example application to utilize Express instead of KOA. When I enter gulp serve in the CentOS 7 terminal, the app launches successfully. However, upon typing http : // localhost : 8080 in the browser, a 404 ...

Using an exported function with parameters as a filtering mechanism for the material datepicker

I am currently facing an issue while trying to set a const exported function in a material datepicker filter with parameters. When I try to set the parameters in my component, the function gets executed and returns the result (a boolean) instead of simply ...

Having trouble getting the header div to span the entire screen

I'm currently working on a project where I have set the header div to 100% width and the main container to also be 100% width. However, I am facing an issue where the right and left sides of the header are not filling the entire space. I managed to fi ...

The npm script isn't executing

In my package.json file, I have the following script: "scripts": { "build": "browserify app/components/main.js -o build/js/app.js -d" } Executing this command from the shell works fine and the file is created successfully. But when I try to run npm bui ...