What is a creative way to make a background color change happen when hovering (with a unique twist)?

Let's cut to the chase.

I have 4 elements named background-1, background-2, background-3, and background-4.

Upon landing on the page, I want Background-1 to have a "background-color: #D8D7D8;" while the other three should be "background-color: #FBFBFB;".

Then, whenever I hover over any background-x element, I want all of them to change to #FBFBFB except the one being hovered over, which should turn to #D8D7D8.

I'm sure this is easy for you, but it's a bit tricky for me haha.

This is how my CSS code looks like:

.background-1 :hover{
    background: #D8D7D8;
    transition: .3s ease-in-out;
}
.background-2 :hover{
    background: #D8D7D8;
    transition: .3s ease-in-out;
}
.background-3 :hover{
    background: #D8D7D8;
    transition: .3s ease-in-out;
}
.background-4 :hover{
    background: #D8D7D8;
    transition: .3s ease-in-out;
}

Check out how it looks like here: Imgur

Thank you in advance!

Milack

Answer №1

Assuming all four elements are siblings, one potential approach is outlined below.

For the default value of bg1, a JavaScript event handler has been proposed.

const divs = document.querySelectorAll('.bg');

function clearBgExcept(target) {
  divs.forEach(div => div.style.backgroundColor = div !== target ? "#FBFBFB" : "#D8D7D8");
}

function clearAllBg() {
  divs.forEach(div => div.style.backgroundColor = div.dataset.default ? "#D8D7D8" : "#F8F8F8" );
}
divs.forEach(el => el.addEventListener('mouseover', e => {
    clearBgExcept(e.target);
  }));
  
divs.forEach(el => el.addEventListener('mouseout', () => {
    clearAllBg();
  }));
  
.wrapper {
  min-width: 300px;
  min-height: 300px;
  margin: 0 auto;
  display: grid;
  grid-gap: 10px;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

.bg {
  background-color: #FBFBFB;
  border: 1px solid #CCC;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: .3s ease-in-out;
}

.bg[data-default="true"], .bg:hover {
  background-color: #D8D7D8;
}
<div class="wrapper">
  <div class="bg" data-default="true">BG1</div>
  <div class="bg">BG2</div>
  <div class="bg">BG3</div>
  <div class="bg">BG4</div>
</div>

Answer №2

If your elements are siblings, you can achieve the desired effect without the need for Javascript:

.background-1,
.background-2,
.background-3,
.background-4 {
  background-color: #FBFBFB;
  padding: 2em;
}

.background-1 {
  background-color: #D8D7D8;
}

.background-1:hover {
  background: #D8D7D8;
  transition: .3s ease-in-out;
}

.background-2:hover {
  background: #D8D7D8;
  transition: .3s ease-in-out;
}

.background-3:hover {
  background: #D8D7D8;
  transition: .3s ease-in-out;
}

.background-4:hover {
  background: #D8D7D8;
  transition: .3s ease-in-out;
}

.column:hover .background-1:not(:hover) {
  background-color: #FBFBFB;
}
<div class="column">
  <div class="background-1">
    B1
  </div>
  <div class="background-2">
    B2
  </div>
  <div class="background-3">
    B3
  </div>
  <div class="background-4">
    B4
  </div>
</div>

The unique aspect of this approach is in resetting the color of background-1 when the parent element is hovered but background-1 itself is not being hovered.

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

"Flooding the page with flex wrap is causing everything to

Having trouble with the scraping of some divs as they are not wrapping properly within my container Your HTML: <div class="anime-list"> {% for anime in response_data %} <div class="anime-item"> ...

Using typefaces in mobile native apps

@font-face { font-family: "Frodo Simplified Italic"; src: url("../resources/FrodoSimplified_It.ttf"); } Utilizing a HTML5 application that is embedded within an iframe of a webview includes the CSS statement above. The fonts specified within the state ...

Adjustable columns that fill the rest of the screen's height below a navigation bar

I am facing an issue with my non-fixed navbar and two-column layout when testing the responsiveness on a mobile screen size. I need Column A to fill the remaining viewport's height, while Column B should move under Column A without hardcoding any valu ...

Moving up stacked columns with bootstrap or any other CSS technique

Is there a way to make column [3] move up if column [2] is taller than column [1]? [1][2] [3][4] Check out this bootply example of the layout : http://www.bootply.com/KbAf8UOk9c Currently, there is empty space between column [1] and [3] that I would li ...

How can I ensure that the NextJS Image component fills its parent div completely in terms of both height and width?

Having trouble getting the Image component to behave like the img element? Can anyone provide a solution for making Image achieve the same result as shown in the example below? <div style={{ width: "100vw", height: "100vh", display ...

Prevented a frame from "https://googleads.g.doubleclick.net" from accessing another frame

After placing ads on my website, they are displaying properly. However, I am receiving an error repeatedly in the console when the page loads: A frame from origin "" is being blocked from accessing a frame with origin "". The requesting frame has an "ht ...

Customize the overlay color using the theme's background color

I want to create an overlay on my webpage that covers all the content. However, I'm facing a challenge because my website allows users to customize the theme colors, and the overlay div does not adopt these color changes automatically. To rectify this ...

Capture selected items from checkboxes and incorporate them into the CSS styling

I would like to give users the ability to choose from a series of checkboxes with additional options to add to their CSS. Using JavaScript, I am searching for checked boxes, extracting their names, adding them to a list, and then inserting that list into ...

Bootstrap Collapse feature fails to collapse on Twitter

I'm having trouble with the bootstrap collapse navbar. It expands fine, but I can't seem to collapse the navigation. Everything seems correct in my code: <nav class="navbar navbar-inverse nav-justified"> <div class="navbar- ...

Automatically resizing button on media query adjust

I'm experiencing an issue with these 5 buttons in mobile view. On desktop, all 5 buttons are displayed inline. However, on mobile, the last 2 buttons should appear below the first 3. I need them to remain fixed and occupy all available space even when ...

Learn how to display two rows of div elements within a single div container

In my program, I am using a for loop to display multiple div elements in one row. The first div is showing up correctly, but I am having trouble inserting a new div that looks the same as the first one. How can I achieve this? Thank you in advance. "first ...

Webpage created from scratch encounters technical difficulties on mobile device browser

My website seems to be getting stuck at the beginning of a section, particularly on mobile browsers. You can check out the website here <section class="home-slider owl-carousel"> <div class="slider-item" style="background-image: url(images/bg ...

How to Fix the Issue of CSS Font Face Path Not Working in Your Express Application

My current font files are stored in the public/fonts/ directory. The CSS files can be found in public/css/. Despite trying various path and css adjustments, I am unable to successfully load the fonts... @font-face {font-family: trend_sans_oneregular; ...

Images on the login page are not being displayed due to CSS urls not functioning properly

Struggling with an issue and in need of some quick assistance. I have an ASP.NET application that utilizes Forms authentication. On the Login.aspx page, I have implemented multiple background images for visual appeal. These image URLs are set using CSS st ...

The behavior of -webkit-border-radius differs from that of -moz-border-radius

My website is displaying differently on Safari compared to Firefox. I want the CSS to make it look consistent across both browsers. I understand that I could use two div boxes - one for the outline and one for the image. However, I prefer how Firefox only ...

How can jQuery be utilized to dynamically update the text in a navbar?

<div id="page1" data-role="page"> <div data-role="header" data-position="fixed" align="center"><img src="img/VAWE-long-300x30-transparent.png" width="300" height="30" alt="" /></div> <div data-role="content" style="margin ...

When the height is set to 100vh, the Div and Image appear misaligned vertically

Could someone explain why there is a vertical separation between the div and the image? <div style="height: 100vh; display: inline-block;width: 50%; background-color: blue">TEST </div><!-- --><img src="photos/openening.jpg" alt="SICK ...

Encountering issues with implementing a sticky header feature for Primeng tables

I'm having trouble implementing a sticky header with p-table in my project. I've followed the CSS provided in the documentation for primeng 7 but it's not working as expected. Can someone please assist me with this issue? Thank you! Below i ...

I am encountering an issue where I am unable to access properties of null while trying to read the 'pulsate' function within the

The current version of my material-ui library is as follows: "@mui/icons-material": "^5.5.1", "@mui/material": "^5.5.1", This is how I included the Button component : import Button from "@mui/material/Button&qu ...

Design a customized menu using a JavaScript JSON array

Looking to generate a menu using JSON Array data Here is the JSON Array: [{ "page_id":"102608802958096849","title":"Submenu 1 1","page_order":1,"parent_id":"305280635460611248","layout":"header","page_url":"submenu-1-1.html" },{ "page_id":"207782 ...