纯 CSS 折叠/扩展 div

我有一个纯 CSS 可折叠 div,它基于使用 :target假类的其他人的代码。我正在尝试建立一个页面,上面有12个以上的问题,当你点击 + 按钮时,答案 div 会在下面展开。我不知道如何在不编写大量额外 CSS 的情况下在这个页面上创建多个折叠的 div 元素。有没有人对如何编写这样的 CSS 代码最小化有什么建议?(也就是说,我不需要为每个12 + 的问题输入一组唯一的选择符)。

我不能使用 Javascript,因为这是一个 wordpress.com 网站,不允许 JS。

这是我的小提琴: http://jsfiddle.net/dmarvs/94ukA/4/

<div class="FAQ">
<a href="#hide1" class="hide" id="hide1">+</a>
<a href="#show1" class="show" id="show1">-</a>
<div class="question"> Question Question Question Question Question Question Question Question Question Question Question? </div>
<div class="list">
<p>Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer Answer </p>
</div>
</div>
/* source: http://www.ehow.com/how_12214447_make-collapsing-lists-java.html */


.FAQ {
vertical-align: top;
height:auto !important;
}
.list {
display:none;
height:auto;
margin:0;
float: left;
}
.show {
display: none;
}
.hide:target + .show {
display: inline;
}
.hide:target {
display: none;
}
.hide:target ~ .list {
display:inline;
}


/*style the (+) and (-) */
.hide, .show {
width: 30px;
height: 30px;
border-radius: 30px;
font-size: 20px;
color: #fff;
text-shadow: 0 1px 0 #666;
text-align: center;
text-decoration: none;
box-shadow: 1px 1px 2px #000;
background: #cccbbb;
opacity: .95;
margin-right: 0;
float: left;
margin-bottom: 25px;
}


.hide:hover, .show:hover {
color: #eee;
text-shadow: 0 0 1px #666;
text-decoration: none;
box-shadow: 0 0 4px #222 inset;
opacity: 1;
margin-bottom: 25px;
}


.list p{
height:auto;
margin:0;
}
.question {
float: left;
height: auto;
width: 90%;
line-height: 20px;
padding-left: 20px;
margin-bottom: 25px;
font-style: italic;
}
341412 次浏览

您只需迭代两个链接中的锚。

<a href="#hide2" class="hide" id="hide2">+</a>
<a href="#show2" class="show" id="show2">-</a>

看这个 jsfiddle http://jsfiddle.net/eJX8z/

我还在 FAQ 调用中添加了一些空白,以改进格式。

或者一个超级简单的版本,几乎没有任何 css:)

<style>
.faq ul li {
display:block;
float:left;
padding:5px;
}


.faq ul li div {
display:none;
}


.faq ul li div:target {
display:block;
}




</style>




<div class="faq">
<ul>
<li><a href="#question1">Question 1</a>
<div id="question1">Answer 1 </div>
</li>




<li><a href="#question2">Question 2</a>
<div id="question2">Answer 2 </div>
</li>
<li><a href="#question3">Question 3</a>
<div id="question3">Answer 3 </div>
</li>
<li><a href="#question4">Question 4</a>
<div id="question4">Answer 4 </div>
</li>
<li><a href="#question5">Question 5</a>
<div id="question5">Answer 5 </div>
</li>
<li><a href="#question6">Question 6</a>
<div id="question6">Answer 6 </div>
</li>
</ul>
</div>

Http://jsfiddle.net/ionko22/4skd3/

Depending on what browsers/devices you are looking to support, or what you are prepared to put up with for non-compliant browsers you may want to check out the <summary> and <detail> tags. They are for exactly this purpose. No css is required at all as the collapsing and showing are part of the tags definition/formatting.

我举了一个例子 给你:

<details>
<summary>This is what you want to show before expanding</summary>
<p>This is where you put the details that are shown once expanded</p>
</details>

浏览器支持多种多样。 尝试使用 webkit 获得最佳效果。其他浏览器可能默认显示所有解决方案。您也许可以回退到上面描述的 hide/show 方法。

@ gbtimmon 的回答 很棒,但是太复杂了,我已经尽可能简化了他的代码。

#answer,
#show,
#hide:target {
display: none;
}


#hide:target + #show,
#hide:target ~ #answer {
display: inherit;
}
<a href="#hide" id="hide">Show</a>
<a href="#/" id="show">Hide</a>
<div id="answer"><p>Answer</p></div>

使用 <summary><details>

使用 <summary><details>元素是最简单的,但是请看 浏览器支持,因为当前 IE 不支持它。不过,您可以使用 填料(大多数是基于 jQuery 的)。请注意,不支持的浏览器当然只会显示扩展版本,所以在某些情况下可能是可以接受的。

/* Optional styling */
summary::-webkit-details-marker {
color: blue;
}
summary:focus {
outline-style: none;
}
<details>
<summary>Summary, caption, or legend for the content</summary>
Content goes here.
</details>

另见 如何样式化 <details>元素(HTML5博士) (有点棘手)。

纯 CSS3

:target选择器有一个非常好的 浏览器支持,它可以用来在框架内创建一个 单身可折叠元素。

.details,
.show,
.hide:target {
display: none;
}
.hide:target + .show,
.hide:target ~ .details {
display: block;
}
<div>
<a id="hide1" href="#hide1" class="hide">+ Summary goes here</a>
<a id="show1" href="#show1" class="show">- Summary goes here</a>
<div class="details">
Content goes here.
</div>
</div>
<div>
<a id="hide2" href="#hide2" class="hide">+ Summary goes here</a>
<a id="show2" href="#show2" class="show">- Summary goes here</a>
<div class="details">
Content goes here.
</div>
</div>