Change background color of div on click (CSS only)

AlphaX

I have three divs, of which the first needs to have a set background color on page load (this is to show that this is the default choice and preselected)

I want that when the user clicks the 2nd or 3rd div that

  1. the 1st div's background color is removed / white
  2. the clicked div has the background color (grey)

Should the user click on the 1st div again, it should behave like the 2nd or 3rd div and take the background color.

I have the following code per below, but I can't remove the background color of the 1st div when any other div is clicked.

Could anyone help me please?

JSFiddle: https://jsfiddle.net/AlphaX/1zp8yehu/1/

.one {background-color: grey;}

[tabindex]:focus { background-color: grey;}
<div class="parent" tabindex="0">


<div class="one" tabindex="1">
one
</div>

<div class="two" tabindex="2">
two
</div>

<div class="three" tabindex="3">
three
</div>

</div>

Temani Afif

:focus-within can help you here:

.active {
  background-color: grey;
}

.parent:focus-within * { /* rest all background when one element is focused */
  background: none;
}

.parent [tabindex]:focus { /* set the background of only the focused one */
  background-color: grey;
}
<div class="parent" tabindex="0">


  <div class="one active" tabindex="1">
    one
  </div>

  <div class="two" tabindex="2">
    two
  </div>

  <div class="three" tabindex="3">
    three
  </div>

</div>

A more tricky code if you want to have all of them transparent when clicking outside:

.active {
  position:relative;
  z-index:0;
}
.active::before {
  content:"";
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background: grey;
  animation:h 0.1s forwards paused;
}

.parent:focus-within *::before {
  animation-play-state:running;
}

.parent [tabindex]:focus { 
  background-color: grey;
}

@keyframes h {
  1%,100% {
    background:transparent;
  }
}
<div class="parent" tabindex="0">


  <div class="one active" tabindex="1">
    one
  </div>

  <div class="two" tabindex="2">
    two
  </div>

  <div class="three" tabindex="3">
    three
  </div>

</div>

And to keep the last clicked element colored

.parent [tabindex] {
  transition:999s;
  background: rgba(255,255,255,0);
}
.parent:focus-within [tabindex] {
  transition:0s;
  background: rgba(255,255,254,0); /* a little different coloration to make sure we trigger the transition */
}
.parent [tabindex]:focus,
.parent [tabindex].active:focus{ 
  background-color: rgb(128,128,128);
  transition:0s;
}

.parent .active{ 
  background-color: rgb(128,128,127); /* a little different coloration to make sure we trigger the transition */
}
<div class="parent" tabindex="0">


  <div class="one active" tabindex="1">
    one
  </div>

  <div class="two" tabindex="2">
    two
  </div>

  <div class="three" tabindex="3">
    three
  </div>

</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Change div background color on click using only css

From Dev

Change background color of divs in next div click

From Dev

Change div background color on click event

From Dev

Change div background color after click

From Dev

Change background on button click, using CSS only?

From Dev

How to change the background color of a div in HTML/CSS

From Dev

How to make a div change color when you click another div using only css?

From Dev

Change the color of a div element using only css

From Dev

Change background color for div

From Dev

Loading JSON into a div and change background color on button click at the same time

From Dev

How to change bootstrap label background color after click using CSS?

From Dev

Change children color div when click parent div | CSS Pseudo

From Dev

Button - Change background color on click

From Dev

Change background button color on click

From Dev

Setting background color of div on click

From Dev

How to change the background color of a span/div WITHOUT CSS

From Dev

Change only background and color of titlebar

From Dev

Only change background color of one <a>

From Dev

Javascript - Change DIV background on click

From Dev

CSS only change a separate div color on hover even if not nested or sibling?

From Dev

change color and shape of div on click

From Dev

CSS: Change Background Color on Hover

From Dev

Change background color of "selected" with CSS

From Dev

CSS: Change Background Color on Hover

From Dev

CSS: How to change background color

From Dev

CSS Spinning Div Background Color

From Dev

css - div background color not showing

From Dev

Css : Scale background color of a div

From Dev

Change background color and active div color

Related Related

HotTag

Archive