{"id":249463,"date":"2017-01-01T12:33:40","date_gmt":"2017-01-01T19:33:40","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=249463"},"modified":"2020-04-09T16:17:36","modified_gmt":"2020-04-09T23:17:36","slug":"indeterminate","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/selectors\/i\/indeterminate\/","title":{"rendered":":indeterminate"},"content":{"rendered":"
<\/p>\n The most common place we might see this in play is with checkboxes in a form:<\/p>\n There are a few quirky things about First off, there is no way to set a checkbox to an indeterminate state in HTML. In the opening example above, we were able to set the second checkbox to checked by explicitly saying so in the HTML.<\/p>\n It is only logical to assume we could do the same with an indeterminate state:<\/p>\n But, sadly, that is not the case so do not use that last example in your code.<\/p>\n At the time of this writing, Javascript is the only means for setting an indeterminate state on a checkbox. One way to go about it is to select a specific checkbox by ID:<\/p>\n The limitation of the example above is that the checkbox can never get back to an indeterminate state once it changes to another state. Instead, we can rotate between checked, unchecked and indeterminate states:<\/p>\n See the Pen Rotate Checkbox States<\/a> by Geoff Graham (@geoffgraham<\/a>) on CodePen<\/a>.<\/p>\n A checkbox still only counts whether it has been checked or unchecked, regardless of whether we have activated an indeterminate state. In other words, indeterminate masks the actual value of the checkbox and does not count as a value of its own.<\/p>\n Like numeric inputs<\/a>, an indeterminate state does not style consistently in every browser.<\/p>\n Overall, however, here is the support for indeterminate checkboxes.<\/p>\n This browser support data is from Caniuse<\/a>, which has more detail. A number indicates that browser supports the feature at that version and up.<\/p><\/div>:indeterminate<\/code> is a pseudo-class selector in CSS named for a state that is neither checked nor unchecked. It’s that in-between state that we might consider the “Maybe” between “Yes” and “No” options. The state is not fully determined, hence the name: indeterminate.<\/p>\n
Indeterminate Checkboxes<\/h3>\n
:indeterminate<\/code> when it comes to checkboxes that are worth noting before digging into how it can be selected in CSS.<\/p>\n
It cannot be set in the HTML<\/h4>\n
<!-- This is checked by default -->\n<input type=\"checkbox\" checked=\"\"><\/code><\/pre>\n
<!-- This does not work! -->\n<input type=\"checkbox\" indeterminate=\"\"><\/code><\/pre>\n
var checkbox = document.getElementById(\"some-checkbox\");\ncheckbox.indeterminate = true;<\/code><\/pre>\n
<!-- Inline click handler, just for demo -->\n<input type=\"checkbox\" id=\"demo\" onclick=\"toggle(this)\"><\/code><\/pre>\n
function toggle(demo) {\n if (demo.readOnly) demo.checked=demo.readOnly=false;\n else if (!demo.checked) demo.readOnly=demo.indeterminate=true;\n}<\/code><\/pre>\n
It is purely a visual state<\/h4>\n
It’s default appearance is inconsistent across browsers<\/h4>\n
Desktop<\/h4>