Чистое меню CSS и HTML (с элементами подменю и текстом)
Я пытаюсь создать чистое меню 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>
1 ответ
Ваша проблема в том, что вы используете один и тот же идентификатор для каждого поля ввода. Идентификатор должен быть уникальным для каждого поля, чтобы вы могли открывать и закрывать текст, связанный с этим полем.
Вы должны использовать классы для стилизации и поведения, которое вы хотите, во всех полях ввода, таких как подсветка и границы, а затем иметь разные идентификаторы для каждого поля.
Пример 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>
Скрипка: https://jsfiddle.net/8m2pr0ky/