简体   繁体   English

复选框样式仅CSS

[英]Checkbox styling only css

I have a checkbox and i would like to style it. 我有一个复选框,我想为其设置样式。 When the checkbox is unchecked it should have a black border and when the checkbox is checked the hole checkbox should be black without hook. 如果未选中该复选框,则边框应为黑色;选中该复选框时,孔复选框应为黑色且不带钩。 Below you see what i have done so far. 在下面您可以看到我到目前为止所做的事情。 Sadly it wont work. 可悲的是它不会工作。

My Code: 我的代码:

<form>
    <input class="newsletter" type="checkbox" value="text">Ich möchte den Newsletter bekommen
</form>

input[type=checkbox]:not(old){
  width     : 13px;
  margin    : 0;
  padding   : 0;
  opacity   : 0;
}  

.newsletter:unchecked{
    width:13px;
    height: 13px;
    border: 2px solid black;
border-radius:3px;
}

.newsletter:checked{
    width:13px;
    height: 13px;
    border: 2px solid black;
    background-color:black;
border-radius:3px;
}

The first part of my code should hide the current checkbox. 我的代码的第一部分应该隐藏当前的复选框。 The second part should be the checkbox when the box is unchecked and the third part when the box is checked. 如果未选中该框,则第二部分应为复选框,而选中该框时,第二部分应为复选框。 I thought this is how you are styling these checkboxes. 我认为这就是您设计这些复选框的方式。 What am i doing wrong? 我究竟做错了什么?

This may help you 这可能对您有帮助

 .checkbox input[type=checkbox] { display: none; } .checkbox input[type=checkbox]:not(:checked) + label:before { content: ""; border: 2px solid #000; height: 10px; width: 10px; display: inline-block; } .checkbox input[type=checkbox]:checked + label:before { content: ""; border: 2px solid #000; height: 10px; width: 10px; background: #000; display: inline-block; } 
 <form> <div class="checkbox"> <input id="check" class="newsletter" type="checkbox" value="text"> <label for="check"> Ich möchte den Newsletter bekommen</label> </div> </form> 

The first thing you need to do with your code is add a label , eg: 您需要对代码做的第一件事是添加label ,例如:

<form>
  <input class="newsletter" type="checkbox" value="text" id="newsletter" name="newsletter">
   <label for="newsletter">Ich möchte den Newsletter bekommen</label>
</form>

I've also added an id and name attribute to the checkbox. 我还向该复选框添加了idname属性。 The id needs to be the same as the for attribute on the label for this trick to work. id必须与label上的for属性相同,此技巧才能起作用。

Next, we need to visibly hide the checkbox. 接下来,我们需要显式隐藏该复选框。 I tend to use a class of sr-only in the CSS to visibly hide things based on Yahoo's example : 我倾向于在CSS中使用一类sr-only来根据Yahoo的示例明显地隐藏事物:

.sr-only{
  height: 1px;
  width: 1px;
  overflow: hidden
  clip: rect(1px,1px,1px,1px);
  position: absolute;
}  

Next we create a pseudo element before the label. 接下来,我们在标签before创建一个伪元素。 For the style rules I'm using the adjacent sibling selector ( + ) to apply the rules when the label is an adjacent sibling of the checkbox. 对于样式规则,当标签是复选框的相邻兄弟时,我使用相邻的兄弟选择器( + )来应用规则。 I'm keeping your class name so it'll only target labels that're siblings of class newsletter : 我保留了您的班级名称,因此它将仅定位到班级newsletter同级标签:

.newsletter:not(checked) + label:before{
  content:" ";  
  display: inline-block;
  width:13px;
  height: 13px;
  border: 2px solid black;
  border-radius:3px;
}

The :not(checked) means apply these rules when newsletter is not checked. :not(checked)表示不检查newsletter时应用这些规则。

To change the styles when newsletter is checked we change the background colour like this: 要在checked newsletter时更改样式,我们可以像这样更改背景色:

.newsletter:checked + label:before{
 background-color:black;
}

This way clicking our pseudo checkbox or the text in the label will check the box (clicking the label at all sends focus to the field, so with a checkbox will check it). 这样,单击我们的伪复选框或标签中的文本将选中该复选框(完全单击标签会将焦点发送到该字段,因此带有复选框将对其进行选中)。

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

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