Is there a way to modify the CSS of another element when hovering over it?

I am working on a thermometer design that features multiple segments and a circle at the start. When hovering over each segment, I want it to highlight along with the circle. Currently, the issue is that the first segment is created using two overlapping divs, resulting in an unintended effect when hovered over.

Is there a way to modify the CSS code to ensure that both the first segment and the circle light up simultaneously when either one is hovered over? Ideally, I would like a solution within CSS, but I am open to using JavaScript or jQuery if necessary. Below is the current HTML and CSS:

<div class="thermo">
    <div class="startthermo">&nbsp;</div>
    <div class="thermopadding">&nbsp;</div>
    <div class="thermalcapsule" style="width: 10%">
        <div class="thermal thermal1">1</div>
        100
    </div>
    <div class="thermalcapsule" style="width: 15%">
        <div class="thermal thermal2">2</div>
        200
    </div>
    <!-- More HTML here -->
</div>

.thermo {
    width: 95%;
    margin-left: auto;
    margin-right: auto;
}

.thermalcapsule {
    margin-right: 1px;
    margin-top: 20px;
    float:left;
    text-align: center;
    cursor: pointer;
    position: relative;
    z-index: 1;
}

.thermalcapsule:hover .thermal1{
    background-color: #000000;
}

.thermoimage {
    margin-left: -15px;
    margin-top: 8px;
    float: left;
}

.thermopadding {
    width: 2%;
    float:left;
}

.startthermo {
    width: 50px;
    height: 50px;
    border-radius: 50px;
    background-color: #60A4CE;
    border: 9px solid #f4f4f4;
    display: block;
    float:left;
    position: absolute;
}

.thermal {
    width: 100%;
    padding: 5px;
    text-align: center;
    color: #fff;
    font-weight: bold;
    position: relative;
    z-index : 5;
}

.thermal1 {
    background-color: #60A4CE;
}

.thermal2 {
    background-color: #437C87;
}

Answer №1

Here is my solution to the problem, I hope it can be helpful:

Link to the code

<div class="thermo">
    <div class="thermopadding">&nbsp;</div>
    <div class="thermalcapsule" style="width: 15%">
        <div class="startthermo">&nbsp;</div>
        <div class="thermal thermal1">1</div>
        100
    </div>
    <div class="thermalcapsule" style="width: 15%">
        <div class="thermal thermal2">2</div>
        200
    </div>
    <!-- Additional HTML content -->
</div>

.thermalcapsule:hover .startthermo,
.thermalcapsule:hover .thermal1{
    background-color: #000000;
}

.startthermo {
    width: 50px;
    height: 50px;
    border-radius: 50px;
    background-color: #60A4CE;
    border: 9px solid #f4f4f4;
    display: block;
    float:left;
    position: absolute;
    margin-left: -35px;
    margin-top: -20px;
}

Answer №2

To begin, modify the HTML segment as follows:

<div class="temp">
  <div class="temperaturecapsule" style="width: 20%">
    <div class="temperature temp1">1</div>
    100
  </div>
  <div class="temperaturecapsule" style="width: 15%">
    <div class="temperature temp2">2</div>
    200
  </div>
  <!-- Additional HTML code here -->

  <div class="starttemp">&nbsp;</div>
</div>

Then, implement this selector to alter the background-color of the circle:

.temperaturecapsule:first-of-type:hover ~ .starttemp {
  background-color: #000000;
}

What advantage does this provide?

In this scenario, if the .temperaturecapsule elements are dynamically generated, there is no need to validate whether the current capsule is the first one or not.

In this instance, the .temppadding element has been eliminated and the following style is utilized to apply the effect:

.temperaturecapsule:first-of-type {
  /* Instead of using .temppadding */
  margin-left: 2%;
}

JSBin Demo

Answer №3

.thermalcapsule:hover .thermal1 {
  background-color: #FFFFFF; //delete this code
  background-color: #336699; insert the following line
}

Answer №4

If you're looking for a way to achieve this using JavaScript instead of pure CSS, here's a solution:

Check out the JSFiddle example

The approach involves creating a wrapper element around the two elements you want to highlight together and utilizing the mouseenter and mouseleave events to toggle classes on the inner elements.

Here is the JavaScript code snippet:

$(function () {
    $(".wrapper").mouseenter(function () {
        var $this = $(this);
        $("div", $this).addClass("highlighted");
    });

    $(".wrapper").mouseleave(function () {
        var $this = $(this);
        $("div", $this).removeClass("highlighted");
    });
});

And here is the corresponding HTML structure:

<div class="wrapper">
    <div class="div1"></div>
    <div class="div2"></div>
</div>

For the CSS styling, you can use the following rules:

.div1 {
    background-color: red;
    min-height: 50px;
    min-width: 100px;
}

.div2 {
    background-color: blue;
    min-height: 50px;
    min-width: 100px;
}

.highlighted {
    background-color: green;
}

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

The complexity of JQuery's design has left many puzzled

I am new to using Jquery and I have come to the following realizations: Every selector in Jquery always returns a wrapped list of items, which can be: An empty list A list with a single element A list with multiple elements Chained functions operate o ...

Unable to retrieve the width of an `element` using Angular's built-in jQuery method

I'm attempting to retrieve the width of an element using the AngularJS link method, but I'm not seeing the expected result. Here's my code: var myApp = angular.module("myApp", ['ngResource']); myApp.factory("server", function($r ...

Showing ASP code within a DIV element (inline)

Working on setting up a 'shopping cart' feature on our website, but running into some issues with the ASP code. Does anyone have any advice to offer? In the image below, you can see that when items are added to the cart, the 'total' is ...

Searching for a name in JSON or array data using jQuery can be accomplished by utilizing various methods and functions available

Having trouble searching data from an array in jQuery. When I input Wayfarer as the value for the src_keyword variable, it returns relevant data. PROBLEM The issue arises when I input Wayfarer Bag as the value for the src_keyword variable. It returns em ...

Failure to send AJAX POST data

When the login button is clicked, I want to send the contents of the email text box. However, nothing happens when I click the login button. Here is the file directory structure: Model/ <-- configuration.php is located inside the Model ...

Hidden input value appearing blank even though it contains data

What is preventing me from retrieving the value of this input? Refer to the image below: ...

Error message "e.nodename undefined when set to setTimeout" was encountered

I developed a unique piece of code for input boxes located at the top of different tables. By using a class='filt', the table can be filtered based on the inputs provided. However, most of the inputs consist of more than one letter, so I wanted t ...

How can the table header be displayed in a Vue JS application after receiving the server response?

On my Vue.JS web page, I am making an API call using the Http GET method. Currently, the table header is displayed before receiving the API response, which doesn't look good. Can someone help me modify my code so that the table header is only displaye ...

Is it possible to uncheck a checkbox from a list, even if some of them are already unchecked?

I am attempting to implement a feature where checking a checkbox selects all others in the list, and unchecking one deselects all. Currently, only selecting one checkbox allows me to check all the rest, but unchecking one doesn't trigger the reverse a ...

What's the reason for my badge being an oval shape?

I am attempting to design a badge that hovers above my cart icon and shows the total number of items currently in the cart. However, I have encountered an issue where the badge appears as an oval shape rather than a perfect circle. Can anyone assist with ...

Using jQuery to add a class of "trigger" to the loaded URL

Utilizing jQuery for ajax section loading, I aim to create a concise and versatile function that loads the relevant section based on a clicked tab. Here's where it gets tricky... I want to extract the class of the clicked tab and combine it with the ...

The Javascript toggle for hiding and showing did not function as expected

I've been attempting to create a JavaScript toggle so that when I click on any of the buttons below, only that particular div is shown and all others are hidden. Unfortunately, my code is not working as expected: function toggle(show,hide) { do ...

When there is no content in the responseText, AJAX will display an error

I've encountered an issue while trying to fetch data using AJAX. The problem lies in receiving an empty responseText. Here's the code I'm working with: JavaScript: function getFounder(id) { var founder = ""; $.ajax({ ...

The number of columns in the table do not match

Observing the table below, it appears that although I have used colspan="4" for two td elements, they are not equal in width. The first column seems to be wider than the second. What might be causing this discrepancy? <table border="0" cellpadd ...

Clear information upon clicking to switch to a new controller

I have a scenario where one div contains another div. The contained div has its own controller that controls its visibility based on a variable changed by clicking an icon button in the container. The structure is as follows: <div ng-controller="BarCo ...

Unable to submit form using the submit button

Can anyone explain why the form won't submit in this scenario? I prefer not to use input type='Submit', but the function call seems to be working. <button onclick='submitForm()'>Save</button> function submitForm() { ...

Error Loading Message Appears with jQuery Mobile 1.3.1 and Flot Integration

I am trying to implement a flot interactive example with jQuery Mobile 1.3.1 in my project. However, I am encountering an issue with the latest version of jQuery Mobile where there is a strange frame around the chart and a "loading" text at the bottom of t ...

Activating radio button based on the value in a text field

When the value is set to "Runner", the radio button will be enabled. If the value is anything other than "Runner", the radio button will be disabled. However, the code provided below seems to always disable the button, which is not the intended behavior. ...

Looking to position a JSX component to the left on your webpage with React and Bootstrap?

Currently, I am working on a React project where I am utilizing Bootstrap to style my components. The issue I am facing is with aligning a JSX component to the left on my webpage. Here is the code snippet of the component: import React from 'react&apo ...

Inconsistency with jQuery Ajax response in Internet Explorer leading to null results

Currently, I am attempting to retrieve an HTML document by utilizing the jQuery ajax() method. It appears that when I try to analyze the retrieved data using $(data), every browser except Internet Explorer returns a DOM element. However, in Internet& ...