Custom checkbox using font awesome and css

时光怂恿深爱的人放手 提交于 2019-12-20 06:13:53

问题


I'm making a custom checkbox with font awesome and css.

On click/when checkbox is checked, I trying to create some padding around the black checked box instead(having a smaller black box in the white box when checked/clicked)

@import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);

/*** basic styles ***/

/*** custom checkboxes ***/

input[type=checkbox] {
  display: none;
}
/* to hide the checkbox itself */

input[type=checkbox] + label:before {
  font-family: FontAwesome;
}
input[type=checkbox] + label:before {
  content: "\f096";
}
/* unchecked icon */

/*input[type=checkbox] + label:before { letter-spacing: 10px; }*/

/* space between checkbox and label */

input[type=checkbox]:checked + label:before {
  content: "\f0c8";
}
/* checked icon */

input[type=checkbox]:checked + label:before {
  letter-spacing: 5px;
}
/* allow space for check mark */
<div>
  <input id="box1" type="checkbox" />
  <label for="box1"></label>
  <br>
  <input id="box2" type="checkbox" />
  <label for="box2"></label>
  <br>
  <input id="box3" type="checkbox" />
  <label for="box3"></label>
</div>

回答1:


You're using a single character so you can't add letter-spacing.

I'd suggest something like this.

Reduce the size of the replacement icon/character, add padding and a border. Alternatively, search for a proper icon/character that appears how you want it to be and use that.

@import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);

/*** basic styles ***/

/*** custom checkboxes ***/

input[type=checkbox] {
  display: none;
}
/* to hide the checkbox itself */

input[type=checkbox] + label:before {
  font-family: FontAwesome;
}
input[type=checkbox] + label:before {
  content: "\f096";
}
/* unchecked icon */

/*input[type=checkbox] + label:before { letter-spacing: 10px; }*/

/* space between checkbox and label */

input[type=checkbox]:checked + label:before {
  content: "\f0c8";
  display: inline-block;
}
/* checked icon */

input[type=checkbox]:checked + label:before {
  font-size: 60%;
  border: 1px solid black;
  vertical-align: top;
  padding: 2px;
  border-radius: 2px;
}
/* allow space for check mark */
<div>
  <input id="box1" type="checkbox" />
  <label for="box1"></label>
  <br>
  <input id="box2" type="checkbox" />
  <label for="box2"></label>
  <br>
  <input id="box3" type="checkbox" />
  <label for="box3"></label>
</div>



回答2:


Like this?

@import url(//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css);

/*** basic styles ***/

/*** custom checkboxes ***/

input[type=checkbox] {
  display: none;
}
/* to hide the checkbox itself */

input[type=checkbox] + label:before {
  font-family: FontAwesome;
}
input[type=checkbox] + label:before {
  content: "\f096";
}
/* unchecked icon */

/*input[type=checkbox] + label:before { letter-spacing: 10px; }*/

/* space between checkbox and label */

input[type=checkbox]:checked + label:before {
  content: "\f0c8";
  font-size:12px;
  margin-left:1px;
}
/* checked icon */

input[type=checkbox]:checked + label:before {
  letter-spacing: 5px;
}
/* allow space for check mark */
<div>
  <input id="box1" type="checkbox" />
  <label for="box1"></label>
  <br>
  <input id="box2" type="checkbox" />
  <label for="box2"></label>
  <br>
  <input id="box3" type="checkbox" />
  <label for="box3"></label>
</div>


来源:https://stackoverflow.com/questions/32994677/custom-checkbox-using-font-awesome-and-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!