html - 纯 CSS 和 HTML 菜单(带有子菜单项和文本)

标签 html css menu submenu

我正在尝试创建纯 CSS/HTML 菜单(无 JAVASCRIPT),这是代码示例:https://jsfiddle.net/1Lrr4fme/1/ .

目的是让帮助菜单在单击时显示文本。但是,我在导航如何调整 CSS 以允许每个跨度独立于主要跨度打开/关闭时遇到了极大的困难。

我尝试了一些不同的东西,但我想我遗漏了一些东西。

<style id="tutorial" type="text/css">

html { background: white }

body {
    font-family: sans-serif;
    position: relative;
}

body:before, body:after {
    content: "";
    display: table;
}

body:after { clear: both }

p { margin-bottom: 0rem }


article {
    margin-bottom: 3rem;
    position: relative;
}

article:before, article:after {
    content: "";
    display: table;
}

article:after { clear: both }

article section:first-of-type {

}

article section:last-of-type {
    display: none;
    visibility: hidden;
}

section {
    -webkit-transition: .125s linear;
    -moz-transition: .125s linear;
    -ms-transition: .125s linear;
    -o-transition: .125s linear;
    transition: .125s linear;
}

input[type=checkbox] {
    border: 0;
    clip: rect(0 0 0 0);
    height: 1px;
    width: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
}

[for="read_more"] {
    position: absolute;
    bottom: -3rem;
    left: 0;
    width: 100%;
    text-align: center;
    padding: .65rem;
    box-shadow: inset 1px 1px rgba(0, 0, 0, 0.1), inset -1px -1px rgba(0, 0, 0, 0.1);
}

[for="read_more"]:hover {
    background: rgba(0,0,0,.5);
    color: rgb(255,255,255);
}

[for="read_more"] span:last-of-type {
    display: none;
    visibility: hidden;
}

input[type=checkbox]:checked ~ section {
    display: block;
    visibility: visible;
    width: 100%;
}

input[type=checkbox]:checked ~ figure { width: 100% }

input[type=checkbox]:checked ~ [for="read_more"] span:first-of-type {
    display: none;
    visibility: hidden;
}

input[type=checkbox]:checked ~ [for="read_more"] span:last-of-type {
    display: block;
    visibility: visible;
}
</style>

    <article><input id="read_more" role="button" type="checkbox" /> <label for="read_more" onclick=""><span>Show Help</span><span>Hide Help</span></label>
<section>


<article><input id="read_more" role="button" type="checkbox" /> <label for="read_more" onclick=""><span>1. Entering New Contracts</span><span>Hide Help</span></label>
<section>
<p>hi</p>
</section>
</article>

<article><input id="read_more" role="button" type="checkbox" /> <label for="read_more" onclick=""><span>2. Completing a Contract Review</span><span>Hide Help</span></label>
<section>
<p>hi</p>
</section>
</article>

<article><input id="read_more" role="button" type="checkbox" /> <label for="read_more" onclick=""><span>3. Terminating Contracts</span><span>Hide Help</span></label>
<section>
<p>hi</p>
</section>
</article>

<article><input id="read_more" role="button" type="checkbox" /> <label for="read_more" onclick=""><span>4. Calculated Contract Dates</span><span>Hide Help</span></label>
<section>
<p>hi</p>
</section>
</article>
<p>TESTING ONE TWO THREE</p>
</section>
</article>

最佳答案

您的问题是您对每个输入字段使用相同的 ID。每个字段的 ID 应该是唯一的,以便您可以打开和关闭与该字段相关的文本。

您应该使用类来实现所有输入字段(例如突出显示和边框)所需的样式和行为,然后为每个字段设置不同的 ID。

示例 html:

<article><input id="help_menu" role="button" type="checkbox" /> <label for="help_menu" class="checkbox_label" onclick=""><span>Show Help</span><span>Hide Help</span></label>
<section>


<article><input id="new_contract" role="button" type="checkbox" /> <label for="new_contract" class="checkbox_label" onclick=""><span>1. Entering New Contracts</span><span>Hide Help</span></label>
<section>
<p>hi</p>
</section>
</article>

<article><input id="contract_review" role="button" type="checkbox" /> <label for="contract_review" class="checkbox_label" onclick=""><span>2. Completing a Contract Review</span><span>Hide Help</span></label>
<section>
<p>hi</p>
</section>
</article>

<article><input id="terminate_contract" role="button" type="checkbox" /> <label for="terminate_contract" class="checkbox_label" onclick=""><span>3. Terminating Contracts</span><span>Hide Help</span></label>
<section>
<p>hi</p>
</section>
</article>

<article><input id="contract_dates" role="button" type="checkbox" /> <label for="contract_dates" class="checkbox_label" onclick=""><span>4. Calculated Contract Dates</span><span>Hide Help</span></label>
<section>
<p>hi</p>
</section>
</article>
<p>TESTING ONE TWO THREE</p>
</section>
</article>

示例 CSS:

<style id="tutorial" type="text/css">

html { background: white }

body {
    font-family: sans-serif;
    position: relative;
}

body:before, body:after {
    content: "";
    display: table;
}

body:after { clear: both }

p { margin-bottom: 0rem }


article {
    margin-bottom: 3rem;
    position: relative;
}

article:before, article:after {
    content: "";
    display: table;
}

article:after { clear: both }

article section:first-of-type {

}

article section:last-of-type {
    display: none;
    visibility: hidden;
}

section {
    -webkit-transition: .125s linear;
    -moz-transition: .125s linear;
    -ms-transition: .125s linear;
    -o-transition: .125s linear;
    transition: .125s linear;
}

input[type=checkbox] {
    border: 0;
    clip: rect(0 0 0 0);
    height: 1px;
    width: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
}

label.checkbox_label {
    position: absolute;
    bottom: -3rem;
    left: 0;
    width: 100%;
    text-align: center;
    padding: .65rem;
    box-shadow: inset 1px 1px rgba(0, 0, 0, 0.1), inset -1px -1px rgba(0, 0, 0, 0.1);
}

label.checkbox_label:hover {
    background: rgba(0,0,0,.5);
    color: rgb(255,255,255);
}

label.checkbox_label span:last-of-type {
    display: none;
    visibility: hidden;
}

input[type=checkbox]:checked ~ section {
    display: block;
    visibility: visible;
    width: 100%;
}

input[type=checkbox]:checked ~ figure { width: 100% }

input[type=checkbox]:checked ~ label.checkbox_label span:first-of-type {
    display: none;
    visibility: hidden;
}

input[type=checkbox]:checked ~ label.checkbox_label span:last-of-type {
    display: block;
    visibility: visible;
}
</style>

fiddle :https://jsfiddle.net/8m2pr0ky/

关于html - 纯 CSS 和 HTML 菜单(带有子菜单项和文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366601/

相关文章:

javascript - 如何定义 bootstrap tokenfield 以与 textarea 一起使用

javascript - 如何使用特征检测来了解浏览器是否支持 border-radius? (包括IE9)

ASP.NET 表单验证第一次不起作用

xml - 使用未知大小的数组中的项目作为选项在 powershell 中创建菜单

javascript - 在 AngularJS 中更新 iframe

javascript - 如何批量删除HTML5本地存储?

Javascript:语法错误,无法识别的表达式:<h1>

php - 如何将不同模型的两个不同 id 从 Controller 传递到 View

javascript - GE 是如何获得这个背景来修复和扩展的?

android - 制作安卓游戏