纯 CSS 手风琴的可访问性问题
Accessibility issue with pure CSS accordion
我有一个手风琴,我正在努力让键盘可以访问。我正在使用输入元素来跟踪用户何时单击手风琴并显示隐藏的内容,这是导致问题的原因,因为它设置为显示的输入:none。
有没有办法让它可以访问?我仍在学习网络可访问性。将不胜感激任何提示!
.accordion {
width: 100%;
box-shadow: 0 12px 12px -6px rgba(0, 0, 0, 0.2);
}
.accordion input[name="panel"] {
display: none;
}
.accordion label {
position: relative;
display: block;
padding: 1em;
background: rgba(65, 64, 66, 0.8);
border-radius: 0.2em 0.2em 0 0;
width: auto;
font-size: 1.2em;
color: #fff;
letter-spacing: 0.1em;
text-transform: uppercase;
font-family: FSHumanaRegular;
margin-top: 1em;
cursor: pointer;
transition: all 0.2s cubic-bezier(0.865, 0.14, 0.095, 0.87);
}
.accordion label:after {
content: "+";
position: absolute;
right: 1em;
width: 1em;
height: 1em;
color: #eee;
text-align: center;
border-radius: 50%;
background: #fdb600;
}
.accordion label:hover {
color: #fdb600;
}
.accordion input:checked + label {
color: #fdb600;
}
.accordion input:checked + label:after {
content: "-";
line-height: 0.8em;
}
.accordion .accordion_content {
overflow: hidden;
max-height: 0px;
position: relative;
padding: 0 1.425em;
background: rgba(255, 255, 255, 0.6);
box-sizing: content-box;
transition: all 150ms cubic-bezier(0.865, 0.14, 0.095, 0.87);
}
.accordion .accordion_content .accordion_body {
line-height: 1.4em;
padding: 1.8em 0 1.5em;
}
input[name="panel"]:checked ~ .accordion_content {
max-height: 1000em;
}
<div class="accordion" id="myAccordion">
<div>
<input type="checkbox" name="panel" id="accordionContent">
<label for="accordionContent">Historical Data</label>
<div class="accordion_content">
<div class="accordion_body">
<!-- CONTENT -->
<p>Hello, world!</p>
</div>
</div>
</div>
</div>
首先,您不应该对 collapse/expand 任何元素使用复选框(不良做法)。您可以改用 <button>
来 trigger/control 此功能,并且在此按钮上您应该进一步添加正确的 ARIA 值。
您可以查看以下 ARIA 属性,以帮助您进一步启用可折叠元素的辅助功能。
aria-expanded="false"
aria-controls="collapsedElementId"
例如,Bootstrap 的折叠组件的实现使用这样的标记来增强可访问性:https://getbootstrap.com/docs/4.1/components/collapse/#accessibility
我有一个手风琴,我正在努力让键盘可以访问。我正在使用输入元素来跟踪用户何时单击手风琴并显示隐藏的内容,这是导致问题的原因,因为它设置为显示的输入:none。
有没有办法让它可以访问?我仍在学习网络可访问性。将不胜感激任何提示!
.accordion {
width: 100%;
box-shadow: 0 12px 12px -6px rgba(0, 0, 0, 0.2);
}
.accordion input[name="panel"] {
display: none;
}
.accordion label {
position: relative;
display: block;
padding: 1em;
background: rgba(65, 64, 66, 0.8);
border-radius: 0.2em 0.2em 0 0;
width: auto;
font-size: 1.2em;
color: #fff;
letter-spacing: 0.1em;
text-transform: uppercase;
font-family: FSHumanaRegular;
margin-top: 1em;
cursor: pointer;
transition: all 0.2s cubic-bezier(0.865, 0.14, 0.095, 0.87);
}
.accordion label:after {
content: "+";
position: absolute;
right: 1em;
width: 1em;
height: 1em;
color: #eee;
text-align: center;
border-radius: 50%;
background: #fdb600;
}
.accordion label:hover {
color: #fdb600;
}
.accordion input:checked + label {
color: #fdb600;
}
.accordion input:checked + label:after {
content: "-";
line-height: 0.8em;
}
.accordion .accordion_content {
overflow: hidden;
max-height: 0px;
position: relative;
padding: 0 1.425em;
background: rgba(255, 255, 255, 0.6);
box-sizing: content-box;
transition: all 150ms cubic-bezier(0.865, 0.14, 0.095, 0.87);
}
.accordion .accordion_content .accordion_body {
line-height: 1.4em;
padding: 1.8em 0 1.5em;
}
input[name="panel"]:checked ~ .accordion_content {
max-height: 1000em;
}
<div class="accordion" id="myAccordion">
<div>
<input type="checkbox" name="panel" id="accordionContent">
<label for="accordionContent">Historical Data</label>
<div class="accordion_content">
<div class="accordion_body">
<!-- CONTENT -->
<p>Hello, world!</p>
</div>
</div>
</div>
</div>
首先,您不应该对 collapse/expand 任何元素使用复选框(不良做法)。您可以改用 <button>
来 trigger/control 此功能,并且在此按钮上您应该进一步添加正确的 ARIA 值。
您可以查看以下 ARIA 属性,以帮助您进一步启用可折叠元素的辅助功能。
aria-expanded="false"
aria-controls="collapsedElementId"
例如,Bootstrap 的折叠组件的实现使用这样的标记来增强可访问性:https://getbootstrap.com/docs/4.1/components/collapse/#accessibility