Skip to content Skip to sidebar Skip to footer

Center A Check/tick Within A Custom Check Box

I am building a custom check box, the functionality is almost there but the styling has a way to go. I have a few problems, I need the check centered within the circle I only need

Solution 1:

  • Problem 1: text-align: center; when checkbox ic checked, and remove style="float:left;" from checkbox
  • Problem 2: Remove the text from accord-text div and put it outside.

.row{
  display: flex;
  flex-direction: row;
}

input[type=checkbox] {
  display: none;
}

input[type=checkbox] + .accord-text:before {
  width: 30px;
  height: 30px;
  border-radius: 200%;
  background-color: #d6e4ec;
  border: 1px solid #000;
  display: block;
  font-size: 140%;
  font-weight: 900;
  content: "";
  color: green;
}

input[type=checkbox]:checked + .accord-text:before {
  display: table;
  content: "\2713";
  text-align: center;
}
<divclass="row"><labelfor='product-45-45'><inputtype='checkbox'style="float:left;"id='product-45-45' /><divclass="accord-text"></div></label><strong>header:</strong> sub text
  <strong>more text!</strong></div>

Solution 2:

Check with this snippet also i have fixed the height issue when we checked ,

Add display:block and max-height to this input[type=checkbox]:checked + .accord-text:before

input[type=checkbox] {
  display: none;
}
label {
float:left;
}
input[type=checkbox] + .accord-text:before {
  width: 30px;
  height: 30px;
  border-radius: 200%;
  background-color: #d6e4ec;
  border: 1px solid #000;
  display: block;
  font-size: 150%;
  font-weight: 900;
  content: "";
  color: green;
  text-align:center;
  max-height:30px;
}

input[type=checkbox]:checked + .accord-text:before {
  display: table;
  content: "\2713";
  max-height:30px;
  display:block;
}
span {
float:left;
padding-left:5px;
}
<labelfor='product-45-45'><inputtype='checkbox'style="float:left;"id='product-45-45' /><divclass="accord-text"></div></label><span><strong>header:</strong> sub text
  <strong>more text!</strong></span>

Solution 3:

You need to remove the inline code that's styling the input to float left, you can't center it while that float is on. Then add margin: 0 auto to center it:

input[type=checkbox]:checked {
  float: none;
}

input[type=checkbox] + .accord-text:before {
  margin: 0 auto;
  width: 30px;
  height: 30px;
  border-radius: 200%;
  background-color: #d6e4ec;
  border: 1px solid #000;
  display: block;
  font-size: 150%;
  font-weight: 900;
  content: "";
  color: green;
}

Post a Comment for "Center A Check/tick Within A Custom Check Box"