如何通过一次点击来定位两个元素?

时间:2015-11-24 17:05:52

标签: html css css-selectors target

我希望在点击它后更改框的背景颜色,同时创建另一个带有纯CSS的框。我用目标选择器尝试了它。但我只能设法做其中一个而不是两个同时。

这是我尝试的DEMO



/* 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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:4)

您可以使用:target伪类修改任意数量的元素的表示形式,只要每个元素都嵌套在id的元素中即可。 :target ED:

/* 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>
 

答案 1 :(得分:1)

:target仅适用于同一个元素,因为您无法同时定位两个锚点。你需要:checked

的javascript或css技巧

纯css :checked

的解决方案

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

我做了什么:

<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;
}

答案 2 :(得分:1)

我看到答案已被接受,但如果其他人有兴趣,我会发布我的解决方案。

我将div的位置更改为链接之前并添加了css规则。

新代码:

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

CSS补充道:

backend/portfolio/{$id} 

演示: http://jsfiddle.net/KNG6n/81/