一键定位两个元素? [英] How to target two elements with one click?

查看:73
本文介绍了一键定位两个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击某个框后更改其背景颜色,同时用纯CSS创建另一个框.我尝试使用目标选择器.但是我只能设法做到其中之一,而不能同时做到.

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.

这是我尝试的 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>

推荐答案

您可以使用:target伪类修改任意数量的元素的表示形式,只要每个元素都嵌套在带有:targetid元素内:

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>

这篇关于一键定位两个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆