Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to target two elements with one click?

I want to change the background-color of a box after clicking on it and at the same time create another box with pure CSS. I tried it with the target selector. But I only can manage to do one of them asks and not both at the same time.

Here is a DEMO of my try.

/* fonts */

p {
  font-size: 10px;
}
#school::after,
#work::after {
  font-size: 10px;
  content: "Second box";
  color: white
}
/* white boxes */

.panel {
  height: 50px;
  width: 50px;
  background-color: white;
  border: 1px solid #262626;
  position: relative;
}
/* span (100%, 100%) inside the white-boxes */

.panel span {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}
/* second-box */

.panel div {
  display: none;
}
/* if white-box is targeted, this lets the second-box appear */

.panel div:target {
  display: block;
  height: 50px;
  width: 50px;
  background-color: blue;
  border: 1px solid black;
  border-radius: 4px;
  position: absolute;
  left: 70px;
}
/* for testing purposes */

.panel:active span {
  background-color: black;
}
<p>White Boxes</p>
<div class="panel">
  <a href="#school">
    <span></span>
  </a>
  <div id="school"></div>
</div>

<div class="panel">
  <a href="#work">
    <span></span>
  </a>
  <div id="work"></div>
</div>
like image 666
CodeVolunteer Avatar asked Dec 21 '25 13:12

CodeVolunteer


1 Answers

You can modify the presentation of any number of elements using the :target pseudo-class, so long as each one is nested within the element with the id which is :targeted:

/* fonts */
p {
    font-size: 10px;
}

#school div::after, #work div::after {
    font-size: 10px;
    content: "Second box";
    color: white
}


/* white boxes */
.panel {
    height: 50px;
    width: 50px;
    background-color: white;
    border: 1px solid #262626;
    position: relative;
}

/* span (100%, 100%) inside the white-boxes */
.panel span {
    position:absolute; 
    width:100%;
    height:100%;
    top:0;
    left: 0;
}

/* second-box */
.panel a div {
    display: none;
}

/* if white-box is targeted, this lets the second-box appear */
.panel a:target div {
    display: block;
    height: 50px;
    width: 50px;
    background-color: blue;
    border: 1px solid black;
    border-radius: 4px;
    position: absolute;
    left: 70px;
}


/* if white-box is targeted, this gives the box a blue background */
.panel a:target span {
    background-color: blue;
}


/* for testing purposes */
.panel:active span {
    background-color: black;
}
<p>White Boxes</p>
<div class="panel">
    <a href="#school" id="school">
    <span></span>
    <div></div>
    </a>
</div>

<div class="panel">
    <a href="#work" id="work">
    <span></span>
    <div></div>
    </a>
</div>
like image 97
Rounin - Glory to UKRAINE Avatar answered Dec 24 '25 03:12

Rounin - Glory to UKRAINE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!