How to target two elements with one click?

2019-07-05 12:32发布

问题:

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>

回答1:

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>



回答2:

:target is only for one element at same time, because you can't target two anchors at same time. You need javascript or a css trick with :checked

Solution with pure css :checked

http://jsfiddle.net/KNG6n/78/

What I make:

<div class="panel">
    <label>
        <input type="checkbox">
        <div></div>
    </label>
</div>

CSS

label {
    display:block;
    position: relative;
    width: 100%;
    height: 100%;
}
label input {
    visibility: hidden;
}
label input:checked + div {
    display: block;
}


回答3:

I see an answer was accepted already, but will post my solution if anyone else is interested.

I changed the location of the div to be before the link and added a css rule.

new code:

<p>White Boxes</p>
<div class="panel">
    <div id="school"></div>
    <a href="#school">
        <span></span>
    </a> 

</div>

<div class="panel">
    <div id="work"></div>
    <a href="#work">
        <span></span>
    </a>

</div>

CSS added:

.panel div:target+a span{
    background-color: black;
}

Demo: http://jsfiddle.net/KNG6n/81/