如何使用 CSS 设置复选框样式
我正在尝试使用以下内容设置复选框的样式:
<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />
但该样式并未应用。该复选框仍显示其默认样式。如何赋予它指定的样式?
I am trying to style a checkbox using the following:
<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />
But the style is not applied. The checkbox still displays its default style. How do I give it the specified style?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
您可以使用
label
元素通过一些技巧来设置复选框的样式,示例如下:以及上述代码的 FIDDLE 。请注意,某些 CSS 在旧版本的浏览器中不起作用,但我确信有一些精美的 JavaScript 示例!
You can style checkboxes with a little trickery using the
label
element an example is below:And a FIDDLE for the above code. Note that some CSS doesn't work in older versions of browsers, but I'm sure there are some fancy JavaScript examples out there!
您可以避免添加额外的标记。通过设置 CSS
appearance
,这可以在除 IE 之外的任何地方使用:You can avoid adding extra markup. This works everywhere except IE via setting CSS
appearance
:最近我发现了一个非常有趣的问题解决方案。
您可以使用
appearance: none;
关闭复选框的默认样式,然后按照 此处(示例 4)。不幸的是,浏览器对外观选项的支持非常糟糕。根据我的个人测试,我只让 Opera 和 Chrome 正常工作。但是,当有更好的支持或者您只想使用 Chrome/Opera 时,这将是保持简单的方法。
示例 JSFiddle
“我可以使用吗?”链接
Recently I found a quite interesting solution to the problem.
You could use
appearance: none;
to turn off the checkbox's default style and then write your own over it like described here (Example 4).Unfortunately browser support is quite bad for the
appearance
option. From my personal testing I only got Opera and Chrome working correctly. But this would be the way to go to keep it simple when better support comes or you only want to use Chrome/Opera.Example JSFiddle
"Can I use?" link
我更喜欢使用图标字体(例如 FontAwesome),因为使用 CSS 修改它们的颜色很容易,而且它们在高像素密度设备上的缩放效果非常好。这是另一个纯 CSS 变体,使用与上面类似的技术。
(下面是一个静态图像,以便您可以直观地看到结果;请参阅 JSFiddle 了解交互式版本.)
与其他解决方案一样,它使用
label
元素。相邻的span
包含我们的复选框字符。这是它的 JSFiddle 。
I prefer to use icon fonts (such as FontAwesome) since it's easy to modify their colours with CSS, and they scale really well on high pixel-density devices. So here's another pure CSS variant, using similar techniques to those above.
(Below is a static image so you can visualize the result; see the JSFiddle for an interactive version.)
As with other solutions, it uses the
label
element. An adjacentspan
holds our checkbox character.Here's the JSFiddle for it.
我的解决方案
My solution
您可以在现代浏览器上简单地使用
appearance: none
,这样就没有默认样式并且所有样式都正确应用:You can simply use
appearance: none
on modern browsers, so that there is no default styling and all your styles are applied properly:这是一个简单的 CSS 解决方案,没有任何 jQuery 或 JavaScript 代码。
我正在使用 FontAwseome 图标,但您可以使用任何图像
Here is a simple CSS solution without any jQuery or JavaScript code.
I am using FontAwseome icons but you can use any image
根据我的谷歌搜索,这是复选框样式最简单的方法。只需根据您的设计添加
:after
和:checked:after
CSS 即可。From my googling, this is the easiest way for checkbox styling. Just add
:after
and:checked:after
CSS based on your design.使用纯 CSS 修改复选框样式。这不需要任何 JavaScript 或 HTML 操作:
Modify the checkbox style with plain CSS. This does not require any JavaScript or HTML manipulation:
哎呀!所有这些解决方法让我得出这样的结论:如果你想设计 HTML 复选框的样式,它有点糟糕。
预先警告一下,这不是 CSS 实现。我只是想分享我想出的解决方法,以防其他人发现它有用。
我使用了 HTML5
canvas
元素。这样做的好处是您不必使用外部图像,并且可能可以节省一些带宽。
缺点是,如果浏览器由于某种原因无法正确呈现它,则没有后备方案。尽管这在 2017 年是否仍然是一个问题还有待商榷。
更新
我发现旧代码相当难看,所以我决定重写它。
这是一个工作演示。
新版本使用原型和差异继承来创建一个用于创建复选框的高效系统。创建复选框:
这将立即将复选框添加到 DOM 并挂接事件。查询是否选中复选框:
另外值得注意的是我摆脱了循环。
更新 2
我在上次更新中忽略的一点是,使用画布比仅仅制作一个看起来像你想要的样子的复选框有更多的优势。如果您愿意,您还可以创建多状态复选框。
我稍微修改了最后一个代码片段中使用的
Checkbox
,使其更加通用,从而可以使用具有 3 个状态的复选框“扩展”它。这是演示。正如您所看到的,它已经具有比内置复选框更多的功能。在 JavaScript 和 CSS 之间进行选择时需要考虑的一些事项。
旧的、设计不佳的代码
工作演示
首先,设置画布
接下来,设计一种拥有画布的方法更新自己。
最后一部分是使其具有交互性。幸运的是,这非常简单:
这就是 IE 中可能会遇到问题的地方,因为 JavaScript 中的事件处理模型很奇怪。
我希望这对某人有帮助;它绝对适合我的需求。
Yikes! All these workarounds have led me to the conclusion that the HTML checkbox kind of sucks if you want to style it.
As a forewarning, this isn't a CSS implementation. I just thought I'd share the workaround I came up with in case anyone else might find it useful.
I used the HTML5
canvas
element.The upside to this is that you don't have to use external images and can probably save some bandwidth.
The downside is that if a browser for some reason can't render it correctly, then there's no fallback. Though whether this remains an issue in 2017 is debatable.
Update
I found the old code quite ugly, so I decided to give it a rewrite.
Here's a working demo.
The new version uses prototypes and differential inheritance to create an efficient system for creating checkboxes. To create a checkbox:
This will immediately add the checkbox to the DOM and hook up the events. To query whether a checkbox is checked:
Also important to note is that I got rid of the loop.
Update 2
Something I neglected to mention in the last update is that using the canvas has more advantages than just making a checkbox that looks however you want it to look. You could also create multi-state checkboxes, if you wanted to.
I modified slightly the
Checkbox
used in the last snippet so that it is more generic, making it possible to "extend" it with a checkbox that has 3 states. Here's a demo. As you can see, it already has more functionality than the built-in checkbox.Something to consider when you're choosing between JavaScript and CSS.
Old, poorly-designed code
Working Demo
First, set up a canvas
Next, devise a way to have the canvas update itself.
The last part is to make it interactive. Luckily, it's pretty simple:
This is where you might have problems in IE, due to their weird event handling model in JavaScript.
I hope this helps someone; it definitely suited my needs.
SCSS / SASS 实现
更现代的方法
对于那些使用 SCSS(或轻松转换为 SASS)的人来说,以下内容将会有所帮助。实际上,在复选框旁边创建一个元素,这就是您要设置样式的元素。单击该复选框时,CSS 会重新设置姊妹元素的样式(为新的选中样式)。代码如下:
SCSS / SASS Implementation
A more modern approach
For those using SCSS (or easily converted to SASS), the following will be helpful. Effectively, make an element next to the checkbox, which is the one that you will style. When the checkbox is clicked, the CSS restyles the sister element (to your new, checked style). Code is below:
还有一个简单且轻量级的模板:
https://jsfiddle.net/rvgccn5b/
A simple and lightweight template as well:
https://jsfiddle.net/rvgccn5b/
我认为最简单的方法是设置
label
样式并使checkbox
不可见。HTML
CSS
复选框
,即使它是隐藏的,仍然可以访问,并且它的值将在提交表单时发送。对于旧浏览器,您可能必须使用 JavaScript 将label
的类更改为检查,因为我认为旧版本的 Internet Explorer 无法理解::checked
上的 <代码>复选框。I think the easiest way to do it is by styling a
label
and making thecheckbox
invisible.HTML
CSS
The
checkbox
, even though it is hidden, will still be accessible, and its value will be sent when a form is submitted. For old browsers you might have to change the class of thelabel
to checked using JavaScript because I don't think old versions of Internet Explorer understand::checked
on thecheckbox
.不需要 JavaScript 或 jQuery。
简单地更改您的复选框样式。
这是一个 JSFiddle 链接
No JavaScript or jQuery required.
Change your checkbox style simple way.
Here is a JSFiddle link
不,您仍然无法设置复选框本身的样式,但我(最终)弄清楚了如何设置幻觉的样式,同时保留单击复选框的功能。这意味着即使光标不是完全静止,您也可以切换它,而不必冒选择文本或触发拖放的风险!
该解决方案可能也适合单选按钮。
以下适用于 Internet Explorer 9、Firefox 30.0 和 Chrome 40.0.2214.91,只是一个基本示例。您仍然可以将它与背景图像和伪元素结合使用。
http://jsfiddle.net/o0xo13yL/1/
No, you still can't style the checkbox itself, but I (finally) figured out how to style an illusion while keeping the functionality of clicking a checkbox. It means that you can toggle it even if the cursor isn't perfectly still without risking selecting text or triggering drag-and-drop!
This solution probably also fits radio buttons.
The following works in Internet Explorer 9, Firefox 30.0 and Chrome 40.0.2214.91 and is just a basic example. You can still use it in combination with background images and pseudo-elements.
http://jsfiddle.net/o0xo13yL/1/
这是一个带有一点动画的现代版本,您可以自定义简单的样式:
Here's a modern version with a little animation, and simple styling you can customize:
使用 CSS 自定义复选框(WebKit 浏览器解决方案仅限 Chrome、Safari、移动浏览器)
CSS:
Custom checkbox with CSS (WebKit browser solution only Chrome, Safari, Mobile browsers)
CSS:
通过将 Materialize 与自定义样式表结合使用,您可以实现如下效果:
CSS 代码
< strong>HTML 代码
演示
JSFiddle 演示
By using Materialize with a custom stylesheet, you can achieve something like this:
CSS code
HTML code
Demo
JSFiddle demo
这帮助我更改了复选框的样式(颜色)
我们也可以将相同的方法用于单选按钮。
This helped me to change style (color) for checkbox
We can also use the same for radio buttons.
这是最简单的方法,您可以选择要提供此样式的复选框。
CSS:
HTML:
This is simplest way and you can choose which checkboxes to give this style.
CSS:
HTML:
更新:
下面的答案引用了 CSS 广泛使用之前的状态 3。在现代浏览器(包括 Internet Explorer 9 及更高版本)中,可以更直接地使用您喜欢的样式创建复选框替换,而无需使用 JavaScript。
以下是一些有用的链接:
值得注意的是,根本问题没有改变。您仍然无法将样式(边框等)直接应用于复选框元素并使这些样式影响 HTML 复选框的显示。然而,发生的变化是,现在可以隐藏实际的复选框并将其替换为您自己的样式元素,只使用 CSS 即可。特别是,由于 CSS 现在具有广泛支持的
:checked
选择器,因此您可以使替换正确反映框的选中状态。旧答案
这是一篇关于样式复选框的有用文章。基本上,该作者发现它在不同浏览器之间存在巨大差异,并且无论您如何设计它,许多浏览器总是显示默认复选框。所以确实没有一个简单的方法。
不难想象一种解决方法,您可以使用 JavaScript 将图像覆盖在复选框上,然后单击该图像导致选中真正的复选框。没有 JavaScript 的用户会看到默认复选框。
编辑添加:这里是 一个很好的脚本,可以为您完成此操作;它隐藏真正的复选框元素,将其替换为样式化的跨度,并重定向单击事件。
UPDATE:
The below answer references the state of things before widespread availability of CSS 3. In modern browsers (including Internet Explorer 9 and later) it is more straightforward to create checkbox replacements with your preferred styling, without using JavaScript.
Here are some useful links:
It is worth noting that the fundamental issue has not changed. You still can't apply styles (borders, etc.) directly to the checkbox element and have those styles affect the display of the HTML checkbox. What has changed, however, is that it's now possible to hide the actual checkbox and replace it with a styled element of your own, using nothing but CSS. In particular, because CSS now has a widely supported
:checked
selector, you can make your replacement correctly reflect the checked status of the box.OLDER ANSWER
Here's a useful article about styling checkboxes. Basically, that writer found that it varies tremendously from browser to browser, and that many browsers always display the default checkbox no matter how you style it. So there really isn't an easy way.
It's not hard to imagine a workaround where you would use JavaScript to overlay an image on the checkbox and have clicks on that image cause the real checkbox to be checked. Users without JavaScript would see the default checkbox.
Edited to add: here's a nice script that does this for you; it hides the real checkbox element, replaces it with a styled span, and redirects the click events.
通过使用
:after
和:before
伪类附带的新功能,您可以实现非常酷的自定义复选框效果。这样做的优点是:您不需要向 DOM 添加任何其他内容,只需添加标准复选框即可。请注意,这仅适用于兼容的浏览器。我相信这与某些浏览器不允许您在输入元素上设置
:after
和:before
有关。不幸的是,这意味着目前仅支持 WebKit 浏览器。 Firefox + Internet Explorer 仍然允许复选框发挥作用,只是没有样式,这有望在未来发生变化(代码不使用供应商前缀)。这只是一个 WebKit 浏览器解决方案(Chrome、Safari、移动浏览器)
参见小提琴示例
额外的 Webkit 风格翻转小提琴
You can achieve quite a cool custom checkbox effect by using the new abilities that come with the
:after
and:before
pseudo classes. The advantage to this, is: You don't need to add anything more to the DOM, just the standard checkbox.Note this will only work for compatible browsers. I believe this is related to the fact that some browsers do not allow you to set
:after
and:before
on input elements. Which unfortunately means for the moment only WebKit browsers are supported. Firefox + Internet Explorer will still allow the checkboxes to function, just unstyled, and this will hopefully change in the future (the code does not use vendor prefixes).This is a WebKit browser solution only (Chrome, Safari, Mobile browsers)
See Example Fiddle
Bonus Webkit style flipswitch fiddle
开始之前(截至 2015 年 1 月)
原始问题和答案已有约 5 年历史。因此,这是一个小小的更新。
首先,设置复选框样式有多种方法。基本原则是:
您需要隐藏由浏览器设置样式的默认复选框控件,并且不能使用 CSS 以任何有意义的方式覆盖。
隐藏控件后,您仍然需要能够检测并切换其选中状态。
复选框的选中状态需要通过新元素的样式来反映。
解决方案(原则上)
上述内容可以通过多种方式来完成——您经常会听说使用 CSS3 伪元素是正确的方法。实际上,没有真正的正确或错误的方法,这取决于最适合您将使用它的上下文的方法。也就是说,我有一个首选的方法。
将复选框包裹在
label
元素中。这意味着即使它被隐藏,您仍然可以通过单击标签内的任意位置来切换其选中状态。隐藏您的复选框。
在您将相应设置样式的复选框之后添加一个新元素。它必须出现在复选框之后,以便可以使用 CSS 选择它并根据
:checked
状态设置样式。 CSS 无法选择“向后”。解决方案(代码中)
细化(使用图标)
“但是嘿!”我听到你喊叫。如果我想在框中显示一个漂亮的小勾号或十字怎么办?而且我不想使用背景图片!
嗯,这就是 CSS3 的伪元素可以发挥作用的地方。它们支持
content
属性,该属性允许您注入代表任一状态的 Unicode 图标。或者,您可以使用第三方字体图标源,例如 font Awesome(但请确保您还设置了相关的font-family
,例如FontAwesome
)Before you begin (as of Jan 2015)
The original question and answer are now ~5 years old. As such, this is a little bit of an update.
Firstly, there are a number of approaches when it comes to styling checkboxes. The basic tenet is:
You will need to hide the default checkbox control which is styled by your browser, and cannot be overridden in any meaningful way using CSS.
With the control hidden, you will still need to be able to detect and toggle its checked state.
The checked state of the checkbox will need to be reflected by styling a new element.
The solution (in principle)
The above can be accomplished by a number of means — and you will often hear that using CSS3 pseudo-elements is the right way. Actually, there is no real right or wrong way, it depends on the approach most suitable for the context you will be using it in. That said, I have a preferred one.
Wrap your checkbox in a
label
element. This will mean that even when it is hidden, you can still toggle its checked state by clicking anywhere within the label.Hide your checkbox.
Add a new element after the checkbox which you will style accordingly. It must appear after the checkbox so it can be selected using CSS and styled dependent on the
:checked
state. CSS cannot select 'backwards'.The solution (in code)
Refinement (using icons)
"But hey!" I hear you shout. What about if I want to show a nice little tick or cross in the box? And I don't want to use background images!
Well, this is where CSS3's pseudo-elements can come into play. These support the
content
property which allows you to inject Unicode icons representing either state. Alternatively, you could use a third party font icon source such as font awesome (though make sure you also set the relevantfont-family
, e.g. toFontAwesome
)有一种方法可以仅使用 CSS 来做到这一点。我们可以(ab)使用
label
元素并设置该元素的样式。需要注意的是,这不适用于 Internet Explorer 8 及更低版本。There is a way to do this using just CSS. We can (ab)use the
label
element and style that element instead. The caveat is that this will not work for Internet Explorer 8 and lower versions.现代可访问解决方案 - 使用
accent-color
使用新的
accent-color
属性,并确保满足适当的 对比度为 3:1,以确保可访问性。这也适用于radio
按钮。旧答案,如果您需要比上述提供更多的自定义功能,我只推荐这个:
我一直在滚动和滚动,大量这些答案只是将可访问性排除在外,以多种方式违反 WCAG。我添加了单选按钮,因为大多数时候,当您使用自定义复选框时,您也需要自定义单选按钮。
Fiddles:
迟到,但不知怎的,这在
2019中仍然很困难,< s>2020、2021,所以我添加了三个可访问且易于的解决方案。这些都是无 JavaScript >、可访问和免费外部库*...
如果您想即插即用其中任何一个,只需从小提琴中复制样式表,编辑CSS 中的颜色代码可满足您的需求,然后就可以开始使用了。如果需要复选框,您可以添加自定义 svg 复选标记图标。我为那些不懂 CSS 的人添加了很多评论。
如果您有长文本或小容器,并且在复选框或单选按钮输入下方遇到文本换行,则只需转换为 div
更长的解释:
我需要一个不违反 WCAG、不依赖 JavaScript 或外部库、不会破坏键盘导航(如 Tab 键或空格键选择)、允许焦点事件、允许禁用同时选中的复选框的解决方案并未经选中,最后是一个解决方案,我可以根据需要使用不同的
background-color
's、border-radius
、svg< 自定义复选框的外观/code> 背景等。
我使用了 @Jan Turoň 的这个答案的一些组合来提出我自己的解决方案似乎效果很好。我已经完成了一个单选按钮小提琴,它使用了复选框中的许多相同代码,以便也可以与单选按钮一起使用。
我仍在学习可访问性,因此如果我错过了某些内容,请发表评论,我会尽力纠正它。
这是我的复选框的代码示例:
Modern accessible solution - use
accent-color
Use the new
accent-color
property and make certain to meet a proper contrast ratio of 3:1 to ensure accessibility. This also works forradio
buttons.Old answer, I only recommend this if you need more customization than the above offers:
I have been scrolling and scrolling and tons of these answers simply throw accessibility out the door and violate WCAG in more than one way. I threw in radio buttons since most of the time when you're using custom checkboxes you want custom radio buttons too.
Fiddles:
Late to the party but somehow this is still difficult in
2019,2020, 2021 so I have added my three solutions which are accessible and easy to drop in.These are all JavaScript free, accessible, and external library free*...
If you want to plug-n-play with any of these just copy the style sheet from the fiddles, edit the color codes in the CSS to fit your needs, and be on your way. You can add a custom svg checkmark icon if you want for the checkboxes. I've added lots of comments for those non-CSS'y folks.
If you have long text or a small container and are encountering text wrapping underneath the checkbox or radio button input then just convert to divs like this.
Longer explanation:
I needed a solution that does not violate WCAG, doesn't rely on JavaScript or external libraries, and that does not break keyboard navigation like tabbing or spacebar to select, that allows focus events, a solution that allows for disabled checkboxes that are both checked and unchecked, and finally a solution where I can customize the look of the checkbox however I want with different
background-color
's,border-radius
,svg
backgrounds, etc.I used some combination of this answer from @Jan Turoň to come up with my own solution which seems to work quite well. I've done a radio button fiddle that uses a lot of the same code from the checkboxes in order to make this work with radio buttons too.
I am still learning accessibility so if I missed something please drop a comment and I will try to correct it.
Here is a code example of my checkboxes:
我总是使用伪元素
:before
和:after
来更改复选框和单选按钮的外观。它就像一个魅力。请参阅此链接了解更多信息
CODEPEN
步骤
等 CSS 规则隐藏默认复选框visibility:hidden
或opacity:0
或position:absolute;left:-9999px
等。:before
创建一个假复选框元素并传递一个空的或不间断的空格'\00a0'
;:checked
状态时,传递unicodecontent: "\2713"
,即复选标记;:focus
样式以使复选框可访问。这是我的做法。
使用
:before
和:after
更加时尚I always use pseudo elements
:before
and:after
for changing the appearance of checkboxes and radio buttons. it's works like a charm.Refer this link for more info
CODEPEN
Steps
visibility:hidden
oropacity:0
orposition:absolute;left:-9999px
etc.:before
element and pass either an empty or a non-breaking space'\00a0'
;:checked
state, pass the unicodecontent: "\2713"
, which is a checkmark;:focus
style to make the checkbox accessible.Here is how I did it.
Much more stylish using
:before
and:after
我会遵循SW4的答案。不再是了:Volomike的答案远远优于这里的所有答案(请注意我在答案评论中建议的改进)。如果您对本答案评论的替代方法感到好奇,请继续阅读本答案。首先,隐藏复选框并用自定义跨度覆盖它,建议使用以下 HTML:
换行标签整齐地允许单击文本,而无需“for-id”属性链接。但是,
不要使用
visibility:hidden
或display:none
隐藏它。它通过单击或点击来工作,但这是使用复选框的蹩脚方式。有些人仍然使用更有效的 Tab 来移动焦点,使用 Space 来激活,并使用该方法隐藏来禁用它。如果表单很长,可以使用
tabindex
或accesskey
属性来节省某人的时间。如果您观察系统复选框的行为,就会发现悬停时有一个不错的阴影。样式良好的复选框应该遵循这种行为。cobberboy的答案建议Font Awesome 通常比位图更好,因为字体是可缩放矢量。使用上面的 HTML,我建议使用以下 CSS 规则:
隐藏复选框
我只使用负
z-index
,因为我的示例使用足够大的复选框皮肤来完全覆盖它。我不推荐left: -999px
因为它不能在每个布局中重复使用。 Bushan wagh的答案提供了一种隐藏它的防弹方法并说服浏览器使用tabindex,所以它是一个很好的选择。无论如何,两者都只是一个黑客。今天正确的方法是appearance: none
,请参阅乔斯特的回答:样式复选框标签
添加复选框外观
\00f096
是 Font Awesome 的square-o
,调整填充以在焦点上提供均匀的虚线轮廓(见下文)。添加复选框选中的皮肤
\00f046
是Font Awesome的check-square-o
,与square-o<宽度不一样/code>,这就是上面宽度样式的原因。
添加焦点轮廓
Safari 不提供此功能(请参阅@Jason Sankey 的评论),请参阅此答案了解仅限 Safari 的 CSS
为禁用的复选框设置灰色
在非禁用的复选框上设置悬停阴影
在JS Fiddle上测试它
尝试将鼠标悬停在复选框上,然后使用 Tab 和 Shift+Tab 进行移动,使用 Space 进行切换。
I'd follow the advice of SW4's answer. Not anymore: Volomike's answer is far superior to all the answers here (note my suggested improvement in the comment to the answer). Proceed reading this answer if you are curious about alternative approaches, which this answer comments.First of all, hide the checkbox and to cover it with a custom span, suggesting this HTML:
The wrap in label neatly allows clicking the text without the need of "for-id" attribute linking. However,
Do not hide it using
visibility: hidden
ordisplay: none
It works by clicking or tapping, but that is a lame way to use checkboxes. Some people still use much more effective Tab to move focus, Space to activate, and hiding with that method disables it. If the form is long, one will save someone's wrists to use
tabindex
oraccesskey
attributes. And if you observe the system checkbox behavior, there is a decent shadow on hover. The well styled checkbox should follow this behavior.cobberboy's answer recommends Font Awesome which is usually better than bitmap since fonts are scalable vectors. Working with the HTML above, I'd suggest these CSS rules:
Hide checkboxes
I use just negative
z-index
since my example uses big enough checkbox skin to cover it fully. I don't recommendleft: -999px
since it is not reusable in every layout. Bushan wagh's answer provides a bulletproof way to hide it and convince the browser to use tabindex, so it is a good alternative. Anyway, both is just a hack. The proper way today isappearance: none
, see Joost's answer:Style checkbox label
Add checkbox skin
\00f096
is Font Awesome'ssquare-o
, padding is adjusted to provide even dotted outline on focus (see below).Add checkbox checked skin
\00f046
is Font Awesome'scheck-square-o
, which is not the same width assquare-o
, which is the reason for the width style above.Add focus outline
Safari doesn't provide this feature (see @Jason Sankey's comment), see this answer for Safari-only CSS
Set gray color for disabled checkbox
Set hover shadow on non-disabled checkbox
Test it on JS Fiddle
Try to hover the mouse over the checkboxes and use Tab and Shift+Tab to move and Space to toggle.
CSS 基本用户界面模块级别 4 添加了对此的支持(最终)通过一个名为
accent-color
的新解决方案,它实际上非常简单,与这里几乎所有其他答案不同:将您想要的任何CSS颜色(例如命名值、十六进制代码等)设置为强调颜色的值,并且它将被应用。
目前,该功能适用于 Chrome (v93+)、Edge (v93+)、Firefox (v92+)、Opera (v79+) 和 Safari (v15.4+)。
注意:Edge、Chrome 和 Opera(可能还有 Safari;我无法测试)目前也不支持通过
rgba()
的 alpha 通道值(rgba( 的 RGB 值) )
仍将“工作”;Alpha 通道将被浏览器忽略)。有关更多信息,请参阅 MDN 浏览器支持。The CSS Basic User Interface Module Level 4 adds support for this (finally) via a new solution called
accent-color
, and it's actually quite simple, unlike pretty much every other answer here:Set whatever CSS color (e.g. named value, hex code, etc.) you want as the value of
accent-color
, and it will be applied.This currently works in Chrome (v93+), Edge (v93+), Firefox (v92+), Opera (v79+), and Safari (v15.4+).
Note: Edge, Chrome, and Opera (and possibly Safari; I can't test that) currently don't support alpha channel values via
rgba()
either (the RGB values ofrgba()
will still "work"; the alpha channel will simply be ignored by the browser). See MDN Browser Support for more information.使用纯 CSS,
:before
和:after
没什么特别的,没有转换,您可以关闭默认外观,然后使用内联背景图像对其进行样式设置,如下例所示。这适用于 Chrome、Firefox、Safari,现在还有 Edge (Chromium Edge)。With pure CSS, nothing fancy with
:before
and:after
, no transforms, you can turn off the default appearance and then style it with an inline background image like the following example. This works in Chrome, Firefox, Safari, and now Edge (Chromium Edge).易于实施且易于定制的解决方案
经过大量搜索和测试,我得到了这个易于实施且易于定制的解决方案。 在此解决方案中:
页面中的额外 HTML
和 id
Simple 将流动的 CSS 放在页面顶部,所有复选框样式将像这样更改:
Simple to implement and easily customizable solution
After a lot of search and testing I got this solution which is simple to implement and easier to customize. In this solution:
extra HTML in your page
and id
Simple put the flowing CSS at the top of your page and all checkboxes style will change like this: