css - 纯 CSS 折叠/展开 div

标签 css collapsable

我有一个纯 CSS 可折叠 div,它基于使用 :target 伪类的其他人的代码。我要设置的是一个包含 12 个以上问题的页面,当您单击 + 按钮时,答案 div 会在下方展开。我不知道如何在不编写大量额外 CSS 的情况下在此页面上制作多个折叠的 div 元素。有人对如何编写此代码有任何建议,以便将我的 CSS 代码最小化吗? (也就是说,我不必为 12 个以上的问题中的每一个输入一堆唯一的选择器)。

我不能使用 Javascript,因为这是在不允许 JS 的 wordpress.com 网站上进行的。

这是我的 jfiddle: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;
}

最佳答案

根据您希望支持的浏览器/设备,或者您准备忍受不兼容的浏览器,您可能需要查看 <summary><detail>标签。他们正是为了这个目的。完全不需要 css,因为折叠和显示是标签定义/格式的一部分。

我做了一个例子here :

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

Browser support varies.在 webkit 中尝试以获得最佳效果。其他浏览器可能默认显示所有解决方案。您或许可以回退到上述隐藏/显示方法。

关于css - 纯 CSS 折叠/展开 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15095933/

相关文章:

javascript - 为什么条形图的颜色没有填充并保持白色? d3js

html - 对齐 CSS 网格元素

javascript - "Read More"按钮点击时位置出错。?

javascript - 如何使用纯 CSS/JavaScript(即没有 jQuery、没有 Boostrap 等)创建可水平和垂直折叠的 HTML div?

html - 突出显示最后一个元素的类名的下一个元素 - CSS3

jquery 更改具有相同类的不同 id 的 css

javascript - 如何在选择列表中显示扩展选项?

jquery - 想要显示滑动和可折叠的菜单

javascript - Bootstrap : collapse-group within collapse-group