简体   繁体   English

CSS 复选框样式-大小颜色变化

[英]CSS Checkbox styling - size color change

I am trying to do simple checkbox styling.我正在尝试做简单的复选框样式。 The color change is not working.颜色变化不起作用。

How to change the color and border?如何更改颜色和边框?

 input[type=checkbox]{ height: 25px; width: 25px; background-color: #eee; } input[type=checkbox]:checked { background-color: #3ee738; }
 <p> <input type="checkbox" id="showall" name="showall" value=0>&nbsp; <label for="showall">Show All</label> </p> <p> <input type="checkbox" id="showfl" name="showfl" value=0>&nbsp; <label for="showfl">Filtered</label> </p> <p>

 input[type=checkbox] + label { display: block; margin: 0.2em; cursor: pointer; padding: 0.2em; } input[type=checkbox] { display: none; } input[type=checkbox] + label:before { content: "\2714"; border: 0.1em solid MediumSeaGreen; border-radius: 0.2em; display: inline-block; width: 1em; height: 1em; padding-left: 0.2em; padding-bottom: 0.3em; margin-right: 0.2em; vertical-align: bottom; color: transparent; transition: .2s; } input[type=checkbox] + label:active:before { transform: scale(0); } input[type=checkbox]:checked + label:before { background-color: MediumSeaGreen; border-color: MediumSeaGreen; color: #fff; } input[type=checkbox]:disabled + label:before { transform: scale(1); border-color: #aaa; } input[type=checkbox]:checked:disabled + label:before { transform: scale(1); background-color: #bfb; border-color: #bfb; }
 <p> <input type="checkbox" id="showall" name="showall" value=0>&nbsp; <label for="showall">Show All</label> </p> <p> <input type="checkbox" id="showfl" name="showfl" value=0>&nbsp; <label for="showfl">Filtered</label> </p> <p>

Checkboxes are not currently able to be styled with CSS, as they are typically based on the user's operating system's default styling.复选框当前无法使用 CSS 设置样式,因为它们通常基于用户操作系统的默认样式。 The way to get around this with CSS is to style another element, in your case, the <label> tag to serve as the visual indicator of the checkbox, and then some pseudo selector and pseudo element CSS to trigger the change when the input is checked.使用 CSS 解决此问题的方法是设置另一个元素的样式,在您的情况下, <label>标记用作复选框的视觉指示器,然后一些伪选择器和伪元素 CSS 在输入时触发更改检查。

Because the input and the label are connected via the id and for attributes, shrinking the input hide it works effectively and still maintains accessibility.因为输入和 label 通过idfor属性连接,缩小输入隐藏它有效地工作并且仍然保持可访问性。

 li { position:relative; } input { height:1px; width:1px; position:absolute; opacity:0; top:0; left:0; } label { cursor:pointer; padding-left:25px; } label::before { content:""; position:absolute; left:0; top:0; height:15px; width:15px; border:1px solid blue; } label::after { content:""; color:white; position:absolute; left:2px; top:0; } input[type=checkbox]:checked + label::before { background:blue; } input[type=checkbox]:checked + label::after { content:"✓"; }
 <ul> <li><input id="cat" type="checkbox"> <label for="cat">Cat</label></li> <li><input id="dog" type="checkbox"> <label for="dog">Dog</label></li> </ul>

What you need to do is to hide the actual html element of the checkbox and create a new one that can be styled easily.您需要做的是隐藏复选框的实际 html 元素并创建一个可以轻松设置样式的新元素。

So the logic of this approach is to have a span for the box and a span for the icon and style each accordingly - showing the icon when the input is checked using the :checked pseudoselector.因此,这种方法的逻辑是为框设置一个跨度,为图标和样式分别设置一个跨度——在使用:checked伪选择器检查输入时显示图标。

Note that the label wraps around the whole group so that clicking anywhere on the label will toggle the checkbox - doing it this way removeds the need for the for attribute.请注意,label 环绕整个组,因此单击 label 上的任意位置将切换复选框 - 这样做可以消除对属性的需求。

I also added a checked function that will toggle the other checkbox whebn the showAll check is checked.我还添加了一个已选中的 function,它将在选中 showAll 检查时切换另一个复选框。

Edit - note that you can use other methods of getting the icon in there- you may prefer the font awesome fa-check icon for example.编辑 - 请注意,您可以使用其他方法在其中获取图标 - 例如,您可能更喜欢 font awesome fa-check 图标。

 const showAll = document.querySelector("#showAll"); showAll.addEventListener('click', () => toggleAll()); function toggleAll() { document.querySelector("#showfl").checked = showAll.checked; }
 .checkbox-wrapper { margin-bottom: 16px; } input[type=checkbox]{ display: none }.checkbox-box{ display: inline-block; height: 25px; width: 25px; background-color: #eee; text-align: center }.checkbox-icon { visibility: hidden } input[type=checkbox]:checked +.checkbox-box{ background-color: #3ee738; } input[type=checkbox]:checked +.checkbox-box.checkbox-icon { visibility: visible }
 <div class="checkbox-wrapper"> <label> <input type="checkbox" id="showAll" name="showAll" /> <span class="checkbox-box"> <span class="checkbox-icon">&check;</span> </span> Show All </label> </div> <div class="checkbox-wrapper"> <label> <input type="checkbox" id="showfl" name="showfl" /> <span class="checkbox-box"> <span class="checkbox-icon">&check;</span> </span> Filtered </label> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM