Benjamin Oats
Benjamin Oats

Reputation: 573

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,

  1. I need the check centered within the circle
  2. I only need the check to be active on click of the checkbox itself not the text to.

Im afraid i cant seem to center the check without using padding left of position left.

HTML

<label for='product-45-45'>
  <input type='checkbox' style="float:left;" id='product-45-45' />
  <div class="accord-text">
    <strong>header:</strong> sub text
    <strong>more text!</strong>
  </div>
</label>

CSS

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: 150%;
  font-weight: 900;
  content: "";
  color: green;
}

input[type=checkbox]:checked + .accord-text:before {
  display: table;
  content: "\2713";
}

Upvotes: 3

Views: 5769

Answers (3)

Jishnu V S
Jishnu V S

Reputation: 8409

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;
}
<label for='product-45-45'>
  <input type='checkbox' style="float:left;" id='product-45-45' />
  <div class="accord-text"></div>
</label>
<span><strong>header:</strong> sub text
  <strong>more text!</strong>
</span>

Upvotes: 2

Facundo La Rocca
Facundo La Rocca

Reputation: 3866

  • 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;
}
<div class="row">
  <label for='product-45-45'>
  <input type='checkbox' style="float:left;" id='product-45-45' />
  <div class="accord-text">

  </div>
</label>

  <strong>header:</strong> sub text
  <strong>more text!</strong>
</div>

Upvotes: 5

Pixelomo
Pixelomo

Reputation: 6737

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;
}

Upvotes: 0

Related Questions