gpt4 book ai didi

html - 可访问的自定义样式复选框

转载 作者:行者123 更新时间:2023-11-28 02:49:43 25 4
gpt4 key购买 nike

我正在使用标签元素来创建自定义样式的复选框。本质上,我将它用作按钮,以便稍后使用 :checked 选择器执行一些样式设置。我没有将值提交给表单。

<input type="checkbox" id="agree" style="display:none" />
.......
<!-- few more divs here -->
.......
<label for="agree" role="button">Click here</label>
<div>This div will be styled if the checkbox is ticked</div>

我的问题是,从可访问性的角度来看,这是否正确?有没有更语义化的方式来做到这一点? (只有css一定要提)

我需要一个自定义样式的复选框。

我认为正确的语义方式是使用 <button> , 但它不能用作标签,也不会勾选复选框..

最佳答案

执行此操作的标准方法是利用 aria-checkedrole=checkbox 属性,但这需要 Javascript:

<label for="agree">Click here</label>
<div id="agree" role="checkbox" aria-checked="true" tabindex="0">
This div will be styled if the checkbox is ticked</div>

您的示例中的可访问性失败是多方面的:

请注意,您可以完美地使用标准的 checkbox 元素并在其上应用一些 css 而无需完全隐藏它。

<label>
<input type="checkbox" id="agree" value="1" />
<div>I agree with the above elements</div>
</label>

-----
/* the input would be hidden but still focusable */
#agree {width: 0px; margin-left: 2px; overflow: hidden;}

/* when focusing the element, we will outline the text to make the focus visible */
#agree:focus + div {outline: dotted 2px blue}

/* default when unticked */
#agree + div {background-image: url("ko.png") no-repeat; border: solid transparent 2px;}

/* default when ticked */
#agree:checked + div {background-image: url("ok.png") no-repeat; border: solid green 2px;}

这要求您的 CSS 产生明显的差异,而不是仅仅依赖于颜色的使用。

关于html - 可访问的自定义样式复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39874844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com